Skip to main content

MDB that receives from ActiveMQ example

12 replies [Last post]
mikehake
Offline
Joined: 2008-01-14

Hi everyone,
This week I was tasked with getting a MDB in Glassfish to receive messages from ActiveMQ. It took a little bit of work to get it figured out, but now that I know how it works it is actually quite simple. Below is a description of exactly what I had to do. Id also like to thank Ramesh and Zoltan for their help with this!

The first step is to deploy the ActiveMQ resource adapter. The resource adapter comes with the ActiveMQ instillation and can be found at ACTIVEMQ_HOME\lib\optional\activemq-rar-4.1.1.rar. I deployed the resource adapter using the app server admin console by selecting Applications->Connector Modules->Deploy. Since ActiveMQ was running on the same system I accepted all default properties.

Next we need to create an Admin Object Resource that will link the jms queue or topic to our newly deployed resource adapter. From the admin console select Resources->Connectors->Admin Object Resources->New. For JNDI Name I used jms/TestTopic, Resource Type is javax.jms.Topic and be sure to choose the ActiveMQ Resource Adapter. In the Additional Properties set the property Name=TestTopic

The final bit of configuration is to create a Physical Destination. In the Admin Console select Configuration->Java Messaging Service->Physical Destinations->New. Create a Physical Destination with a Name of TestTopic and a Type of javax.jmsTopic. No additional properties are needed.

The code for the Message Driven Bean is as follows:
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Topic"),
@ActivationConfigProperty(propertyName="destination", propertyValue="TestTopic")
},
mappedName = "jms/TestTopic"
)
public class MessageBean implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("Message Received:" + textMessage.getText());

} catch ( JMSException e ) {
e.printStackTrace();
}
}
}

Because we want our MDB to receive messages from ActiveMQ we must also provide a sun-ejb-jar.xml deployment descriptor that specifies what resource adapter should be used. Below is the complete sun-ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>

MessageBean

activemq-rar-4.1.1

