Wednesday, September 21, 2011

Solution for: Spring JMS + ActiveMQ + Tomcat = Tomcat does not shutdown

This is one of the most frustrating problems. When you decide to use Tomcat with Spring JMS to connect and message with ActiveMQ, everything works fine till you try to shutdown Tomcat.  It keeps throwing exception and looks like the DefaultMessageListnerContainer thread lives on. As seen from the logs on tomcat console, the problem occurs because the tomcat shutsdown before the JMS Spring container “DefaultMessageListenerContainer” and you will see Nullpointer, Classnotfound exceptions, as tomcat container is down and the classloader doesn’t exist to give the necessary classes from the previously loaded jars. And I could not find a single solution on google. After, much googling I found a solution for a similar problem when using Quartz, tomcat and spring. So, this solution was derived from that one [Sorry, I don’t have that link anymore].

Create a ServletContextListener, and in contextDestroyed method get the jms container bean to manually call shutdown. The listener contextInitialized and contextDestroyed are called at start and end of Servlet lifecycle. The below code assumes that you have a class called SpringApplicationContextInstance that gives you the spring context using which you can access your beans defined in spring context.

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

* Created by Shreyas Purohit
public class JMSContainerShutDownHook implements ServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
LogManager.log(Level.INFO, "JMSContainerShutDownHook: Initialized called");

public void contextDestroyed(ServletContextEvent servletContextEvent) {
LogManager.log(Level.INFO, "JMSContainerShutDownHook: Destroyed called");
try {
LogManager.log(Level.INFO, "JMSContainerShutDownHook: Fetching JMS Container Bean from Application Context");
DefaultMessageListenerContainer container = SpringApplicationContextInstance.getInstance().getBean("jmsContainer");
LogManager.log(Level.INFO, "JMSContainerShutDownHook: Calling shutdown on DefaultMessageListenerContainer");
Thread.sleep(3000); //Wait for the container to shutdown
} catch (Exception e) {
LogManager.log(Level.ERROR, e);
LogManager.log(Level.INFO, "JMSContainerShutDownHook: Exiting");

Wednesday, September 14, 2011

ActiveMQ 5.5.0 performance test plugin

It not very simple to apply the perf test instructions given at I encountered many problems, and here is a way to make it run.

Required: Maven 2, SVN client like tortoise SVN


1. Checkout the source code for Active MQ 5.5.0 from SVN to directory AMQ

2. Checkout perf test code from SVN to directory AMQPerf

3. cd AMQ/activemq-tooling/maven-activemq-perf-plugin and edit pom.xml. Remove line <scope>test</scope> from org.slf4j dependency.

4. cd AMQPerf and edit pom.xml. Change <version> in <parent> to 5.5.0

5. cd AMQ/activemq-tooling and run “mvn clean install”

6. cd AMQPerf and run “mvn clean install”

7. Run Commands as provided in from the AMQPerf directory in separate command prompts/Consoles, that is-

Console1> mvn activemq-perf:consumer
Console2> mvn activemq-perf:producer

This works for me perfectly.

Monday, September 12, 2011

Ruby On Rails mysql2 error

I was having trouble installing mysql2 gem on my Win7 64 bit. I have copied these instructions from stackoverflow. And these really work.

For Win 7:

  1. Download a zip file with mysql server 5.1 NOT the msi one. Make sure it's 32-bit NOT 64-bit. (From here
  2. Since there is no installer file with this, create a folder c:\mysql-gem-install - you can remove it once you finish.
  3. Extract all the files from the zip file into the folder you just created.
  4. now run this command
    gem install mysql2 -- '--with-mysql-lib="c:\mysql-gem-install\lib\opt" --with-mysql-include="c:\mysql-gem-install\include"'
I just installed mysql2 gem v. 0.3.7
EDIT: One more thing: make sure you run the command in Command Prompt directly. As in not PowerShell or Consol2 - for some reason if you try that it will give you and error " invalid option" - has to do with the way -- is parsed.
--Answered by Nick Gorbikoff

For Linux:

sudo apt-get install libmysql-ruby libmysqlclient-dev

--Answered by spacemonkey

I successfully installed mysql 5.5, and gem mysql2-0.3.7