You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by neek <ne...@nickfenwick.com> on 2011/09/02 07:41:14 UTC

Stopping an embedded Broker (due to webapp reload)

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.

Re: Stopping an embedded Broker (due to webapp reload)

Posted by neek <ne...@nickfenwick.com>.
Thanks Dejan.  So just to clarify:

private BrokerService service =
BrokerFactory.createBroker("xbean:activemq.xml");

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

public void destroy() {
    service.stop();
    service.waitUntilStopped();
    service = null;
}

Might be worth setting to null in destroy() just in case the finalization or
garbage collection of the servlet isn't prompt.

Cheers
Nick

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

Re: Stopping an embedded Broker (due to webapp reload)

Posted by Dejan Bosanac <de...@nighttale.net>.
Hi Nick,

yeah that's the way to go. Just define the broker as a variable in your
servlet. The broker is just a regular Java object so no problems there.

Regards
-- 
Dejan Bosanac - http://twitter.com/dejanb
-----------------
The experts in open source integration and messaging - http://fusesource.com
ActiveMQ in Action - http://www.manning.com/snyder/
Blog - http://www.nighttale.net


On Fri, Sep 2, 2011 at 7:41 AM, neek <ne...@nickfenwick.com> wrote:

> 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.
>