Monday, March 28, 2011

putting object to MQ and read objects from EJB Messaging Bean

Object putting java code

========================

QueueConnection queueConnection = null;

QueueSession jmsSession = null;



try {

Hashtable hashtable=new Hashtable();

hashtable.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");

hashtable.put("java.naming.provider.url","jnp://localhost:1099");



QueueConnection queueConnection =MessagingServiceMDBUtil.getQueueConnection(hashtable);


jmsSession = queueConnection.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);

QueueSender sender = jmsSession.createSender(MessagingServiceMDBUtil.getQueue(LookupUtil.lookupMessagingConfig().getJndiProperties()));

// create the message

ObjectMessage message = jmsSession.createObjectMessage();

ArrayList msgList = new ArrayList();

//Object

Student studentObj=new Student();

studentObj.setFristName("Gayan");

studentObj.setLastName("Dissanayake");

msgList.add(studentObj);

message.setObject(msgList);



//send the message

sender.send(message);

sender.close();

log.debug("Message is written to jms queue");

} catch (JMSException e) {

log.error("Sending message via jms failed", e);

} catch (NamingException e) {

log.error("Sending message via jms failed", e);

} catch(ModuleException moduleException) {

log.error("Sending message via jms failed", moduleException);

} finally {

if (queueConnection != null){

try {

queueConnection.close();

log.debug("Closed jms queue connection");

} catch (JMSException e) {

log.error("Closing jms queue connection failed", e);

}

}

if (jmsSession != null){

try {

jmsSession.close();

if (log.isDebugEnabled()) log.debug("Closed jms session");

} catch (JMSException e) {

if (log.isDebugEnabled()) log.error("Closing jms session failed", e);

}

}

}


EJB Messaging Bean

===================


public class MessagingServiceMDB implements MessageDrivenBean, MessageListener {

private static final long serialVersionUID = -6583759698965963589L;

private final Log logger = LogFactory.getLog(getClass());



public void onMessage(Message message){

try {

logger.debug("Entered MessagingServiceMDB.onMessage()");

ObjectMessage objMessage = (ObjectMessage)message;

ArrayList msgProfiles = (ArrayList)objMessage.getObject();

//do business logic here using MQ object

logger.debug("Entered MessagingServiceMDB.onMessage()");

} catch (JMSException jmsException) {

logger.error("Exception in MessagingServiceMDB:onMessage() " + jmsException.getMessage(), jmsException);

} catch (Exception exception) {

logger.error("Exception in MessagingServiceMDB:onMessage() " + exception.getMessage(), exception);

}

}


public void setMessageDrivenContext(MessageDrivenContext cxt) throws EJBException {}

public void ejbRemove() throws EJBException {}

public void ejbCreate() throws EJBException {}

}


add following to ejb-jar.xml

============================


<message-driven >

<description><![CDATA[MessagingServiceMDB Message Driven Bean]]></description>

<display-name>MessagingServiceMDB</display-name>


<ejb-name>MessagingServiceMDB</ejb-name>


<ejb-class>com.isa.holidays.messaging.service.MessagingServiceMDB</ejb-class>


<transaction-type>Container</transaction-type>

<acknowledge-mode>Auto-acknowledge</acknowledge-mode>

<message-driven-destination>

<destination-type>javax.jms.Queue</destination-type>

</message-driven-destination>


<resource-ref >

<res-ref-name>jms/QCF</res-ref-name>

<res-type>javax.jms.QueueConnectionFactory</res-type>

<res-auth>Container</res-auth>

</resource-ref>


</message-driven>

No comments: