简体   繁体   中英

JMS ActiveMQ queue already exists

I have a standalone java application that fires up several JMS consumers using the spring-jms tags in my spring config:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Here is what the bean definitions look like:

<bean id="fooConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="${foo_broker_url}" />
</bean>

<jms:listener-container connection-factory="fooConnectionFactory" acknowledge="client" client-id="fooService">
    <jms:listener destination="${foo_queue_name}" ref="fooListener" selector="${foo_selector}" />
</jms:listener-container>
...

I plan to write a Windows batch file to start up the consumer, and a separate batch file that will shut it down (for the moment I have been starting it up using the command line).

My problem is two-fold:
1. How would the shut-down batch file actually go about shutting down the connections?
2. If the startup batch file is executed while the queues are already connected, I don't want the application to create a second connection. Any ideas how to prevent this?

Regarding question number 2: I understand that attempting to send a message to the queue and waiting for a response or failure might work, but it seems like overkill and there must be a better way. I read this in the API documentation for the javax.jms.Connection class:

"If another connection with the same clientID is already running when this method is called, the JMS provider should detect the duplicate ID and throw an InvalidClientIDException."

However, specifying a clientID on the listener container (see bean definitions above) does not seem to prevent a second connection.

Update

I've determined that the easiest solution may be to run this application as a webapp under Tomcat. I will leave the question open, however, under the hopes that someone will provide a viable solution to either of the questions posed.

The first question could be rephrased/clarified as the following: How would I terminate an application/thread that is running by executing a separate application/script?

The second question could be elaborated on by asking: Is there a piece of data relating to the JMS producer connection to a queue that would make it unique? Based on this unique data could it be established that "this" connection already exists?

I have determined that the easiest way to handle these problems is to convert my standalone to a webapp and deploy under Tomcat. This way I will be able to more easily manage starting/stopping the application, and I will be able to ensure that only one instance of the application is running at a given time.

I'll be monitoring this question going forward, so feel free to post your own solution and I would be willing to change the accepted answer to yours!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM