Hello
On Tue, 11 Dec 2007, caesarkim wrote:
> I have a jms client consuming a message from queue in the activeMQ, but for
> some reason, it hangs and throws the following error. I am using ActiveMQ
> 4.1. My JMS client is using multiple connections.
>
> here is pseudo code.
> JMSClient extends Thread
> {
> public void run()
> {
> ActiveMQConnectionFactory factory = ...
> Connection conn = factory.createQueueConnection();
I don't have the JMS spec handy, but I think the problem is probably
being caused by this line. I don't think the connection factory is
guaranteed to be thread safe.
> QueueSession session = conn.createQueueSession();
> QueueReceiver receiver = session.createReceiver(queue);
> while(true)
> {
> Message m = receiver.receive(1);
> // Receiving a message.
> }
>
>
> }
> }
>
> for(int i = 0; i < 10; i++)
> {
> new JMSClient().start();
> }
>
>
>
> IllegalStateException: The Consumer is closed at
> org.apache.activemq.ActiveMQMessageConsumer.checkClosed(ActiveMQMessageConsumer.java:672)
>
> Does anybody know why it is thrown and how to prevent this error?
>
> Please correct this if i am using the jms client in a wrong way for
> multi-threading.
If I remember correctly, the JMS 1.1 specification says that you can
share a connection between threads, but that a session should only be
used in a single thread at any given time.
So you can do:
ConnectionFactory factory = ...;
for (...) {
// connection per thread
new JMSClient(factory.createConnection()).start();
}
or:
ConnectionFactory factory = ...;
// single connection shared by all threads
Connection conn = factory.createConnection();
for (...) {
new JMSClient(conn).start();
}
or
ConnectionFactory factory = ...;
Connection conn = factory.createConnection();
for (...) {
new JMSClient(conn.createSession()).start();
}
I'm pretty sure the last one will work. The others might work, but I
don't have the JMS spec to check if they should.
Hope that helps.
Cheers,
Albert