1 22 package org.jboss.test.security.ejb; 23 24 import javax.ejb.MessageDrivenBean ; 25 import javax.ejb.MessageDrivenContext ; 26 import javax.ejb.EJBException ; 27 import javax.jms.MessageListener ; 28 import javax.jms.Message ; 29 import javax.jms.Queue ; 30 import javax.jms.QueueConnection ; 31 import javax.jms.QueueConnectionFactory ; 32 import javax.jms.QueueReceiver ; 33 import javax.jms.QueueSender ; 34 import javax.jms.QueueSession ; 35 import javax.jms.Session ; 36 import javax.naming.InitialContext ; 37 import javax.naming.NamingException ; 38 39 import org.jboss.test.security.interfaces.Entity; 40 import org.jboss.test.security.interfaces.EntityHome; 41 42 49 public class RunAsMDB implements MessageDrivenBean , MessageListener 50 { 51 org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass()); 52 53 private MessageDrivenContext ctx = null; 54 private InitialContext iniCtx; 55 56 public RunAsMDB() 57 { 58 } 59 60 public void setMessageDrivenContext(MessageDrivenContext ctx) 61 throws EJBException 62 { 63 this.ctx = ctx; 64 try 65 { 66 iniCtx = new InitialContext (); 67 } 68 catch(NamingException e) 69 { 70 throw new EJBException (e); 71 } 72 } 73 74 public void ejbCreate() 75 { 76 } 77 78 public void ejbRemove() 79 { 80 ctx = null; 81 } 82 83 public void onMessage(Message message) 84 { 85 Queue replyTo = null; 86 try 87 { 88 replyTo = (Queue ) message.getJMSReplyTo(); 89 String arg = message.getStringProperty("arg"); 90 EntityHome home = (EntityHome) iniCtx.lookup("java:comp/env/ejb/Entity"); 91 Entity bean = home.findByPrimaryKey(arg); 92 String echo = bean.echo(arg); 93 log.info("RunAsMDB echo("+arg+") -> "+echo); 94 sendReply(replyTo, arg); 95 } 96 catch(Throwable e) 97 { 98 log.debug("failed", e); 99 if( replyTo != null ) 100 sendReply(replyTo, "Failed, ex="+e.getMessage()); 101 } 102 } 103 private void sendReply(Queue replyTo, String info) 104 { 105 try 106 { 107 InitialContext ctx = new InitialContext (); 108 QueueConnectionFactory queueFactory = (QueueConnectionFactory ) ctx.lookup("java:comp/env/jms/QueFactory"); 109 QueueConnection queueConn = queueFactory.createQueueConnection(); 110 QueueSession session = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 111 Message msg = session.createMessage(); 112 msg.setStringProperty("reply", info); 113 QueueSender sender = session.createSender(replyTo); 114 sender.send(msg); 115 sender.close(); 116 session.close(); 117 queueConn.close(); 118 log.info("Sent reply"); 119 } 120 catch(Exception e) 121 { 122 log.error("Failed to send reply", e); 123 } 124 } 125 } 126 | Popular Tags |