In the third and final part of this tutorial we will crate a MDB (Message Driven Bean) that listens to our embedded ActiveMQ from Part 1.
Pre-Requirements:
- Netbeans 6.5 +
- Glassfish Setup from Tutorial Part 1
Tutorial:
- Create a new EJB AmdMDB
- Create a Message Driven Bean NewMessageBean
- Create a Glassfish Descriptor glassfish-ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE glassfish-ejb-jar PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 EJB 3.1//EN" "http://glassfish.org/dtds/glassfish-ejb-jar_3_1-1.dtd"> <glassfish-ejb-jar> <enterprise-beans> <ejb> <ejb-name>NewMessageBean</ejb-name> <mdb-resource-adapter> <resource-adapter-mid>activemq-rar-5.5.0</resource-adapter-mid> </mdb-resource-adapter> <mdb-connection-factory> <jndi-name>amqpool</jndi-name> </mdb-connection-factory> </ejb> </enterprise-beans> </glassfish-ejb-jar> - Create a Standard Deployment Descriptor ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?> <ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee" version = "3.1" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"> <display-name>Ejb1</display-name> <enterprise-beans> <message-driven> <display-name>NewMessageBean</display-name> <ejb-name>NewMessageBean</ejb-name> <ejb-class>mdb.NewMessageBean</ejb-class> <messaging-type>javax.jms.MessageListener</messaging-type> <transaction-type>Container</transaction-type> <activation-config> <activation-config-property> <activation-config-property-name>DestinationType</activation-config-property-name> <activation-config-property-value>javax.jms.Queue</activation-config-property-value> </activation-config-property> <activation-config-property> <activation-config-property-name>Destination</activation-config-property-name> <activation-config-property-value>amqmsg</activation-config-property-value> </activation-config-property> </activation-config> </message-driven> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>NewMessageBean</ejb-name> <method-name>onMessage</method-name> <method-params> <method-param>java.lang.String</method-param> </method-params> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar> - Deploy the EJB
- Send a message from ActiveMQ Web Console
- Send Message from external Client (from Part 2)
Resume
In the 3 parts of the tutorial we walked through all the steps from embedding ActiveMQ into Glassfish, sending messages from both admin client and external client and receiving messages in a MDB. It is a working sample, that requires some finetuning towards more security and certainly features. I hope it helps you to get started, even all the nitty-gritty details and JMS science stuff are omitted. I believe it is much easier to get started with a working sample and begin to adopt to your own needs and read more in some of the JMS, ActiveMQ and Glassfish books and websites.
I spend more time trying to get a external client to talk to Glassfish JMS (unsuccessfully) than to get the above setup into a production system ! Thanks to AvtiveMQ team for this great messaging tool.











I have a problem with setting Resource Adapter Config
ServerUrl: vm://localhost:61616
BrokerXmlConfig: broker:(tcp://localhost:61616)
When I set localhost MDB (tutorial part 3 works), but remote application won’t work.
When I set 0.0.0.0 MDB won’t work, but remote application (part 2 works).
Pingback: Glassfish V3.1 running embedded ActiveMQ for JMS (Updates) | The JavaDude Weblog
Thanks for writing this very comprehensible tutorial.
Is there any way i could get rid of the activemq-rar-5.5.1 setting in the glassfish-ejb-jar.xml so that i do not have this external dependecy when it comes to providing support for other broker-implementations such as OpenMQ? I have tried to simply remove the setting and it did work to a certain extent. Messages could be send to the broker but could not be received by the MDBs.
Every help / hint would be much appreciated.
Don’t think it is possible without this. I had the same experience.
Hmm, thanks for the quick reply. It seems to me the Adapter causes more problems than it solves ^^
I think I will have a look at the genericra Ressource-Adapter to see if it provides a better solution.
Didn’t manage to get the generic working either…
Hi, thanks for writing this guide. It was a big help in setting up the basic functionality.
Could you point me in the right direction in securing the embedded ActiveMQ? I’m trying to prevent users from queuing or consuming messages without a username/password. I’ve tried setting a UserName and Password property on the Resource Adapter Config, and also setup simpleAuthenticationPlugin in broker-config.xml. Neither seem to work, as anyone can still interact with the queue.
I would appreciate any help. Thanks!
I am facing the same requirement for a production system, secure webconsole and broker.
Trying to apply GF security with user realm for the webconsole.
The broker-config.xml is NOT read for embedded AMQ if you follow above tutorial.
Let me try too, you need to place sections in the ra.xml in order to configure through GF.
Will post another entry on this.