1 22 package org.jboss.ejb3.test.jms.managed; 23 24 import javax.ejb.EJBException ; 25 import javax.ejb.SessionContext ; 26 import javax.jms.Connection ; 27 import javax.jms.ConnectionFactory ; 28 import javax.jms.JMSException ; 29 import javax.jms.MessageProducer ; 30 import javax.jms.Queue ; 31 import javax.jms.Session ; 32 import javax.jms.TextMessage ; 33 import javax.naming.InitialContext ; 34 35 import javax.ejb.Remove ; 36 import javax.ejb.Init ; 37 38 import org.jboss.ejb3.Container; 39 import org.jboss.logging.Logger; 40 41 public class JMSTestBean 42 implements JMSTest 43 { 44 45 private static Logger log = Logger.getLogger(JMSTestBean.class); 46 47 private Connection connection; 48 49 private Queue queue; 50 51 private SessionContext ctx; 52 53 public void setSessionContext(SessionContext sc) 54 { 55 ctx = sc; 56 } 57 58 @Init 59 public void ejbCreate() 60 { 61 try 62 { 63 InitialContext iniCtx = new InitialContext (); 64 65 ConnectionFactory cf = 66 (ConnectionFactory ) iniCtx.lookup(Container.ENC_CTX_NAME + "/env/jms/MyConnectionFactory"); 67 68 connection = cf.createConnection(); 69 connection.start(); 70 queue = (Queue )iniCtx.lookup(Container.ENC_CTX_NAME + "/env/jms/MyQueue"); 71 } 72 catch (Throwable t) 73 { 74 log.error(t); 75 } 76 } 77 78 79 public void test1() throws EJBException 80 { 81 Session session = null; 82 83 try 84 { 85 session = 86 connection.createSession(true, 0); 87 88 MessageProducer producer = session.createProducer(queue); 89 90 TextMessage message = session.createTextMessage(); 91 92 message.setText("Testing 123"); 93 94 producer.send(message); 95 } 96 catch (JMSException e) 97 { 98 log.error(e); 99 ctx.setRollbackOnly(); 100 throw new EJBException (e.toString()); 101 } 102 finally 103 { 104 try 105 { 106 if (session != null) session.close(); 107 } 108 catch (JMSException e) 109 { 110 } 111 } 112 } 113 114 @Remove 115 public void remove() 116 { 117 118 try 119 { 120 if (connection != null) 121 connection.close(); 122 } 123 catch (Exception e) 124 { 125 e.printStackTrace(); 126 } 127 128 } 129 } 130 | Popular Tags |