Sending ObjectMessage to MDB
ramas_srs Sep 16, 2002 9:02 AMHello,
I am trying to run the following JMS CLient program and the associated MDB in JBoss v3.0 EJB
server. The client program (GeoClient_MsgProducer.java) is able to send the messages to the
mesage queue, but, the MDB does not seem to act on the messages. I would appreciate any help
on this. Following are the code snippets. I can send the full code if required.
1. Client program : GeoClient_MsgProducer
import com.totalflood.poseidon.common.GeoCodeRequest ;
import javax.jms.* ;
import java.util.Date;
public class GeoClient_MsgProducer {
public static void main (String [] args) throws Exception {
GeoCodeRequest gReq = new GeoCodeRequest() ;
int count = 5 ;
Context jndiContext = getInitialContext ();
QueueConnectionFactory factory = (QueueConnectionFactory)jndiContext.lookup ("ConnectionFactory");
Queue msgQueue1 = (Queue)jndiContext.lookup ("queue/geoq-GeoCoder");
QueueConnection connect = factory.createQueueConnection ();
QueueSession session = connect.createQueueSession (false,Session.AUTO_ACKNOWLEDGE);
QueueSender sender = session.createSender (msgQueue1);
for (int i = 0; i < count; i++) {
gReq.setMsgSrc("GeoClient") ;
gReq.setReqId(1) ;
gReq.setCertNo(1) ;
gReq.setCltId(10010) ;
gReq.setPropAddr1("6033 West Century Boulevard") ;
gReq.setPropAddr2("Suite 1075") ;
gReq.setPropCity("Los Angeles") ;
gReq.setStateCd("CA") ;
gReq.setPropZip("90045") ;
System.out.println ("Sending message #"+i);
ObjectMessage tmpObjMsg = session.createObjectMessage() ;
tmpObjMsg.setObject(gReq) ;
sender.send ((Message)tmpObjMsg);
}
connect.close ();
}
public static Context getInitialContext ()
throws javax.naming.NamingException {
return new InitialContext ();
}
}
2. Message Driven Bean : GeoMsgProcessorBean
import com.totalflood.poseidon.common.GeoCodeRequest ;
import java.util.*;
import javax.jms.*;
import javax.naming.*;
import javax.ejb.* ;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
public class GeoMsgProcessorBean
implements javax.ejb.MessageDrivenBean, javax.jms.MessageListener {
MessageDrivenContext ejbContext;
Context jndiContext;
public void setMessageDrivenContext (MessageDrivenContext mdc) {
ejbContext = mdc;
try
{
jndiContext = new InitialContext ();
}
catch(NamingException ne) {
throw new EJBException (ne);
}
}
public void ejbCreate () {}
public void onMessage (Message message) {
try
{
System.out.println ("MsgProcessor::onMessage() called..");
String jmsQueue = "queue/geoq-JmsRcv" ;
String hveQueue = "queue/hveq-HVE" ;
GeoCodeRequest gReqRslt = new GeoCodeRequest() ;
ObjectMessage procObjMsg = (ObjectMessage)message;
/* Check the message source. If the message is from the "GeoClient" program,
then it is to be sent to the Jms client for GeoCoding. If the message is
from the Jms client then send the message to the next queue/module */
String msgSrc = procObjMsg.getStringProperty("MessageSource") ;
System.out.println("Message Source = "+msgSrc) ;
if (msgSrc.equals("GeoClient")) {
sendMsgToQueue(procObjMsg, jmsQueue) ;
}
if (msgSrc.equals("JmsClient")) {
gReqRslt = (GeoCodeRequest)procObjMsg.getObject() ;
// Print the geocoded details
String outputStrng1 = procObjMsg.getStringProperty("MessageContent") ;
String outputStrng2 = gReqRslt.getStdAddr1() ;
String outputStrng3 = gReqRslt.getStdAddr2() ;
String outputStrng4 = gReqRslt.getStdCity() ;
String outputStrng5 = gReqRslt.getStdStateCd() ;
String outputStrng6 = gReqRslt.getStdZip() ;
System.out.println ("********************************");
System.out.println (outputStrng2);
System.out.println (outputStrng3);
System.out.println (outputStrng4+", "+outputStrng5+" - "+outputStrng6);
System.out.println ("********************************");
//sendMsgToQueue(procObjMsg, hveQueue) ;
}
} catch(Exception e) {
throw new EJBException (e);
}
}
public void ejbRemove () {
try
{
jndiContext.close ();
ejbContext = null;
} catch(NamingException ignored) { }
}
}