1 25 26 package jms; 27 28 import javax.ejb.CreateException; 29 import javax.ejb.SessionBean; 30 import javax.ejb.SessionContext; 31 import javax.jms.ConnectionFactory; 32 import javax.jms.Destination; 33 import javax.jms.MessageProducer; 34 import javax.jms.ObjectMessage; 35 import javax.jms.Session; 36 import javax.jms.Topic; 37 import javax.naming.InitialContext; 38 import javax.naming.NamingException; 39 40 45 public class EjbCompBean implements SessionBean { 46 47 private SessionContext ejbContext; 48 49 private InitialContext ictx; 51 52 private ConnectionFactory cf = null; 53 54 private Topic topic = null; 55 56 60 public void setSessionContext(SessionContext ctx) { 61 ejbContext = ctx; 62 } 63 64 public void ejbRemove() { 65 } 66 67 public void ejbCreate() throws CreateException { 68 69 initJMSObjects(); 72 } 73 74 public void ejbPassivate() { 75 } 76 77 public void ejbActivate() { 78 } 79 80 84 88 public void sendMsg(java.lang.String s) { 89 90 javax.jms.Connection tc = null; 91 Session session = null; 92 MessageProducer mp = null; 93 94 System.out.println("Method sendMsg(" + s + ")"); 95 96 try { 98 tc = cf.createConnection(); 99 session = tc.createSession(true, Session.AUTO_ACKNOWLEDGE); 100 mp = session.createProducer((Destination) topic); 101 } catch (Exception e) { 102 e.printStackTrace(); 103 System.exit(2); 104 } 105 106 try { 108 ObjectMessage message; 109 message = session.createObjectMessage(); 110 message.setObject(s); 111 mp.send(message); 112 session.close(); 113 tc.close(); 114 } catch (Exception e) { 115 e.printStackTrace(); 116 System.exit(2); 117 } 118 119 } 120 121 125 129 private void initJMSObjects() throws CreateException { 130 131 try { 133 ictx = new InitialContext(); 134 } catch (NamingException e) { 135 e.printStackTrace(); 136 System.exit(2); 137 } 138 if (cf == null) { 139 140 try { 143 cf = (ConnectionFactory) ictx.lookup("java:comp/env/jms/conFactSender"); 144 System.out.println("EjbComp : ConnectionFactory = " + cf.toString()); 145 } catch (javax.naming.NameNotFoundException ne) { 146 throw new CreateException(ne.toString()); 147 } catch (Exception e) { 148 e.printStackTrace(); 149 throw new CreateException(); 150 } 151 152 try { 155 topic = (Topic) ictx.lookup("java:comp/env/jms/topiclistener"); 156 System.out.println("EjbComp : Topic (jms/topiclistener) = " + topic.toString()); 157 } catch (javax.naming.NameNotFoundException ne) { 158 throw new CreateException(ne.toString()); 159 } catch (Exception e) { 160 e.printStackTrace(); 161 throw new CreateException(); 162 } 163 } 164 165 } 166 } | Popular Tags |