Below is a simple stand alone java program that posts JMS messages to a local ActiveMQ instance. If you have the setup correct, the MDB will receive the messages.

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQTest {

public static void main(String[] argv) throws Exception{
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("TestTopic");
MessageProducer publisher = session.createProducer(topic);
publisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
connection.start();
for(int x=0;x<5;x++){
TextMessage message = session.createTextMessage("Test message "+x);
System.out.println("Sending message: "+message.getText());
publisher.send(message);
}
connection.stop();
connection.close();
System.out.println("Done.");
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cppdeveloper

The first part is very good at saying exactly how to do this, but toward the
end more explanation is needed. Where do the MessageBean.java file,
sun-ejb-jar.xml file, and main java program ActiveMQTest.java file sit? Do
we deploy them/do any configuration through the Glassfish admin console?

glassfish-2 wrote:
>
> Hi everyone,
> This week I was tasked with getting a MDB in Glassfish to receive messages
> from ActiveMQ. It took a little bit of work to get it figured out, but now
> that I know how it works it is actually quite simple. Below is a
> description of exactly what I had to do. Id also like to thank Ramesh and
> Zoltan for their help with this!
>
> The first step is to deploy the ActiveMQ resource adapter. The resource
> adapter comes with the ActiveMQ instillation and can be found at
> ACTIVEMQ_HOME\lib\optional\activemq-rar-4.1.1.rar. I deployed the resource
> adapter using the app server admin console by selecting
> Applications->Connector Modules->Deploy. Since ActiveMQ was running on the
> same system I accepted all default properties.
>
> Next we need to create an Admin Object Resource that will link the jms
> queue or topic to our newly deployed resource adapter. From the admin
> console select Resources->Connectors->Admin Object Resources->New. For
> JNDI Name I used jms/TestTopic, Resource Type is javax.jms.Topic and be
> sure to choose the ActiveMQ Resource Adapter. In the Additional Properties
> set the property Name=TestTopic
>
> The final bit of configuration is to create a Physical Destination. In the
> Admin Console select Configuration->Java Messaging Service->Physical
> Destinations->New. Create a Physical Destination with a Name of TestTopic
> and a Type of javax.jmsTopic. No additional properties are needed.
>
> The code for the Message Driven Bean is as follows:
> import javax.ejb.ActivationConfigProperty;
> import javax.ejb.MessageDriven;
> import javax.jms.JMSException;
> import javax.jms.Message;
> import javax.jms.MessageListener;
> import javax.jms.TextMessage;
>
> @MessageDriven(activationConfig = {
> @ActivationConfigProperty(propertyName="destinationType",
> propertyValue="javax.jms.Topic"),
> @ActivationConfigProperty(propertyName="destination",
> propertyValue="TestTopic")
> },
> mappedName = "jms/TestTopic"
> )
> public class MessageBean implements MessageListener {
> public void onMessage(Message message) {
> TextMessage textMessage = (TextMessage) message;
> try {
> System.out.println("Message Received:" +
> textMessage.getText());
>
> } catch ( JMSException e ) {
> e.printStackTrace();
> }
> }
> }
>
> Because we want our MDB to receive messages from ActiveMQ we must also
> provide a sun-ejb-jar.xml deployment descriptor that specifies what
> resource adapter should be used. Below is the complete sun-ejb-jar.xml
>
> > Server 9.0 EJB 3.0//EN"
> "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
>
>
>
> MessageBean
>
> activemq-rar-4.1.1
>

>

>

>

>
> Below is a simple stand alone java program that posts JMS messages to a
> local ActiveMQ instance. If you have the setup correct, the MDB will
> receive the messages.
>
> import javax.jms.Connection;
> import javax.jms.DeliveryMode;
> import javax.jms.MessageProducer;
> import javax.jms.Session;
> import javax.jms.TextMessage;
> import javax.jms.Topic;
> import org.apache.activemq.ActiveMQConnectionFactory;
>
> public class ActiveMQTest {
>
> public static void main(String[] argv) throws Exception{
> ActiveMQConnectionFactory factory = new
> ActiveMQConnectionFactory("tcp://localhost:61616");
> Connection connection = factory.createConnection();
> Session session = connection.createSession(false,
> Session.AUTO_ACKNOWLEDGE);
> Topic topic = session.createTopic("TestTopic");
> MessageProducer publisher = session.createProducer(topic);
> publisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
> connection.start();
> for(int x=0;x<5;x++){
> TextMessage message = session.createTextMessage("Test message
> "+x);
> System.out.println("Sending message: "+message.getText());
> publisher.send(message);
> }
> connection.stop();
> connection.close();
> System.out.println("Done.");
> }
> }
> [Message sent by forum member 'mikehake' (mikehake)]
>
> http://forums.java.net/jive/thread.jspa?messageID=254034
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>
>

--
View this message in context: http://old.nabble.com/MDB-that-receives-from-ActiveMQ-example-tp14848249...
Sent from the java.net - glassfish users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

anushree123
Offline
Joined: 2009-05-04

Hi i had initially followed the steps given in http://weblogs.java.net/blog/rampsarathy/archive/2007/03/glassfish_v2_an...
but got stuck up as i did not know how to create jndi bindings.
i did not find any resources online for the same.

this is what i ve done:
i deployed the ActiveMQ resource adapter
then created Admin Object Resource and a Physical Destination.
i created the MDB n then i am supposed to provide a deployment descriptor sun-ejb-jar.xml.
when i searched through glassfish folder i found 4 such files. which one am i supposed to edit??

also while writing a sample java class that posts messages to ActiveMQ:
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
....
i m gettin error in last import "package does not exist.".
is there something else i m supposed to do??
thanks in advance for any help...

Message was edited by: anushree123

Message was edited by: anushree123

rampsarathy
Offline
Joined: 2005-11-23

If using Active MQ RA is not a requirement for your assignment, you could use generic jms ra and follow the instructions here
http://weblogs.java.net/blog/rampsarathy/archive/2007/03/glassfish_v2_an...

-Ramesh

anushree123
Offline
Joined: 2009-05-04

Hi i had initially followed the steps given in http://weblogs.java.net/blog/rampsarathy/archive/2007/03/glassfish_v2_an...
but got stuck up as i did not know how to create jndi bindings.
i did not find any resources online for the same.
in the code snippet which u ve given there r lines:
import org.apache.activemq.*;
import org.apache.activemq.command.*;

for which i get the same error:"package does not exist"..

am i missin out on somethin??
thanks for ur help..

rampsarathy
Offline
Joined: 2005-11-23

you have to include active mq jar files in the classpath while compiling
-Ramesh

anushree123
Offline
Joined: 2009-05-04

Hi
i figured out the problem.
in netbeans, we have to add the jar/zip files to the project.so i added activemq-all-5.2.0.jar n all errors disappeared..
there is one more thing i m not getting. how do we provide sun-ejb-jar.xml deployment descriptor??
i m a newbie at this n it would be really helpful if u could point me to some tutorials online on this subject..
thanks for ur help..
Anushree

rampsarathy
Offline
Joined: 2005-11-23

Just ensure that the sun-ejb-jar.xml is present in the META-INF folder of your ejb jar file (along with ejb-jar.xml) , you could add it to the conf folder in netbeans

anushree123
Offline
Joined: 2009-05-04

Hi,
I m working on a similar task..
i m gettin error in lines
import org.apache.activemq.*; and
import org.apache.activemq.command.*;
the error is "package does not exist"
i m using activemq 5.2 with glassfish v2.

also i did not understand which sun-ejb-jar.xml file has to be modified..
could u point me to some example as i m a newbie and m gettin confused
thanks in advance for ur help..

Message was edited by: anushree123

rampsarathy
Offline
Joined: 2005-11-23

could you please elaborate
if you are trying to integrate GlassFish with Active MQ, are you following this document
http://weblogs.java.net/blog/rampsarathy/archive/2007/03/glassfish_v2_an...

janchj
Offline
Joined: 2008-12-03

Very nice example - Thanks!

Is it possible to post to the ActiveMQ queue/topic through Glassfish? I tried it using:
---------------------------------------------------------------------------------------
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.enterprise.naming.SerialInitContextFactory");
env.put("org.omg.CORBA.ORBInitialPort", "3700");
env.put("org.omg.CORBA.ORBInitialHost", "localhost");

env.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
env.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");

Context ic = new InitialContext(env);

connectionFactory = (ConnectionFactory)(ic.lookup("jms/ActiveFactory"));

Connection connection = connectionFactory.createConnection();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

queue = (Queue) ic.lookup("jms/ActiveQueue");
System.out.println("Queue = " + queue);
---------------------------------------------------------------------------------

I received the following:

Queue = queue://null

java.lang.NullPointerException
at org.apache.activemq.command.ActiveMQDestination.hashCode(ActiveMQDestination.java:296)
at java.util.HashMap.get(HashMap.java:300)
at org.apache.activemq.openwire.OpenWireFormat.getMarshallCacheIndex(OpenWireFormat.java:481)
at org.apache.activemq.openwire.v2.BaseDataStreamMarshaller.tightMarshalCachedObject1(BaseDataStreamMarshaller.java:145)
at org.apache.activemq.openwire.v2.ProducerInfoMarshaller.tightMarshal1(ProducerInfoMarshaller.java:97)
at org.apache.activemq.openwire.OpenWireFormat.marshal(OpenWireFormat.java:228)
at org.apache.activemq.transport.tcp.TcpTransport.oneway(TcpTransport.java:118)
at org.apache.activemq.transport.InactivityMonitor.oneway(InactivityMonitor.java:141)
at org.apache.activemq.transport.TransportFilter.oneway(TransportFilter.java:80)
at org.apache.activemq.transport.WireFormatNegotiator.oneway(WireFormatNegotiator.java:93)
at org.apache.activemq.transport.MutexTransport.oneway(MutexTransport.java:43)
at org.apache.activemq.transport.ResponseCorrelator.oneway(ResponseCorrelator.java:60)
at org.apache.activemq.ActiveMQConnection.asyncSendPacket(ActiveMQConnection.java:1151)
at org.apache.activemq.ActiveMQSession.asyncSendPacket(ActiveMQSession.java:1648)
at org.apache.activemq.ActiveMQMessageProducer.(ActiveMQMessageProducer.java:100)
at org.apache.activemq.ActiveMQSession.createProducer(ActiveMQSession.java:776)
at org.apache.activemq.ra.ManagedSessionProxy.createProducer(ManagedSessionProxy.java:225)
at sender.main(sender.java:66)

Any suggestions?

rampsarathy
Offline
Joined: 2005-11-23

Hi,
You could use generic jms resource adapter as shown here
http://weblogs.java.net/blog/rampsarathy/archive/2007/03/glassfish_v2_an...
Follow the steps, and see the code for the MDB
(http://weblogs.java.net/blog/rampsarathy/archive/ActiveMQTestBean.java)
that sends a message to the Active MQ queue

-Ramesh

gregorz
Offline
Joined: 2009-01-29

Yes, you need to set the property of the topic/queue in the glassfish admin console as follows

PhysicalName =

The default JMS provider uses Name = , which you probably already have there.