TECHNICALFACILITATION.COM - THE ULTIMATE WEBSPHERE AND JAVA CERTIFICATION GUIDES AND RESOURCES
Google
Download the Completed Solution: 14mdbtesting.ear.

Scroll to the bottom of the page for pertinent code snippets.

Doing Some JMS Programming with MDBs and MDB Clients

This free, multimedia tutorial shows you how to use IBM's Rational Application Developer (IRAD) 6.0 to create and test a J2EE Message Driven Bean (MDB). We will also use a Stateless Session EJB to place a message on a JMS Queue/Topic, all of which was setup in the tutorial that deals with configuring JMS messaging with WebSphere.

By the way, queue is a word with four vowels in a row. How many others can you name?

If you found something helpful here, please do your part and help support the site. Link to us, buy some books, support our sponsors, tell your developer friends about us, and remember: Happy Java!

/*This SLSB places a message on a message queue, which is then consumed by the MDB*/


package com.examscam.ejb;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

/**
* Bean implementation class for Enterprise Bean: MagicMessageMaker
*/
public class MagicMessageMakerBean implements javax.ejb.SessionBean {

public void dropOffMessage() throws Exception
{
try
{
String magicString = "The magic number is: " + System.currentTimeMillis()%100;

InitialContext context = new InitialContext();
ConnectionFactory cf = (ConnectionFactory)context.lookup("jms/examscamcf");
Connection conn = cf.createConnection();
Session jmsSession = conn.createSession(false,Session.AUTO_ACKNOWLEDGE);

Destination destination = (Destination)context.lookup("jms/examscamqueue");

//session requires the destination to create the producer
MessageProducer producer = jmsSession.createProducer(destination);

TextMessage message = jmsSession.createTextMessage();
message.setText(magicString);
producer.send(message);

producer.close();
jmsSession.close();
conn.close();
}

catch(Exception e)
{
System.out.println(e.getClass() + e.getMessage());
}
}








private javax.ejb.SessionContext mySessionCtx;
/**
* getSessionContext
*/
public javax.ejb.SessionContext getSessionContext() {
return mySessionCtx;
}
/**
* setSessionContext
*/
public void setSessionContext(javax.ejb.SessionContext ctx) {
mySessionCtx = ctx;
}
/**
* ejbCreate
*/
public void ejbCreate() throws javax.ejb.CreateException {
}
/**
* ejbActivate
*/
public void ejbActivate() {
}
/**
* ejbPassivate
*/
public void ejbPassivate() {
}
/**
* ejbRemove
*/
public void ejbRemove() {
}
}

package com.examscam.ejb;
/**
* Home interface for Enterprise Bean: MagicMessageMaker
*/
public interface MagicMessageMakerHome extends javax.ejb.EJBHome {
/**
* Creates a default instance of Session Bean: MagicMessageMaker
*/
public com.examscam.ejb.MagicMessageMaker create()
throws javax.ejb.CreateException,
java.rmi.RemoteException;
}



package com.examscam.ejb;
/**
* Remote interface for Enterprise Bean: MagicMessageMaker
*/
public interface MagicMessageMaker extends javax.ejb.EJBObject {
public void dropOffMessage() throws Exception, java.rmi.RemoteException;



}
package com.examscam.ejb;

import javax.jms.JMSException;

/**
* Bean implementation class for Enterprise Bean: MagicMDB
*/
public class MagicMDBBean implements javax.ejb.MessageDrivenBean,
javax.jms.MessageListener {
private javax.ejb.MessageDrivenContext fMessageDrivenCtx;

/**
* getMessageDrivenContext
*/
public javax.ejb.MessageDrivenContext getMessageDrivenContext() {
return fMessageDrivenCtx;
}

/**
* setMessageDrivenContext
*/
public void setMessageDrivenContext(javax.ejb.MessageDrivenContext ctx) {
fMessageDrivenCtx = ctx;
}

/**
* ejbCreate
*/
public void ejbCreate() {
}

public void onMessage(javax.jms.Message message) {
/* cast the generic message into a TextMessage */
javax.jms.TextMessage textMessage = (javax.jms.TextMessage) message;
try {
System.out.println("MDB has obtained the following message: "
+ textMessage.getText());
} catch (JMSException e) {
System.out.println(e.getClass() + e.getMessage());
}

}


/**
* ejbRemove
*/
public void ejbRemove() {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" 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">
<display-name>
ExamScamEJB</display-name>
<enterprise-beans>
<session id="StatefulTimer">
<ejb-name>StatefulTimer</ejb-name>
<home>com.examscam.ejb.StatefulTimerHome</home>
<remote>com.examscam.ejb.StatefulTimerRemote</remote>
<ejb-class>com.examscam.ejb.StatefulTimerBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
<session id="StatelessTimer">
<ejb-name>StatelessTimer</ejb-name>
<home>com.examscam.ejb.StatelessTimerHome</home>
<remote>com.examscam.ejb.StatelessTimerRemote</remote>
<ejb-class>com.examscam.ejb.StatelessTimerBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
<entity id="PersonBMP">
<ejb-name>PersonBMP</ejb-name>
<local-home>com.examscam.ejb.PersonBMPLocalHome</local-home>
<local>com.examscam.ejb.PersonBMPLocal</local>
<ejb-class>com.examscam.ejb.PersonBMPBean</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>com.examscam.ejb.PersonBMPKey</prim-key-class>
<reentrant>false</reentrant>
</entity>
<message-driven id="MagicMDB">
<ejb-name>MagicMDB</ejb-name>
<ejb-class>com.examscam.ejb.MagicMDBBean</ejb-class>
<messaging-type>javax.jms.MessageListener</messaging-type>
<transaction-type>Container</transaction-type>
<message-destination-type>javax.jms.Queue</message-destination-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>
</message-driven>
<session id="MagicMessageMaker">
<ejb-name>MagicMessageMaker</ejb-name>
<home>com.examscam.ejb.MagicMessageMakerHome</home>
<remote>com.examscam.ejb.MagicMessageMaker</remote>
<local-home>com.examscam.ejb.MagicMessageMakerLocalHome</local-home>
<local>com.examscam.ejb.MagicMessageMakerLocal</local>
<ejb-class>com.examscam.ejb.MagicMessageMakerBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<ejb-client-jar>ExamScamEJBClient.jar</ejb-client-jar>
</ejb-jar>
s
Google
THE ULTIMATE CERTIFICATION AND WEBSPHERE RESOURCES - BUY THEM NOW ON AMAZON
eXTReMe Tracker