You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by kneumei <ky...@pramari.com> on 2008/10/02 16:22:29 UTC

How to properly close a queue?

Hi,
I am building an application in which I am using ActiveMQ to send messages
to clients.  So far I have something like this.  The BrokerService and
connectionFactory are created when my application starts up and are put into
a singleton, and the connectionFactory is made available to other parts of
the program:

 BrokerService broker = new BrokerService();
 broker.addConnector("tcp://localhost:61616");
 broker.setPersistent(false);
 broker.setUseJmx(false);
 broker.start();
 ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory();
 connectionFactory.setBrokerURL("tcp://localhost:61616");


When some part of my program wants to create a new queue, it creates a new
MyMessageQueue object which has these two methods.  The start method is used
to create a new queue, and the stop method is used to destroy it: 


	private Connection connection;
	private Session session;
	private Destination destination;
	private MessageProducer messageProducer;
	private String queueName;

	public MessageQueueImpl(String queueName) {
		this.queueName = queueName;
	}

	public void startMessageQueue(ConnectionFactory connectionFactory)
			throws JMSException {
		connection = connectionFactory.createConnection();
		connection.start();
		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		destination = session.createQueue(queueName);
		messageProducer = session.createProducer(destination);
	}


	public void stopMessageQueue() throws JMSException {
		messageProducer.close();
		session.close();
		connection.stop();
		connection.close();
	}


I have recently been profiling my application and noticed that after the
stopMessageQueue() method is called, a thread called
"QueueThread:queue://QUEUENAME"  has not died.  Am I doing something wrong
when shutting down the queue?

Thanks,
Kyle
-- 
View this message in context: http://www.nabble.com/How-to-properly-close-a-queue--tp19780281p19780281.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: How to properly close a queue?

Posted by kneumei <ky...@pramari.com>.
Regarding the QueueThread: Here is the stack trace for it:

