Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

6 Steps in Creating MDB

6.1 Writing the bean class

package com.test.MDB;

import javax.ejb.*;
import javax.jms.*;

public class MessageBean implements MessageDrivenBean, MessageListener{

private static final long serialVersionUID = 1L;


protected MessageDrivenContext ctx;
public void ejbCreate()
{
System.out.println("ejbCreate()");
}
public void onMessage(Message inMessage)
{
TextMessage txtmsg=null;

try{
if(inMessage instanceof TextMessage)
{
txtmsg=(TextMessage)inMessage;
System.out.println ("Message received:"+txtmsg.getText ());
}
else
{
System.out.println ("Message of wrong
type:"+inMessage.getClass ().getName ());
}

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

}
public void ejbRemove()
{
System.out.println("ejbRemove()");
}
public void setMessageDrivenContext (MessageDrivenContext ctx) throws
EJBException {
this.ctx=ctx;
}
}

To compile the bean type the following in the workspace directory:


Javac -classpath C:\JBoss-4.0\client\
Jboss-j2ee.jar;
com/test/MDB/MessageBean.java

Explanation:

1) The MDB does not require any home interface or Remote interface class for its
implementation.
2) Message-driven beans process multiple JMS messages asynchronously, rather than
processing a serialized sequence of method calls.
3) Under the workspace directory , we can create the MDB class packaging under the
folder: com.test.MDB
4) As for any Enterprise bean we have to import javax.ejb.* API’s. The javax.jms.* API
provides classes and methods to implement messaging concept in java.( JMS Queue
or Topic)
5) The message-driven bean's onMessage () method performs all of the business logic
for the EJB. The application server calls onMessage() when the EJB's associated JMS
Queue or Topic receives a message, passing the full JMS message object as an
argument. It is the message-driven EJB's responsibility to parse the message and
perform the necessary business logic in onMessage()
6) As usual the MDB has ejbCreate (), ejbRemove () and setMessageDrivenContext ()
methods which are responsible for complete lifecycle of the instantiating the bean,
setting the environment variable context and destroying the bean.
7) The MDB implements the interface MessageDrivenBean which tells the application
server that the bean is a MDB which receives messages from the client.
8) Message-driven bean methods should not throw an application exception or a
RemoteException, even in onMessage().

6.2 writing the deployment descriptor

<?xml version="1.0" encoding="UTF-8"?>

<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee" version="2.1"


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">

<enterprise-beans>
<message-driven>
<ejb-name>Message</ejb-name>
<ejb-class>com.test.MDB.MessageBean</ejb-class>
<messaging-type>javax.jms.MessageListener</messaging-type>
<transaction-type>Container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Topic</destination-type>
<subscription-durability>NonDurable</subscription-durability>
</message-driven-destination>
<messaging-destination-type>javax.jms.Topic</messaging-destination-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-
name>
<activation-config-property-value>javax.jms.Topic</activation-config-property-
value>
</activation-config-property>
</activation-config>
</message-driven>
</enterprise-beans>
</ejb-jar>

Explanation:

The deployment descriptor is the same as for any other EJB deployment. The important
tags are:

(i) transaction-type: It specifies whether the message-driven bean manages its own
transactions or the container manages transactions on behalf of the bean.
(ii) Messaging-type: It defines the type of messaging service or listener used for
subscribing/consuming to the message
(iii) Destination-type: It specifies whether the MDB should be associated with the JMS
Queue or Topic destination
(iv) Subscription-durability: It specifies whether or not the associated Topic is
durable. A non durable subscription lasts for the life time of the subscriber object
while a durable subscription retain the state it was left in by the earlier subscriber

6.3 creating the deployment file and deploying the bean

To deploy the Ejb, we need to package all the class files and the xml file
(Deployment Descriptor) into a JAR file. To create the jar we have to type the fooling
command from the workspace directory:

Jar –cfv Message.jar com/test/MDB/*.class META-INF/ejb-jar.xml

To deploy the bean in JBOSS, we have to just copy the jar file from the workspace
directory to the deploy directory under JBoss install directory. . You don't even have
to stop and restart JBoss. If you copy this file into the deploy directory when JBoss is
running, if everything is fine, it will spit out this message

6.4 creating the client application and running it


import java.util.Properties;

import javax.jms.*;
import javax.naming.*;
import com.test.MDB.*;

public class MessageClient {

public static void main(String[] args) throws Exception {

Properties properties = new Properties ();


properties.put (Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
properties.put (Context.PROVIDER_URL, "jnp: //localhost:1099");

Context ctx = new InitialContext(properties);


System.out.println("got context");

Topic topic = (Topic) ctx.lookup("topic/Message");

TopicConnectionFactory factory = (TopicConnectionFactory) ctx


.lookup("java:/XAConnectionFactory");
TopicConnection connection = factory.createTopicConnection();
TopicSession session = connection.createTopicSession (false,
Session.AUTO_ACKNOWLEDGE);

TopicPublisher publisher=session.createPublisher(topic);

TextMessage msg = session.createTextMessage ();


msg.setText ("This is a test message to check the functionality of
Message driven bean");

publisher.publish(msg);
System.out.println ("Message sent successfully ");

}
}

To test whether your EJB is working, we have created the Client class. To compile
the client application, type the following from the workplace directory

javac -classpath C:\JBoss-4.0\client\jboss-


j2ee.jar;. MessageClient.java

To run the client application, type the following from the workplace directory

java -classpath C:\JBoss-4.0\client\jboss-


client.jar;C:\JBoss-4.0\client\jnp-client.jar;.
MessageClient

Explanation:

(i) The client application first sets the environment context variables such as
INITIAL_CONTEXT_FACTORY and PROVIDER_URL as constants which is
derived from the jboss application server xml files
(ii) Here we are using publish and subscribe messaging (pub/sub) via Topic.
Pub/sub is a push-based model, where consumers automatically receive
messages without them having to poll the topic for new messages.
(iii) Here a one producer can send messages to multiple consumers. The
destination to which a producer sends a message is called a topic.
(iv) TopicConnectionFactory are used as a factory for making TopicConnection
(v) The look up data for TopicConnectionFactory and Topic can be obtained from
the jboss xml for connections.
(vi) The TopicPublisher sends/publish the message to the topic for MDB to receive
or consume the message and print it on the console.

You might also like