View Javadoc

1   /***
2    *
3    * Copyright 2004 Hiram Chirino
4    * Copyright 2004 Protique Ltd
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   **/
19  package org.codehaus.activemq.transport.gnet;
20  
21  import org.apache.geronimo.network.SelectorManager;
22  import org.apache.geronimo.pool.ClockPool;
23  import org.apache.geronimo.pool.ThreadPool;
24  import org.codehaus.activemq.message.WireFormat;
25  import org.codehaus.activemq.transport.TransportChannel;
26  import org.codehaus.activemq.transport.TransportChannelFactorySupport;
27  
28  import javax.jms.JMSException;
29  import java.net.URI;
30  
31  /***
32   * An implementation of a TransportChannelFactory which uses
33   * the Geronimo network layer for connectivity.
34   *
35   * @version $Revision: 1.9 $
36   */
37  public class GTransportChannelFactory extends TransportChannelFactorySupport {
38  
39      private static ThreadPool threadPool;
40      private static ClockPool clockPool;
41      private static SelectorManager selectorManager;
42  
43      public static void init(SelectorManager sm, ThreadPool tp,
44                              ClockPool cp) throws IllegalArgumentException {
45  
46          if (sm == null || tp == null || cp == null) {
47              throw new IllegalArgumentException();
48          }
49  
50          selectorManager = sm;
51          threadPool = tp;
52          clockPool = cp;
53      }
54  
55      private static void init() throws Exception {
56  
57          // Don't init if allready done.
58          if (threadPool != null) {
59              return;
60          }
61  
62          threadPool = new ThreadPool();
63          threadPool.setKeepAliveTime(1 * 1000);
64          threadPool.setPoolSize(5);
65          threadPool.setPoolName("C Pool");
66  
67          clockPool = new ClockPool();
68          clockPool.setPoolName("C Clock");
69  
70          selectorManager = new SelectorManager();
71          selectorManager.setThreadPool(threadPool);
72          selectorManager.setThreadName("C Manager");
73          selectorManager.setTimeout(1000);
74  
75          threadPool.doStart();
76          clockPool.doStart();
77          selectorManager.doStart();
78  
79      }
80  
81      /***
82       * Create a Channel to a remote Node - e.g. a Broker
83       *
84       * @param wireFormat
85       * @param remoteLocation
86       * @return the TransportChannel bound to the remote node
87       * @throws JMSException
88       */
89      public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
90          return create(null, remoteLocation);
91      }
92  
93      /***
94       * Create a Channel to a remote Node - e.g. a Broker
95       *
96       * @param wireFormat
97       * @param remoteLocation
98       * @param localLocation  -
99       *                       e.g. local InetAddress and local port
100      * @return the TransportChannel bound to the remote node
101      * @throws JMSException
102      */
103     public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
104         try {
105             init();
106             return populateProperties(new GTransportChannel(wireFormat, remoteLocation, localLocation, selectorManager, threadPool, clockPool), remoteLocation);
107         }
108         catch (Exception e) {
109             JMSException ex = new JMSException(e.getMessage());
110             ex.setLinkedException(e);
111             throw ex;
112         }
113     }
114 
115     public boolean requiresEmbeddedBroker() {
116         return false;
117     }
118 }