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.
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.
Than you can add a new Message Driven Bean to your project
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.
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!!








Great! But I have a problem:
When I try the Remote invocation procedure, my Glassfish admin console doesn’t permit me to check the “Java Web Start” checkbox. It is uncheckable. Do you know why?
Thanks
Hi,
Thanks for such wonderful tutorial.
Really Helpful.
Nice tutorial, it worked really well.
. Thanks….
I used Netbeans 7.0 prior but show failed glassfish server 3.1 error. Then i used netbeanas 6.5 and glassfish 2.0, it worked
Congratulations for the tutorial! I’ve made it easier with Netbeans 7 and Glasshfish v3!
I got an error message when unfolding the factoryPool
REST Request ‘http://localhost:4848/management/domain/resources/connector-resource/jms/helloFactoryPool‘ failed with response code ’404′.
It worked great in 7.0 with a EE 5 profile
I tried this step by step….
and everything worked justfine but I recived an error:—
“deploy?path=C:\Users\vbj\Documents\NetBeansProjects\MDBClient\dist\MDBClient.jar&name=MDBClient&force=true failed on GlassFish Server 3″
also I cannot save the settings in QueueConnectionFactory. it says:-
” An error has occurred
error.noSuchProxy”
Make sure you use the full JEE 6 profile. The Web Profile may not contain all the features needed.
Also check your deployment descriptor. Maybe it forces the application to use Java EE 5 instead of Java EE 6.
You can also try to undeploy the app, restart the server and clean and redeploy your app again. That can solve problems sometimes.
Check the beans.xml file for some errors.