Thursday, June 4, 2009

How to use Eclipse to debug ActiveMQ

This article shows you how to debug source code of ActiveMQ from a remote machine.

Before Reading

  • Install Linux. Here I use Ubuntu 8.04
  • Install ActiveMQ. Here I use version 5.2.0.
  • Install JDK. Here I use Java 1.6 (build 1.6.0_07-b06)
  • Install Maven 2. Here I use version 2.2.
  • Prepare environment variables
  • Install Eclipse. Here I use version 3.2.
  • Read http://activemq.apache.org/activemq-520-release.html

Get the Source Code

  • Check out the source code into directory, for example at ~/svn
      cd ~/svn
svn co https://svn.apache.org/repos/asf/activemq/tags/activemq-5.2.0

Prepare Build Environment

  • Make sure that you have proper settings of Java environment, for example JAVA_HOME is the location where your JDK install.
      export JAVA_HOME="/usr/lib/jvm/java6-sun"  
  • Install maven 2

Build the Code

  • Make a symbolic link to real source and switch to the directory activemq, for example:
      ln -s activemq-5.2.0 activemq; cd activemq  
  • Compile and install ActiveMQ:
      mvn clean install   
  • Some files will be found at ~/.m2/repository/org/apache/activemq/apache-activemq/5.2.0/apache-activemq-5.2.0-src.tar.gz and ~/.m2/repository/org/apache/activemq/apache-activemq/5.2.0/apache-activemq-5.2.0-bin.tar.gz
      sudo su mkdir /opt/apache
      cp ~/.m2/repository/org/apache/activemq/apache-activemq-5.2.0-bin.tar.gz /opt/apache/.
      tar -zxvf apache-activemq-5.2.0-bin.tar.gz
      cd /opt/apache
      ln -s apache-activemq-5.2.0/ activemq

Setting Environment Variables

  • Environment variables for ActiveMQ
      export ACTIVEMQ_HOME="/opt/apache/activemq"
export CLASSPATH="$ACTIVEMQ_HOME/activemq-all-5.2.0.jar:$CLASSPATH"
  • Environment variables for enabling remote debug ActiveMQ
    1. Create a file at $HOME/.activemqrc and add a variable ACTIVEMQ_DEBUG=true. Or you can just run with ACTIVEMQ_DEBUG=true ./bin/activemq
    2. Check if a variable $ACTIVEMQ_DEBUG_OPTS is set at $ACTIVEMQ_HOME/bin/activemq as following
 -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
* jdwp: Java Debug Wire Protocol
* transport: indicates that debuggers should attach using sockets
* server: when 'y', listens for a debugger application to attach
* suspend: the JVM should wait for a debugger application to attach before starting up
* address: the port to expose for debugger applications (or address to connect to in order to debug)

Execute Eclipse

Prepare Workspace

  • Create a directory for development purpose, for example ~/dev/activemq and set it as your workspace of Eclipse.
  • Create a new project from File > New > Project, for example a project named research
  • Import the ActiveMQ source into research from File > Import... and select Existing Projects into Workspace from the General folder
  • Click Next to select root directory, for example ~/svn/activemq. Please check the Copy projects into workspace and click Finish.
  • To here, you have many projects imported into ~/dev/activemq

Prepare Debug

  • Switch to Java perspective, from Window > Open Perspectives > Java.
  • Find the Main.java in activemq-console, it is under package org.apache.activemq.console and open it.




  • Toggle breakpoint as you want, for example the first line of public static void main(String[] args) {.
  • Switch to Debug perspective, from Window > Open Perspectives > Debug.
  • Open a terminal and run activemq, for example
cd /opt/apache/activemq
bin/activemq



  • A "Listening for transport dt_socket at address: 5005" message will show when you execute bin/activemq. It will stay there waiting for remote connection.
  • Click the Bug icon and select Debug...
  • Create a Remote Java Application as shown in the figure.
  • Click Debug, the eclipse will attach the remote java application and stop at the breaking point in Main.java.