activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From neek <n...@nickfenwick.com>
Subject Stopping an embedded Broker (due to webapp reload)
Date Fri, 02 Sep 2011 05:41:14 GMT
Hi guys,

I have an embedded broker, created in a servlet's init() method with:

				BrokerService brokerService = BrokerFactory.createBroker(
					new URI("xbean:broker.xml"));
				brokerService.start();

I use this servlet to manage lifetime of various services used by the web
application.  I'm finding that reloading the web app causes the broker to
fail to start because it's already running:

nested exception is javax.management.InstanceAlreadyExistsException:
org.apache.activemq:BrokerName=bikesbroker,Type=Broker (Error creating bean
with name 'org.apache.activemq.xbean.XBeanBrokerService#0' defined in class
path resource [broker.xml]:

So it seems I must manually get hold of, and stop, the broker instance in my
servlet's destroy() method, to match the startup operation performed in
init().

I've read http://activemq.apache.org/how-do-i-restart-embedded-broker.html
which addresses this situation, but I'm unclear what to do when a
potentially long period of time may have passed between making the start()
and stop() calls.

How should I get hold of the broker?  Should I call createBroker again, in
the same way as in init()?  From the how-do-i-restart-embedded-broker page,
with a "NEW LINE" added:

public void init(ServletConfig config) throws ServletException {
    BrokerService service =
BrokerFactory.createBroker("xbean:activemq.xml"); 
    service.start();
    service.waitUntilStarted();
}

public void destroy() {
    BrokerService service =
BrokerFactory.createBroker("xbean:activemq.xml"); // NEW LINE
    service.stop();
    service.waitUntilStopped();
}

I'm presuming I shouldn't simply hold on to the BrokerService instance
created in init() and re-use it in destroy().  Would that be safe?  I've
tried it, and it does stop the broker, and the broker then start
successfully as the webapp reloads, so this seems to work but I'd like
advice on whether it's safe.

Thanks for any advice.
Nick

--
View this message in context: http://activemq.2283324.n4.nabble.com/Stopping-an-embedded-Broker-due-to-webapp-reload-tp3785326p3785326.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Mime
View raw message