/** * Copyright ? 2002 Sun Microsystems, Inc. All rights reserved. */ package com.sun.inout; import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.EJBException; import javax.ejb.MessageDrivenBean; import javax.ejb.MessageDrivenContext; import javax.ejb.CreateException; import javax.naming.*; import javax.jms.*; /** * A simple message driven bean. * The code illustrates the requirements of a message-driven bean class: *
MessageDrivenBean and MessageListener interfaces.
* public.
* abstract or final.
* onMessage method.
* ejbCreate method and one ejbRemove method.
* finalize method.
* onMessage
* method of the message-driven bean. In the ActiveMQTestBean class, the
* onMessage method casts the incoming message to a TextMessage and displays the text.
* @param inMessage incoming message.
*/
public void onMessage(Message inMessage) {
QueueConnection qconn = null;
try {
if (inMessage instanceof TextMessage) {
TextMessage txtmsg = (TextMessage)inMessage;
System.out.println("MESSAGE BEAN: Message received: index="+txtmsg.getText());
qconn = queueConnectionFactory.createQueueConnection();
QueueSession qss = qconn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender sender = qss.createSender(queue);
TextMessage msg = qss.createTextMessage();
msg.setText(txtmsg.getText());
sender.send(msg);
qconn.close();
} else {
System.out.println("WRONG MESSAGE TYPE received: " + inMessage.getClass().getName());
}
} catch (Throwable t) {
t.printStackTrace();
mdc.setRollbackOnly();
try {
if (qconn != null) qconn.close();
System.out.println("MESSAGE BEAN: Rollback: index="+((TextMessage)inMessage).getText());
} catch (Exception e) {
System.out.println("MESSAGE BEAN: Unable to get index property because:"+e.getMessage());
e.printStackTrace();
}
throw new RuntimeException(t.getMessage());
}
/* finally
{
try
{
qconn.close();
}
catch(Exception e)
{
;
}
}
*/
} // onMessage
/**
* Removes the bean. Required by EJB spec.
*/
public void ejbRemove() {
System.out.println("In ActiveMQTestBean.remove()");
}
} // class