Tag Archives: Java EE

Monitor an Active MQ queue with Hermes JMS

Hermes JMS is a great tool to monitor the activities on your JMS broker. Especially, it is quite useful to see what are the messages transiting in the queues and topics (you can also record them and replay them, which is pretty cool).


However, when you are monitoring the messages transiting in a queue, Hermes JMS only takes a snapshot of what is pending, i.e. not yet delivered. That means that if you have a consumer plugged on this queue, you will barely see any message since they are immediately consumed as they are sent (most of the cases at least)… It is a problem when you want to see the flow of messages, record it and replay it.

Mirrored queues

I am using Apache ActiveMQ as a JMS Broker. My solution for this broker is to use mirrored queues.

This feature of ActiveMQ enables to route messages from one destination to another without any external interventions. So if we route messages from the queue we want to monitor to a topic, messages coming to the queue will be automatically sent to the topic. If we subscribe to this topic with Hermes JMS, then we are able to see all messages which have been sent to the original queue. Indeed, topics are easier to monitor with Hermes JMS. We see there the continuous flow of messages as they arrive.

How is this done in ActiveMQ? I requires very few configuration in your activemq.xml.

First you need to configure the broker instance as enabling virtual topics:

<broker brokerName="localhost" 
   dataDirectory="${activemq.data}" ... >

Then, if we assume the queue to monitor is called test.queue and if you want to route the messages to be routes to a topic called test.queue.mirror, you have to create a destination interceptor:

			<compositeQueue name="test.queue" forwardOnly="false">
					<topic physicalName="test.queue.mirror"/>

Be sure the queue and topic destinations exists as well:

	<amq:queue physicalName="test.queue"></amq:queue>
	<amq:topic physicalName="test.queue.mirror"></amq:topic>

Back to Hermes JMS

Here we are! Restart ActiveMQ and you will notice that when you send a message to queue.test, this message is also sent to test.queue.mirror.

Start to Hermes JMS and connect to the topic: you see the flow of messages sent to the queue arriving.

Hermes JMS with mirrored queue

Java EE startup and shutdown callbacks

Hurray, this is the first post of this blog!
As a first subject, I would like to discuss a problem where it was quite hard to find a solution on the Web. Let me share this with you.

The problem

Given a standard Java EE 5 application (with EJBs, Servlets…), how to develop some code which will be started when the application startup/shutdown?

In other words, how to execute something when you start or stop your application?
Indeed, when you start the server you may need to initialize some stuff like initializing your cache, and when you stop the server, wouldn’t it be great to do some data cleanup?

In my case, our application was a pure back-end application (only EJBs) which embedded a remote service interface and we wanted to register it (resp. unregister it) to a service locator at the application startup (resp. shutdown).

Unsatisfactory solutions

Application server and framework tricks

In fact, most of the application servers already come with their own solutions. In Weblogic, for example, we have the notion of startup/shutdown classes (but admittedly, I have not tested it).

But in our case, we had to deploy on a JBoss 6 server! And moreover, wouldn’t it be preferable to have a pure Java EE way, so that it could be deployed anywhere with minimal configuration?

If we were using some Java development framework like Seam, there are also some tricks, like using an observer on the org.jboss.seam.postInitializatio event like this:

public class Initializer {

   public void init() {
      // startup code here

But once again, this is a specific solution and if you are not using Seam then you should find something else (besides, I haven’t looked for the implementation of the shutdown callback with Seam).

Static blocks in EJBs

So what if we just put static block in EJBs and let’s just hope that when the EJB class will be loaded, the static code will be executed?
Well, this is a bad idea. By definition, you have no control on how the EJBs are loaded by the application server. It may even not be called before the first invocation of the EJB.
So this is obviously not a good solution.

EJB 3.1

It is true that we are speaking only about Java EE 5 in this post, but I just want to mention that in Java EE 6 with EJB 3.1, we can use the pretty code below:

public class StartupShutdownBean {

	private void startup() {
		// your startup code here

	private void shutdown() {
		// your shutdown code here


The solutions

So, the only solution found was to use the servlet API. But the good news is that you have two choices:

Using Servlet init and destroy methods

The idea here is to have the startup and shutdown code in the init() and destroy() methods of a servlet embedded in your application. So if you do not have any servlet in your application, you will have to create one like this with these methods.

public class InitServlet extends HttpServlet {

	public void init() throws ServletException {
		// startup code here

	public void destroy() {
		// shutdown code here

You should guarantee that this servlet is loaded on start-up, (and potentially before the other servlets). This is guaranteed by the following statement in the web.xml.


The number between the load-on-startup tag indicates the order in which servlet are loaded on startup (the lowest are first).
This solution is ok admittedly, especially if you have already servlets in your application. But what if you have none? Isn’t it a bit costly to create a servlet listening to coming requests just for two methods to call during the application life?
For that reason, I prefer therefore the second solution, which is…

Using Servlet context listeners (the one I prefer)

The other solution is still in the servlet API, and it is to use a servlet context listener.
This comes as a Java interface ServletContextListener. Classes implementing this interface will be loaded before (resp. after) the instantiation (resp. the destruction) of all servlets, therefore, it is a good place to put your application startup/shutdown code.
Here is how it is done:

public class InitContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent ce) {
        // startup code here

    public void contextDestroyed(ServletContextEvent ce) {
        // shutdown code here

You will also need to declare the context listener in the web.xml with the following statement:


That’s all…


So, to sum up, it seems that to have some startup/shutdown code in your application there is no other clean solution than to use the servlet API, either with the init()/destroy() methods or with a servlet context listener.

The other best/long-term/cleanest solution is of course to migrate to Java EE 6, but not everyone is ready to do it, right?