"QueueThread:queue://1" daemon prio=6 tid=0x0adc9c00 nid=0x8a8 waiting on
condition [0x0bc0f000..0x0bc0fc94]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x031aa7d8> (a
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(Unknown Source)
	at
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown
Source)
	at java.util.concurrent.LinkedBlockingQueue.take(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

   Locked ownable synchronizers:
	- None


-- 
View this message in context: http://www.nabble.com/How-to-properly-close-a-queue--tp19780281p19780800.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: How to properly close a queue?

Posted by James Strachan <ja...@gmail.com>.
2008/10/2 kneumei <ky...@pramari.com>:
>
> Perhaps I am misunderstanding how JMS works, or maybe I didn't explain myself
> very well.  How I am viewing JMS's relationship to my code right now is like
> this:  My business objects (with their associated queues) are on the server.
> They produce messages.  Consumers (on a client machine) may come and go, but
> the queue should not be destroyed until the business object is (that way
> future clients might connect to the same queue).  If my understanding of
> temporary queues is correct, the queue will be destroyed whenever a client
> disconnects from the queue.

The queue is owned by the connection which creates it.  So your
business object creates the queue; when you close it, you close the
connection and the temporary queue goes away.

-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://open.iona.com

Re: How to properly close a queue?

Posted by kneumei <ky...@pramari.com>.
Perhaps I am misunderstanding how JMS works, or maybe I didn't explain myself
very well.  How I am viewing JMS's relationship to my code right now is like
this:  My business objects (with their associated queues) are on the server. 
They produce messages.  Consumers (on a client machine) may come and go, but
the queue should not be destroyed until the business object is (that way
future clients might connect to the same queue).  If my understanding of
temporary queues is correct, the queue will be destroyed whenever a client
disconnects from the queue.  So what I am trying to figure out is when the
object that produces messages has reached its lifecycle end, how do I clean
up the queue that was associated with it.
-- 
View this message in context: http://www.nabble.com/How-to-properly-close-a-queue--tp19780281p19782218.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: How to properly close a queue?

Posted by James Strachan <ja...@gmail.com>.
2008/10/2 kneumei <ky...@pramari.com>:
>
> Is there a way to delete the queue programatically from the broker instead of
> using JMX? In my application, there are certain business objects that each
> have an associated queue.  When the object is deleted, I need the queue to
> close.
>
> If there is not a way to do this, I imagine that I am not using activeMQ in
> the way it was intended and need to do more research.

Why not use temporary queues - they do exactly this?

-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://open.iona.com

Re: How to properly close a queue?

Posted by kneumei <ky...@pramari.com>.
Is there a way to delete the queue programatically from the broker instead of
using JMX? In my application, there are certain business objects that each
have an associated queue.  When the object is deleted, I need the queue to
close.

If there is not a way to do this, I imagine that I am not using activeMQ in
the way it was intended and need to do more research.
-- 
View this message in context: http://www.nabble.com/How-to-properly-close-a-queue--tp19780281p19781592.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: How to properly close a queue?

Posted by James Strachan <ja...@gmail.com>.
BTW if you want the queue to disappear when your JMS consumer closes,
use a temporary queue

2008/10/2 James Strachan <ja...@gmail.com>:
> Each queue uses up resources in the broker unless you use the JMX APIs
> to delete queues from the broker.
>
> 2008/10/2 kneumei <ky...@pramari.com>:
>>
>> Hi,
>> I am building an application in which I am using ActiveMQ to send messages
>> to clients.  So far I have something like this.  The BrokerService and
>> connectionFactory are created when my application starts up and are put into
>> a singleton, and the connectionFactory is made available to other parts of
>> the program:
>>
>>  BrokerService broker = new BrokerService();
>>  broker.addConnector("tcp://localhost:61616");
>>  broker.setPersistent(false);
>>  broker.setUseJmx(false);
>>  broker.start();
>>  ActiveMQConnectionFactory connectionFactory = new
>> ActiveMQConnectionFactory();
>>  connectionFactory.setBrokerURL("tcp://localhost:61616");
>>
>>
>> When some part of my program wants to create a new queue, it creates a new
>> MyMessageQueue object which has these two methods.  The start method is used
>> to create a new queue, and the stop method is used to destroy it:
>>
>>
>>        private Connection connection;
>>        private Session session;
>>        private Destination destination;
>>        private MessageProducer messageProducer;
>>        private String queueName;
>>
>>        public MessageQueueImpl(String queueName) {
>>                this.queueName = queueName;
>>        }
>>
>>        public void startMessageQueue(ConnectionFactory connectionFactory)
>>                        throws JMSException {
>>                connection = connectionFactory.createConnection();
>>                connection.start();
>>                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
>>                destination = session.createQueue(queueName);
>>                messageProducer = session.createProducer(destination);
>>        }
>>
>>
>>        public void stopMessageQueue() throws JMSException {
>>                messageProducer.close();
>>                session.close();
>>                connection.stop();
>>                connection.close();
>>        }
>>
>>
>> I have recently been profiling my application and noticed that after the
>> stopMessageQueue() method is called, a thread called
>> "QueueThread:queue://QUEUENAME"  has not died.  Am I doing something wrong
>> when shutting down the queue?
>>
>> Thanks,
>> Kyle
>> --
>> View this message in context: http://www.nabble.com/How-to-properly-close-a-queue--tp19780281p19780281.html
>> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>>
>>
>
>
>
> --
> James
> -------
> http://macstrac.blogspot.com/
>
> Open Source Integration
> http://open.iona.com
>



-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://open.iona.com

Re: How to properly close a queue?

Posted by James Strachan <ja...@gmail.com>.
Each queue uses up resources in the broker unless you use the JMX APIs
to delete queues from the broker.

2008/10/2 kneumei <ky...@pramari.com>:
>
> Hi,
> I am building an application in which I am using ActiveMQ to send messages
> to clients.  So far I have something like this.  The BrokerService and
> connectionFactory are created when my application starts up and are put into
> a singleton, and the connectionFactory is made available to other parts of
> the program:
>
>  BrokerService broker = new BrokerService();
>  broker.addConnector("tcp://localhost:61616");
>  broker.setPersistent(false);
>  broker.setUseJmx(false);
>  broker.start();
>  ActiveMQConnectionFactory connectionFactory = new
> ActiveMQConnectionFactory();
>  connectionFactory.setBrokerURL("tcp://localhost:61616");
>
>
> When some part of my program wants to create a new queue, it creates a new
> MyMessageQueue object which has these two methods.  The start method is used
> to create a new queue, and the stop method is used to destroy it:
>
>
>        private Connection connection;
>        private Session session;
>        private Destination destination;
>        private MessageProducer messageProducer;
>        private String queueName;
>
>        public MessageQueueImpl(String queueName) {
>                this.queueName = queueName;
>        }
>
>        public void startMessageQueue(ConnectionFactory connectionFactory)
>                        throws JMSException {
>                connection = connectionFactory.createConnection();
>                connection.start();
>                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
>                destination = session.createQueue(queueName);
>                messageProducer = session.createProducer(destination);
>        }
>
>
>        public void stopMessageQueue() throws JMSException {
>                messageProducer.close();
>                session.close();
>                connection.stop();
>                connection.close();
>        }
>
>
> I have recently been profiling my application and noticed that after the
> stopMessageQueue() method is called, a thread called
> "QueueThread:queue://QUEUENAME"  has not died.  Am I doing something wrong
> when shutting down the queue?
>
> Thanks,
> Kyle
> --
> View this message in context: http://www.nabble.com/How-to-properly-close-a-queue--tp19780281p19780281.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>
>



-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://open.iona.com