1 22 package org.jboss.test.invokers.ejb; 23 24 import javax.ejb.EJBException ; 25 import javax.ejb.MessageDrivenBean ; 26 import javax.ejb.MessageDrivenContext ; 27 import javax.jms.JMSException ; 28 import javax.jms.Message ; 29 import javax.jms.MessageListener ; 30 import javax.jms.Queue ; 31 import javax.jms.QueueConnection ; 32 import javax.jms.QueueConnectionFactory ; 33 import javax.jms.QueueSender ; 34 import javax.jms.QueueSession ; 35 import javax.jms.ObjectMessage ; 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 39 import org.jboss.logging.Logger; 40 import org.jboss.test.invokers.interfaces.BusinessObjectLocal; 41 import org.jboss.test.invokers.interfaces.BusinessObjectLocalHome; 42 43 47 public class JMSGatewayMDB implements MessageDrivenBean , MessageListener 48 { 49 static Logger log = Logger.getLogger(JMSGatewayMDB.class); 50 private MessageDrivenContext ctx = null; 51 private QueueConnection queConn; 52 private QueueSession session; 53 private Context enc; 54 55 public void setMessageDrivenContext(MessageDrivenContext ctx) 56 { 57 this.ctx = ctx; 58 try 59 { 60 InitialContext iniCtx = new InitialContext (); 61 enc = (Context ) iniCtx.lookup("java:comp/env"); 62 QueueConnectionFactory factory = (QueueConnectionFactory ) enc.lookup("jms/ConnectionFactory"); 63 queConn = factory.createQueueConnection(); 64 session = queConn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 65 } 66 catch(Exception e) 67 { 68 log.error("Setup failure", e); 69 throw new EJBException ("Setup failure", e); 70 } 71 } 72 public void ejbCreate() 73 { 74 } 75 public void ejbRemove() throws EJBException 76 { 77 try 78 { 79 if( session != null ) 80 session.close(); 81 if( queConn != null ) 82 queConn.close(); 83 } 84 catch(Exception e) 85 { 86 log.error("Failed to close JMS resources", e); 87 } 88 } 89 90 94 public void onMessage(Message message) 95 { 96 log.info("onMessage, msg="+message); 97 try 98 { 99 ObjectMessage objMsg = (ObjectMessage ) message; 100 Queue replyTo = (Queue ) message.getJMSReplyTo(); 101 String ejbName = message.getStringProperty("ejbName"); 102 Object [] args = (Object []) objMsg.getObject(); 103 Object ref = enc.lookup("ejb/"+ejbName); 104 log.info("ejb/"+ejbName+" = "+ref); 105 BusinessObjectLocalHome home = (BusinessObjectLocalHome) ref; 106 BusinessObjectLocal bean = home.create(); 107 String reply = bean.doSomethingSlowly(args[0], (String ) args[1]); 108 reply = reply + "viaJMSGatewayMDB"; 109 sendReply(reply, replyTo); 110 } 111 catch(Exception e) 112 { 113 log.error("onMessage failure", e); 114 } 115 } 116 117 private void sendReply(String reply, Queue replyTo) throws JMSException 118 { 119 QueueSender sender = session.createSender(replyTo); 120 Message replyMsg = session.createObjectMessage(reply); 121 sender.send(replyMsg); 122 } 123 } 124 | Popular Tags |