KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > message > Sender1_1SF


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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: Sender1_1SF.java,v 1.7 2004/12/17 15:08:35 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 // SenderSF1_1.java
27
// Stateful Session Bean
28

29 package org.objectweb.jonas.jtests.beans.message;
30
31 import java.rmi.RemoteException JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import javax.ejb.CreateException JavaDoc;
36 import javax.ejb.EJBException JavaDoc;
37 import javax.ejb.FinderException JavaDoc;
38 import javax.ejb.SessionBean JavaDoc;
39 import javax.ejb.SessionContext JavaDoc;
40 import javax.jms.ConnectionFactory JavaDoc;
41 import javax.jms.Destination JavaDoc;
42 import javax.jms.JMSException JavaDoc;
43 import javax.jms.MapMessage JavaDoc;
44 import javax.jms.MessageProducer JavaDoc;
45 import javax.jms.Session JavaDoc;
46 import javax.naming.InitialContext JavaDoc;
47 import javax.naming.NamingException JavaDoc;
48 import javax.rmi.PortableRemoteObject JavaDoc;
49
50 import org.objectweb.jonas.common.Log;
51 import org.objectweb.jonas.jtests.util.Env;
52 import org.objectweb.util.monolog.api.BasicLevel;
53 import org.objectweb.util.monolog.api.Logger;
54
55 /**
56  * This Session Bean is equivalent to the SenderF bean
57  * the only difference is it is written in JMS1.1
58  * it is using ConnectionFactory, Connection, Session and MessageProducer
59  */

60 public class Sender1_1SF implements SessionBean JavaDoc {
61
62     static protected Logger logger = null;
63     SessionContext JavaDoc ejbContext;
64     private transient InitialContext JavaDoc ictx = null;
65     private transient ConnectionFactory JavaDoc cf = null;
66     private transient javax.jms.Connection JavaDoc c = null;
67     private transient MRecordHome ehome = null;
68     private static int count = 1;
69
70     // ------------------------------------------------------------------
71
// SessionBean implementation
72
// ------------------------------------------------------------------
73

74     /**
75      * Set the associated session context. The container calls this method
76      * after the instance creation.
77      * The enterprise Bean instance should store the reference to the context
78      * object in an instance variable.
79      * This method is called with no transaction context.
80      *
81      * @param sessionContext A SessionContext interface for the instance.
82      * @throws EJBException Thrown by the method to indicate a failure caused by
83      * a system-level error.
84      */

85     public void setSessionContext(SessionContext JavaDoc ctx) {
86         if (logger == null)
87             logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
88         logger.log(BasicLevel.DEBUG, "");
89         ejbContext = ctx;
90     }
91     
92     /**
93      * A container invokes this method before it ends the life of the session object.
94      * This happens as a result of a client's invoking a remove operation, or when a
95      * container decides to terminate the session object after a timeout.
96      * This method is called with no transaction context.
97      *
98      * @throws EJBException Thrown by the method to indicate a failure caused by
99      * a system-level error.
100      */

101     public void ejbRemove() {
102         logger.log(BasicLevel.DEBUG, "");
103         try {
104             c.close();
105         } catch (Exception JavaDoc e) {
106             logger.log(BasicLevel.ERROR, "Exception on close:" + e);
107         }
108     }
109     
110     /**
111      * The Session bean must define 1 or more ejbCreate methods.
112      *
113      * @throws CreateException Failure to create a session EJB object.
114      */

115     public void ejbCreate() throws CreateException JavaDoc {
116         logger.log(BasicLevel.DEBUG, "");
117         // Lookup Connection Factories
118
try {
119             ictx = new InitialContext JavaDoc();
120             cf = (ConnectionFactory JavaDoc) ictx.lookup("CF");
121         } catch (NamingException JavaDoc e) {
122             logger.log(BasicLevel.ERROR, "SenderSF : Cannot lookup Connection Factories: "+e);
123             throw new CreateException JavaDoc("SenderSF: Cannot lookup Connection Factories");
124         }
125
126         // Create Connections
127
try {
128
129             c = cf.createConnection();
130         } catch (JMSException JavaDoc e) {
131             logger.log(BasicLevel.ERROR, "SenderSF : Cannot create connections: "+e);
132             throw new CreateException JavaDoc("SenderSF: Cannot create connections");
133         }
134
135         // Lookup Entity Home for checking
136
String JavaDoc BEAN_HOME = "messageMRecordECHome";
137         try {
138             ehome = (MRecordHome) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME), MRecordHome.class);
139         } catch (NamingException JavaDoc e) {
140             logger.log(BasicLevel.ERROR, "SenderSF ejbCreate: Cannot get entity home: "+e);
141             throw new CreateException JavaDoc("SenderSF: Cannot get entity home");
142         }
143     }
144
145     /**
146      * A container invokes this method on an instance before the instance
147      * becomes disassociated with a specific EJB object.
148      */

149     public void ejbPassivate() {
150         logger.log(BasicLevel.DEBUG, "");
151     }
152
153     /**
154      * A container invokes this method when the instance is taken out of
155      * the pool of available instances to become associated with a specific
156      * EJB object.
157      */

