001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: EjbCompBean.java 4618 2004-04-19 06:39:30Z benoitf $
023: * --------------------------------------------------------------------------
024: */package jms;
025:
026: import javax.ejb.CreateException;
027: import javax.ejb.SessionBean;
028: import javax.ejb.SessionContext;
029: import javax.jms.ConnectionFactory;
030: import javax.jms.Destination;
031: import javax.jms.MessageProducer;
032: import javax.jms.ObjectMessage;
033: import javax.jms.Session;
034: import javax.jms.Topic;
035: import javax.naming.InitialContext;
036: import javax.naming.NamingException;
037:
038: /**
039: * EjbCompBean.java Stateful Session Bean sending and synchronously receiving
040: * JMS messages.
041: * @author Fran?ois Exertier Contributor(s):
042: */
043: public class EjbCompBean implements SessionBean {
044:
045: private SessionContext ejbContext;
046:
047: // JNDI will be used to get the JMS administered objects
048: private InitialContext ictx;
049:
050: private ConnectionFactory cf = null;
051:
052: private Topic topic = null;
053:
054: // ------------------------------------------------------------------
055: // SessionBean implementation
056: // ------------------------------------------------------------------
057:
058: public void setSessionContext(SessionContext ctx) {
059: ejbContext = ctx;
060: }
061:
062: public void ejbRemove() {
063: }
064:
065: public void ejbCreate() throws CreateException {
066:
067: // initialisation of JMS administered objects (obtained through JNDI)
068: // used by the bean
069: initJMSObjects();
070: }
071:
072: public void ejbPassivate() {
073: }
074:
075: public void ejbActivate() {
076: }
077:
078: // ------------------------------------------------------------------
079: // EjbComp implementation
080: // ------------------------------------------------------------------
081:
082: /**
083: * Send a JMS message
084: * @param s - the string that will be contained by the message.
085: */
086: public void sendMsg(java.lang.String s) {
087:
088: javax.jms.Connection tc = null;
089: Session session = null;
090: MessageProducer mp = null;
091:
092: System.out.println("Method sendMsg(" + s + ")");
093:
094: // create Connection, Session and MessageProducer
095: try {
096: tc = cf.createConnection();
097: session = tc.createSession(true, Session.AUTO_ACKNOWLEDGE);
098: mp = session.createProducer((Destination) topic);
099: } catch (Exception e) {
100: e.printStackTrace();
101: System.exit(2);
102: }
103:
104: // send the message to the topic
105: try {
106: ObjectMessage message;
107: message = session.createObjectMessage();
108: message.setObject(s);
109: mp.send(message);
110: session.close();
111: tc.close();
112: } catch (Exception e) {
113: e.printStackTrace();
114: System.exit(2);
115: }
116:
117: }
118:
119: // ------------------------------------------------------------------
120: // Internal methods
121: // ------------------------------------------------------------------
122:
123: /**
124: * Initialize the ConnectionFactory and Topic JMS objects that are used by
125: * the bean (through JNDI lookups).
126: */
127: private void initJMSObjects() throws CreateException {
128:
129: // Get InitialContext
130: try {
131: ictx = new InitialContext();
132: } catch (NamingException e) {
133: e.printStackTrace();
134: System.exit(2);
135: }
136: if (cf == null) {
137:
138: // Lookup the ConnectionFactory JMS object
139: // that will be used to send messages
140: try {
141: cf = (ConnectionFactory) ictx
142: .lookup("java:comp/env/jms/conFactSender");
143: System.out.println("EjbComp : ConnectionFactory = "
144: + 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
156: .lookup("java:comp/env/jms/topiclistener");
157: System.out
158: .println("EjbComp : Topic (jms/topiclistener) = "
159: + topic.toString());
160: } catch (javax.naming.NameNotFoundException ne) {
161: throw new CreateException(ne.toString());
162: } catch (Exception e) {
163: e.printStackTrace();
164: throw new CreateException();
165: }
166: }
167:
168: }
169: }
|