/** * 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: * * * Unlike session and entity beans, message-driven beans do not have the remote or local * interfaces that define client access. Client components do not locate message-driven beans * and invoke methods on them. Although message-driven beans do not have business methods, * they may contain helper methods that are invoked internally by the onMessage method. * */ public class ActiveMQTestBean implements MessageDrivenBean, MessageListener { private transient MessageDrivenContext mdc = null; private Context context; Queue queue = null; QueueConnectionFactory queueConnectionFactory = null; /** * Default constructor. Creates a bean. Required by EJB spec. */ public ActiveMQTestBean() { System.out.println("In ActiveMQTestBean.ActiveMQTestBean()"); } /** * Sets the context for the bean. * @param mdc the message-driven-bean context. */ public void setMessageDrivenContext(MessageDrivenContext mdc) { System.out.println("In " + "ActiveMQTestBean.setMessageDrivenContext()"); this.mdc = mdc; } /** * Creates a bean. Required by EJB spec. */ public void ejbCreate() { System.out.println("In ActiveMQTestBean.ejbCreate()"); try { Context jndiContext = new InitialContext(); queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("java:comp/env/jms/outboundXAQCF"); queue = (Queue)jndiContext.lookup("jms/outqueue"); } catch (Exception e) { System.out.println("MESSAGEBEAN.ejbcreate() got Exception "+ e.getMessage()); e.printStackTrace(); } } /** * When the queue receives a message, the EJB container invokes the 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