KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > message > MessageServer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.message;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.ejb.AbstractContext;
34 import com.caucho.ejb.AbstractServer;
35 import com.caucho.ejb.EjbServerManager;
36 import com.caucho.util.L10N;
37 import com.caucho.util.Log;
38
39 import javax.ejb.MessageDrivenBean JavaDoc;
40 import javax.ejb.MessageDrivenContext JavaDoc;
41 import javax.jms.*;
42 import java.lang.reflect.Method JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Server container for a message bean.
49  */

50 public class MessageServer extends AbstractServer {
51   private static final L10N L = new L10N(MessageServer.class);
52   protected static final Logger JavaDoc log = Log.open(MessageServer.class);
53
54   private Connection _connection;
55   private Destination _destination;
56
57   private String JavaDoc _subscriptionName;
58   private String JavaDoc _selector;
59   private int _acknowledgeMode = Session.AUTO_ACKNOWLEDGE;
60
61   private int _consumerMax = 5;
62
63   private MessageDrivenContext JavaDoc _context;
64
65   private ArrayList JavaDoc<Consumer> _consumers = new ArrayList JavaDoc<Consumer>();
66
67   public MessageServer(EjbServerManager manager)
68   {
69     super(manager);
70     
71     _context = new MessageDrivenContextImpl(this);
72   }
73
74   /**
75    * Sets the destination.
76    */

77   public void setDestination(Destination destination)
78   {
79     _destination = destination;
80   }
81
82   /**
83    * Sets the consumer max
84    */

85   public void setConsumerMax(int consumer)
86   {
87     _consumerMax = consumer;
88   }
89
90   /**
91    * Initialize the server
92    */

93   public void init()
94     throws Exception JavaDoc
95   {
96     super.init();
97   }
98
99   /**
100    * Starts the server.
101    */

102   public void start()
103     throws Exception JavaDoc
104   {
105     super.start();
106
107     ConnectionFactory factory = null;//_config.getConnectionFactory();
108
if (factory == null)
109       factory = _ejbManager.getConnectionFactory();
110
111     if (_destination == null)
112       throw new ConfigException(L.l("No destination is configured."));
113
114     if (_consumerMax <= 0)
115       throw new ConfigException(L.l("No listeners are configured."));
116
117     if (factory == null)
118       throw new ConfigException(L.l("Message beans need a jms-connection-factory. The ConnectionFactory object must be configured."));
119
120     Connection connection = factory.createConnection();
121     _connection = connection;
122
123     if (_destination instanceof Topic)
124       _consumerMax = 1;
125
126     for (int i = 0; i < _consumerMax; i++) {
127       Consumer consumer = new Consumer();
128
129       _consumers.add(consumer);
130
131       consumer.start();
132     }
133     
134     _connection.start();
135
136   }
137   
138   void generate()
139     throws Exception JavaDoc
140   {
141   }
142
143   public AbstractContext getContext(Object JavaDoc obj, boolean foo)
144   {
145     throw new UnsupportedOperationException JavaDoc();
146   }
147
148   public Connection getJMSConnection()
149   {
150     return _connection;
151   }
152
153   public Destination getDestination()
154   {
155     return _destination;
156   }
157   
158   /**
159    * Cleans up the entity server nicely.
160    */

161   public void destroy()
162   {
163     try {
164       ArrayList JavaDoc<Consumer> consumers = new ArrayList JavaDoc<Consumer>(_consumers);
165       _consumers = null;
166       
167       for (Consumer consumer : consumers) {
168     consumer.destroy();
169       }
170       
171       if (_connection != null)
172         _connection.close();
173     } catch (Exception JavaDoc e) {
174       log.log(Level.WARNING, e.toString(), e);
175     }
176   }
177
178   class Consumer {
179     private Session _session;
180     private MessageConsumer _consumer;
181     private MessageListener _listener;
182     
183     Consumer()
184       throws Exception JavaDoc
185     {
186     }
187
188     /**
189      * Creates the session.
190      */

191     void start()
192       throws Exception JavaDoc
193     {
194       Class JavaDoc cl = _contextImplClass;
195     
196       _listener = (MessageListener) cl.newInstance();
197
198       if (_listener instanceof MessageDrivenBean JavaDoc) {
199     MessageDrivenBean JavaDoc bean = (MessageDrivenBean JavaDoc) _listener;
200     bean.setMessageDrivenContext(_context);
201       }
202
203       Method JavaDoc create = null;
204
205       try {
206     create = cl.getMethod("ejbCreate", new Class JavaDoc[0]);
207     create.invoke(_listener, new Object JavaDoc[0]);
208       } catch (NoSuchMethodException JavaDoc e) {
209       }
210       
211       // XXX: ejb/090c
212
// XXX: doesn't seem to be properly handling the sessions
213
boolean transacted = false;
214
215       _session = _connection.createSession(transacted, _acknowledgeMode);
216
217       if (_subscriptionName != null) {
218     Topic topic = (Topic) _destination;
219     
220     _consumer = _session.createDurableSubscriber(topic,
221                              _subscriptionName,
222                              _selector,
223                              true);
224       }
225       else {
226     _consumer = _session.createConsumer(_destination, _selector);
227       }
228
229       _consumer.setMessageListener(_listener);
230     }
231
232     /**
233      * Returns the session.
234      */

235     public Session getSession()
236       throws JMSException
237     {
238       return _session;
239     }
240
241     /**
242      * Destroys the listener.
243      */

244     private void destroy()
245       throws JMSException
246     {
247       Session session = _session;
248       _session = null;
249
250       try {
251     if (session != null)
252       session.close();
253       } finally {
254     if (_listener instanceof MessageDrivenBean JavaDoc) {
255       MessageDrivenBean JavaDoc bean = (MessageDrivenBean JavaDoc) _listener;
256       _listener = null;
257       bean.ejbRemove();
258     }
259       }
260     }
261   }
262 }
263
Popular Tags