Professional Documents
Culture Documents
6.1 Writing The Bean Class: 6 Steps in Creating MDB
6.1 Writing The Bean Class: 6 Steps in Creating MDB
package com.test.MDB;
import javax.ejb.*;
import javax.jms.*;
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;
}
}
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().
<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
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:
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
import javax.jms.*;
import javax.naming.*;
import com.test.MDB.*;
TopicPublisher publisher=session.createPublisher(topic);
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
To run the client application, type the following from the workplace directory
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.