问题
I get the following error on shutdown, using an embedded broker:
Exception in thread "ActiveMQ ShutdownHook" java.lang.NoClassDefFoundError: org/apache/activemq/broker/BrokerService
Have I misconfigured something possibly? I used the config spelled out on the activemq page here:
http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html
(the configuration labeled "using spring-2.0" at the bottom). The messages are being delivered properly etc. so I'm not sure if this error is harmless.
回答1:
This is an old issue that can occur when ActiveMQ is embedded and not configured 100% correct. The only thing that is left out in the config you linked is to add useShutdownHook="false" to the <broker> element of the XML config. Below is a quick example:
<amq:broker useJmx="false" persistent="false" useShutdownHook="false">
...
</amq:broker>
This will disable the shutdown hook and prevent this problem from occurring.
回答2:
Actually I would leave the shutdown hook enabled - but it has to be defined on the right place. Some one have implemented it for a reason ;)
I do not know what type of deployment you have, but let's assume web application. In such case you should register shutdown hook in web.xml as ServletContextListener. In this case it would get triggered during shutdown of the web application, and at that time classpath contains all ActiveMQ classes and it would work as expected.
Registering shutdown hook on JVM in case of web application could be problematic. Web container (tomcat) during shutdown will first stop web applications and afterwards JVM shutdown hooks will get executed.
ActiveMQ registers custom threads and those will keep running after web application has been stoped but they will not be able to access classes from this web application (since it has been destroyed) and so you will get NoClassDefFoundError.
One more thing: maybe you can disable shutdown hook. If you are loading ActiveMQ as Spring Beans and Spring context is being loaded from web.xml (Spring Web Context), then Spring will register hooks automatically and close AcrtiveMQ beans correctly.
回答3:
You can stop the activemq instance after integration tests are done. Just add the following lines to your pom.
<execution>
<id>stop-activemq</id>
<goals>
<goal>stop</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
来源:https://stackoverflow.com/questions/8315194/activemq-embedded-broker-exception-in-shutdown-hook