Re: JMS How to send and receive a message
So, you have created a JMS server and a topic?
The code for a listner is pretty standard, so I have included one of
mine for an example.
package com.qwest.rx.otto;
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
/**
* A message-driven bean based on a Queue.
*
* @ejb.bean name="RxWfaEjb" acknowledge-mode="Auto-acknowledge"
destination-type="javax.jms.Queue"
* subscription-durability="Durable"
transaction-type="Container"
*
* @ejb.transaction type="NotSupported"
*
* @weblogic.pool initial-beans-in-free-pool="2"
max-beans-in-free-pool="4"
*
* @weblogic.message-driven
connection-factory-jndi-name="rx.otto.jms.connectionFactory"
*
destination-jndi-name="rx.otto.wfa.jms.queue"
jms-polling-interval-seconds="1"
*
* @weblogic.transaction-descriptor trans-timeout-seconds="10"
*
* @weblogic.enable-call-by-reference True
*/
public class RxWfaEjb implements MessageDrivenBean, MessageListener
{
protected static Logger logger = Logger.getLogger("OttoLogger");
public void ejbRemove() throws EJBException
{
// TODO Auto-generated method stub
}
public void setMessageDrivenContext(MessageDrivenContext arg0)
throws EJBException
{
// TODO Auto-generated method stub
}
public void onMessage(Message message)
{
logger.debug("Recieved Message From Queue:" + message);
MapMessage msg = (MapMessage) message;
if (msg == null)
return;
RxWfaWorker worker = null;
String ticketId = "";
String status = "";
try
{
ticketId = msg.getStringProperty("ticketId");
status = msg.getStringProperty("status");
logger.debug("-< RxOtto >- [ RxWfaEjb ] onMessage (message)
\t=> " + "Working ticket: " + ticketId
+ " status: " + status);
worker = new RxWfaWorker();
worker.ManageTicketStatus(ticketId, status);
}
catch (HibernateException e)
{
logger.error("-< RxOtto >- [ RxWfaEjb ] onMessage (message)
\t=> "
+ "Exception encountered while handling some commit
of ticket and status.", e);
}
catch (JMSException e)
{
logger.error("-< RxOtto >- [ RxWfaEjb ] onMessage (message)
\t=> "
+ "JMS Excpetion encountered while attempting to
consume message.", e);
}
}
/**
* Required creation method for message-driven beans.
*
* @ejb.create-method
*/
public void ejbCreate()
{
}
}
Ryan
sss wrote:
I figure out how to send a message, but I haven't found out
the how to specify the receiver. I have read some article
saying for PTP messaging, the receiver doesn't need to be
online. But how do you know which user to send when no
body is online?
SK