158     public void ejbActivate() {
159         logger.log(BasicLevel.DEBUG, "");
160     }
161     
162     // ------------------------------------------------------------------
163
// private methods
164
// ------------------------------------------------------------------
165

166     /**
167      * return a unique identifier
168      */

169     private String JavaDoc getUUID() {
170         long uuid;
171         synchronized(getClass()) {
172             uuid = System.currentTimeMillis() * 256 + count;
173             count++;
174         }
175         return String.valueOf(uuid);
176     }
177
178     // ------------------------------------------------------------------
179
// Sender implementation
180
// ------------------------------------------------------------------
181

182     /**
183      * send a message on destination (topic or queue)
184      * @param String destination
185      * @param int value set in message
186      * @param int nb of messages sent
187      */

188     public void sendOnDestination(String JavaDoc dst, int val, int nb) {
189         logger.log(BasicLevel.DEBUG, "");
190         // Lookup destinations
191
Destination JavaDoc dest = null;
192         try {
193             dest = (Destination JavaDoc) ictx.lookup(dst);
194         } catch (NamingException JavaDoc e) {
195             throw new EJBException JavaDoc("sendOnDestination: Cannot lookup "+dest);
196         }
197
198         // Create Session
199
// Create Session at each request : Avoids the bug in JMS
200
// about Session not enlisted in transactions if open first.
201
Session JavaDoc ss = null;
202         try {
203             // (true, 0) are the recommanded args, although they are not taken
204
// in account by the container.
205
ss = c.createSession(true, 0);
206         } catch (JMSException JavaDoc e) {
207             throw new EJBException JavaDoc("Cannot create Session: "+e);
208         }
209
210         // Create the MessageProducer for topic
211
MessageProducer JavaDoc producer = null;
212         try {
213             producer = ss.createProducer(dest);
214         } catch (JMSException JavaDoc e) {
215             throw new EJBException JavaDoc("Cannot create MessageProducer: "+e);
216         }
217
218         // Send messages on the destination
219
try {
220             for (int i = 0; i < nb; i++) {
221                 MapMessage JavaDoc mess = ss.createMapMessage();
222                 mess.setString("Id", getUUID());
223                 mess.setString("Text",dst);
224                 mess.setInt("Value", val);
225                 producer.send(mess);
226             }
227         } catch (JMSException JavaDoc e) {
228             throw new EJBException JavaDoc("Cannot send message: "+e);
229         }
230
231         // Close Session: This is mandatory for the correct behaviour of
232
// XA protocol. An XA END must be sent before commit or rollback.
233
try {
234             ss.close();
235         } catch (JMSException JavaDoc e) {
236             throw new EJBException JavaDoc("Cannot close session: "+e);
237         }
238     }
239
240     /**
241      * send messages on destination (topic or queue) (transacted)
242      * @param String destination
243      * @param int value set in message
244      * @param int nb of messages sent
245      */

246     public void sendOnDestinationTx(String JavaDoc dest, int val, int nb) {
247         sendOnDestination(dest, val, nb);
248     }
249
250   
251
252     /**
253      * Checking send methods
254      * @param int value looked in messages received
255      * @param int nb of messages that could be received
256      * @param int nb of seconds max to wait for all messages
257      * @return actual nb of messages received
258      */

259     public int check(int val, int nb, int sec) {
260         Collection JavaDoc elist = null;
261         int retval = 0;
262         for (int i = 0; i <= sec; i++) {
263             logger.log(BasicLevel.DEBUG, "sec : " + i + "/" + sec);
264             try {
265                 elist = ehome.findByValue(val);
266                 retval = elist.size();
267                 if (retval >= nb) {
268                     // clean database before returning
269
Iterator JavaDoc it = elist.iterator();
270                     while (it.hasNext()) {
271                         MRecord ent = (MRecord) PortableRemoteObject.narrow(it.next(), MRecord.class);
272                         try {
273                             ent.remove();
274                         } catch (Exception JavaDoc e) {
275                             throw new EJBException JavaDoc("Error on remove");
276                         }
277                     }
278                     return retval;
279                 }
280             } catch (FinderException JavaDoc e) {
281             } catch (RemoteException JavaDoc e) {
282                 return retval;
283             }
284             try {
285                 Thread.sleep(1000);
286             } catch (InterruptedException JavaDoc e) {
287             }
288         }
289         return retval;
290     }
291
292     /**
293      * Clean all entity beans for this value
294      */

295     public void clean(int val) {
296         logger.log(BasicLevel.DEBUG, "");
297         Collection JavaDoc elist = null;
298         try {
299             elist = ehome.findByValue(val);
300         } catch (FinderException JavaDoc e) {
301             return;
302         } catch (Exception JavaDoc e) {
303             throw new EJBException JavaDoc("Error on find");
304         }
305         Iterator JavaDoc it = elist.iterator();
306         while (it.hasNext()) {
307             MRecord ent = (MRecord) PortableRemoteObject.narrow(it.next(), MRecord.class);
308             try {
309                 ent.remove();
310             } catch (Exception JavaDoc e) {
311                 throw new EJBException JavaDoc("Error on remove");
312             }
313         }
314     }
315     
316 }
317
Popular Tags