I was looking how Message Driven Beans in JEE 6 works and I was surprised that it isn’t that difficult. I was especially surprised about the support for MDB’s (and the other beans) in Netbeans. Creating a working example with a MDB is just a matter of minutes.

In this post I’ll try to teach you how to create a message driven bean which can communicate with an application client.
In order to follow this tutorial, I’ll assume you have Netbeans (6.9) and Glassfish available on your system.

Creating the Message driven bean

Create a new project in Netbeans and choose for a Java EE – EJB Module.

Create a new project

Next, give your project a name and make sure you select Glassfish 3 as your server and Java EE 6 as your Java EE version.

Server and settings

Server and settings

Than you can add a new Message Driven Bean to your project

Add a Message Driven Bean

Give the Bean a name and a package.
Click next to destinations on the Add button to add a destination.
Choose a name for your destination.
Choose between Queue or Topic. In this example it isn’t important which one you choose. The difference between Topic and Queue is if you want Point-to-point or Publish/subscribe communication for your beans.

Name your bean

Netbeans creates code for you and, except for the actual businesscode, your bean is ready.

In this simple example, I just want to pass a name from my applicationclient to my bean and my bean has to echo “Helllo name”

The full code looks like this:

package hello;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

@MessageDriven(mappedName = "jms/hello", activationConfig =  {
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
    })
public class HelloBean implements MessageListener {

    public HelloBean() {
    }

    public void onMessage(Message message) {
        try {
            System.out.println("Hello " + message.getStringProperty("name"));
        } catch (JMSException ex) {
            Logger.getLogger(HelloBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Almost all the configuration is mapped into 1 annotation.
- The annotation @MessageDriven transforms your normal class into an actual Message Driven Bean.
- The @ActivationConfigProperty annotation is to be used for defining properties to your bean such as the acknowledge mode, message selector, subscription durability and destination type.
In previous versions of JEE, you had to create an XML-file to define all these properties.

Our bean is finished and can be deployed.
Start your Glassfish server and simply click on the deploy button to deploy your bean into Glassfish.

If you open your Glassfish admin console (by default located at http://localhost:4848/ ), you should see that your application is successfully deployed and activated into Glassfish.

Also verify that the JMS resources for your MDB are succesfully created

Creating the Application client

The MDB is created and is waiting for messages. Now it’s time to create the Application client.
Just create a new project and choose for an Enterprise Applicaton Client from the list

The code for the applicaton client looks like this:

package mdbclient;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;

public class Main {

    @Resource(mappedName = "jms/helloFactory")
    private static ConnectionFactory connectionFactory;

    @Resource(mappedName = "jms/hello")
    private static Queue queue;

    public static void main(String[] args) throws JMSException {
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(queue);
        Message message = session.createTextMessage();
        message.setStringProperty("name", "World!!");
        producer.send(message);
        System.exit(0);
    }
}

If you deploy and run this application you should see in the server output the string “Hello World!!

What’s going on?
First we have to create 2 resources to inject the parameters into the Queue and ConnectionFactory . This can be done easily with the @resource annotation.
Next, we just have to create the actual jms-message. I think the code is pretty clear so it shouldn’t be to difficult to understand what’s going on.

Remote invocation

It would be cool if we can run the client from a remote computer.
This is also very easy with webstart.

Go to your Glassfish admin and simply enable Java Web Start.

If you launch this jnpl file from a remote compter, you should again see the message “Hello World!!” in your Glassfish Server output.

When you get an error, it’s possible that you have to change the IP address of the orb-listener-1 to the IP address of the computer where your Glassfish is running on.

Happy Messaging!!