KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jms > EjbCompBean


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EjbCompBean.java,v 1.9 2004/04/19 06:39:29 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

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 /**
41  * EjbCompBean.java Stateful Session Bean sending and synchronously receiving
42  * JMS messages.
43  * @author Fran?ois Exertier Contributor(s):
44  */

45 public class EjbCompBean implements SessionBean {
46
47     private SessionContext ejbContext;
48
49     // JNDI will be used to get the JMS administered objects
50
private InitialContext ictx;
51
52     private ConnectionFactory cf = null;
53
54     private Topic topic = null;
55
56     // ------------------------------------------------------------------
57
// SessionBean implementation
58
// ------------------------------------------------------------------
59

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         // initialisation of JMS administered objects (obtained through JNDI)
70
// used by the bean
71
initJMSObjects();
72     }
73
74     public void ejbPassivate() {
75     }
76
77     public void ejbActivate() {
78     }
79
80     // ------------------------------------------------------------------
81
// EjbComp implementation
82
// ------------------------------------------------------------------
83

84     /**
85      * Send a JMS message
86      * @param s - the string that will be contained by the message.
87      */

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         // create Connection, Session and MessageProducer
97
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         // send the message to the topic
107
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     // ------------------------------------------------------------------
122
// Internal methods
123
// ------------------------------------------------------------------
124

125     /**
126      * Initialize the ConnectionFactory and Topic JMS objects that are used by
127      * the bean (through JNDI lookups).
128      */

129     private void initJMSObjects() throws CreateException {
130
131         // Get InitialContext
132
try {
133             ictx = new InitialContext();
134         } catch (NamingException e) {
135             e.printStackTrace();
136             System.exit(2);
137         }
138         if (cf == null) {
139
140             // Lookup the ConnectionFactory JMS object
141
// that will be used to send messages
142
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             // Lookup the topic, the JMS Topic object on which messages
153
// will be sent.
154
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