001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 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: Sender1_1SF.java 5995 2004-12-17 15:08:36Z joaninh $
023: * --------------------------------------------------------------------------
024: */
025:
026: // SenderSF1_1.java
027: // Stateful Session Bean
028: package org.objectweb.jonas.jtests.beans.message;
029:
030: import java.rmi.RemoteException;
031: import java.util.Collection;
032: import java.util.Iterator;
033:
034: import javax.ejb.CreateException;
035: import javax.ejb.EJBException;
036: import javax.ejb.FinderException;
037: import javax.ejb.SessionBean;
038: import javax.ejb.SessionContext;
039: import javax.jms.ConnectionFactory;
040: import javax.jms.Destination;
041: import javax.jms.JMSException;
042: import javax.jms.MapMessage;
043: import javax.jms.MessageProducer;
044: import javax.jms.Session;
045: import javax.naming.InitialContext;
046: import javax.naming.NamingException;
047: import javax.rmi.PortableRemoteObject;
048:
049: import org.objectweb.jonas.common.Log;
050: import org.objectweb.jonas.jtests.util.Env;
051: import org.objectweb.util.monolog.api.BasicLevel;
052: import org.objectweb.util.monolog.api.Logger;
053:
054: /**
055: * This Session Bean is equivalent to the SenderF bean
056: * the only difference is it is written in JMS1.1
057: * it is using ConnectionFactory, Connection, Session and MessageProducer
058: */
059: public class Sender1_1SF implements SessionBean {
060:
061: static protected Logger logger = null;
062: SessionContext ejbContext;
063: private transient InitialContext ictx = null;
064: private transient ConnectionFactory cf = null;
065: private transient javax.jms.Connection c = null;
066: private transient MRecordHome ehome = null;
067: private static int count = 1;
068:
069: // ------------------------------------------------------------------
070: // SessionBean implementation
071: // ------------------------------------------------------------------
072:
073: /**
074: * Set the associated session context. The container calls this method
075: * after the instance creation.
076: * The enterprise Bean instance should store the reference to the context
077: * object in an instance variable.
078: * This method is called with no transaction context.
079: *
080: * @param sessionContext A SessionContext interface for the instance.
081: * @throws EJBException Thrown by the method to indicate a failure caused by
082: * a system-level error.
083: */
084: public void setSessionContext(SessionContext ctx) {
085: if (logger == null)
086: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
087: logger.log(BasicLevel.DEBUG, "");
088: ejbContext = ctx;
089: }
090:
091: /**
092: * A container invokes this method before it ends the life of the session object.
093: * This happens as a result of a client's invoking a remove operation, or when a
094: * container decides to terminate the session object after a timeout.
095: * This method is called with no transaction context.
096: *
097: * @throws EJBException Thrown by the method to indicate a failure caused by
098: * a system-level error.
099: */
100: public void ejbRemove() {
101: logger.log(BasicLevel.DEBUG, "");
102: try {
103: c.close();
104: } catch (Exception e) {
105: logger.log(BasicLevel.ERROR, "Exception on close:" + e);
106: }
107: }
108:
109: /**
110: * The Session bean must define 1 or more ejbCreate methods.
111: *
112: * @throws CreateException Failure to create a session EJB object.
113: */
114: public void ejbCreate() throws CreateException {
115: logger.log(BasicLevel.DEBUG, "");
116: // Lookup Connection Factories
117: try {
118: ictx = new InitialContext();
119: cf = (ConnectionFactory) ictx.lookup("CF");
120: } catch (NamingException e) {
121: logger.log(BasicLevel.ERROR,
122: "SenderSF : Cannot lookup Connection Factories: "
123: + e);
124: throw new CreateException(
125: "SenderSF: Cannot lookup Connection Factories");
126: }
127:
128: // Create Connections
129: try {
130:
131: c = cf.createConnection();
132: } catch (JMSException e) {
133: logger.log(BasicLevel.ERROR,
134: "SenderSF : Cannot create connections: " + e);
135: throw new CreateException(
136: "SenderSF: Cannot create connections");
137: }
138:
139: // Lookup Entity Home for checking
140: String BEAN_HOME = "messageMRecordECHome";
141: try {
142: ehome = (MRecordHome) PortableRemoteObject.narrow(ictx
143: .lookup(BEAN_HOME), MRecordHome.class);
144: } catch (NamingException e) {
145: logger.log(BasicLevel.ERROR,
146: "SenderSF ejbCreate: Cannot get entity home: " + e);
147: throw new CreateException(
148: "SenderSF: Cannot get entity home");
149: }
150: }
151:
152: /**
153: * A container invokes this method on an instance before the instance
154: * becomes disassociated with a specific EJB object.
155: */
156: public void ejbPassivate() {
157: logger.log(BasicLevel.DEBUG, "");
158: }
159:
160: /**
161: * A container invokes this method when the instance is taken out of
162: * the pool of available instances to become associated with a specific
163: * EJB object.
164: */
165: public void ejbActivate() {
166: logger.log(BasicLevel.DEBUG, "");
167: }
168:
169: // ------------------------------------------------------------------
170: // private methods
171: // ------------------------------------------------------------------
172:
173: /**
174: * return a unique identifier
175: */
176: private String getUUID() {
177: long uuid;
178: synchronized (getClass()) {
179: uuid = System.currentTimeMillis() * 256 + count;
180: count++;
181: }
182: return String.valueOf(uuid);
183: }
184:
185: // ------------------------------------------------------------------
186: // Sender implementation
187: // ------------------------------------------------------------------
188:
189: /**
190: * send a message on destination (topic or queue)
191: * @param String destination
192: * @param int value set in message
193: * @param int nb of messages sent
194: */
195: public void sendOnDestination(String dst, int val, int nb) {
196: logger.log(BasicLevel.DEBUG, "");
197: // Lookup destinations
198: Destination dest = null;
199: try {
200: dest = (Destination) ictx.lookup(dst);
201: } catch (NamingException e) {
202: throw new EJBException("sendOnDestination: Cannot lookup "
203: + dest);
204: }
205:
206: // Create Session
207: // Create Session at each request : Avoids the bug in JMS
208: // about Session not enlisted in transactions if open first.
209: Session ss = null;
210: try {
211: // (true, 0) are the recommanded args, although they are not taken
212: // in account by the container.
213: ss = c.createSession(true, 0);
214: } catch (JMSException e) {
215: throw new EJBException("Cannot create Session: " + e);
216: }
217:
218: // Create the MessageProducer for topic
219: MessageProducer producer = null;
220: try {
221: producer = ss.createProducer(dest);
222: } catch (JMSException e) {
223: throw new EJBException("Cannot create MessageProducer: "
224: + e);
225: }
226:
227: // Send messages on the destination
228: try {
229: for (int i = 0; i < nb; i++) {
230: MapMessage mess = ss.createMapMessage();
231: mess.setString("Id", getUUID());
232: mess.setString("Text", dst);
233: mess.setInt("Value", val);
234: producer.send(mess);
235: }
236: } catch (JMSException e) {
237: throw new EJBException("Cannot send message: " + e);
238: }
239:
240: // Close Session: This is mandatory for the correct behaviour of
241: // XA protocol. An XA END must be sent before commit or rollback.
242: try {
243: ss.close();
244: } catch (JMSException e) {
245: throw new EJBException("Cannot close session: " + e);
246: }
247: }
248:
249: /**
250: * send messages on destination (topic or queue) (transacted)
251: * @param String destination
252: * @param int value set in message
253: * @param int nb of messages sent
254: */
255: public void sendOnDestinationTx(String dest, int val, int nb) {
256: sendOnDestination(dest, val, nb);
257: }
258:
259: /**
260: * Checking send methods
261: * @param int value looked in messages received
262: * @param int nb of messages that could be received
263: * @param int nb of seconds max to wait for all messages
264: * @return actual nb of messages received
265: */
266: public int check(int val, int nb, int sec) {
267: Collection elist = null;
268: int retval = 0;
269: for (int i = 0; i <= sec; i++) {
270: logger.log(BasicLevel.DEBUG, "sec : " + i + "/" + sec);
271: try {
272: elist = ehome.findByValue(val);
273: retval = elist.size();
274: if (retval >= nb) {
275: // clean database before returning
276: Iterator it = elist.iterator();
277: while (it.hasNext()) {
278: MRecord ent = (MRecord) PortableRemoteObject
279: .narrow(it.next(), MRecord.class);
280: try {
281: ent.remove();
282: } catch (Exception e) {
283: throw new EJBException("Error on remove");
284: }
285: }
286: return retval;
287: }
288: } catch (FinderException e) {
289: } catch (RemoteException e) {
290: return retval;
291: }
292: try {
293: Thread.sleep(1000);
294: } catch (InterruptedException e) {
295: }
296: }
297: return retval;
298: }
299:
300: /**
301: * Clean all entity beans for this value
302: */
303: public void clean(int val) {
304: logger.log(BasicLevel.DEBUG, "");
305: Collection elist = null;
306: try {
307: elist = ehome.findByValue(val);
308: } catch (FinderException e) {
309: return;
310: } catch (Exception e) {
311: throw new EJBException("Error on find");
312: }
313: Iterator it = elist.iterator();
314: while (it.hasNext()) {
315: MRecord ent = (MRecord) PortableRemoteObject.narrow(it
316: .next(), MRecord.class);
317: try {
318: ent.remove();
319: } catch (Exception e) {
320: throw new EJBException("Error on remove");
321: }
322: }
323: }
324:
325: }
|