001    /*
002     * Copyright 2009 Red Hat, Inc.
003     *  Red Hat licenses this file to you under the Apache License, version
004     *  2.0 (the "License"); you may not use this file except in compliance
005     *  with the License.  You may obtain a copy of the License at
006     *     http://www.apache.org/licenses/LICENSE-2.0
007     *  Unless required by applicable law or agreed to in writing, software
008     *  distributed under the License is distributed on an "AS IS" BASIS,
009     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
010     *  implied.  See the License for the specific language governing
011     *  permissions and limitations under the License.
012     */
013    package org.hornetq.api.jms;
014    
015    import javax.jms.Queue;
016    import javax.jms.Topic;
017    
018    import org.hornetq.api.core.DiscoveryGroupConfiguration;
019    import org.hornetq.api.core.TransportConfiguration;
020    import org.hornetq.core.logging.Logger;
021    import org.hornetq.jms.client.HornetQConnectionFactory;
022    import org.hornetq.jms.client.HornetQDestination;
023    import org.hornetq.jms.client.HornetQJMSConnectionFactory;
024    import org.hornetq.jms.client.HornetQQueueConnectionFactory;
025    import org.hornetq.jms.client.HornetQTopicConnectionFactory;
026    import org.hornetq.jms.client.HornetQXAConnectionFactory;
027    import org.hornetq.jms.client.HornetQXAQueueConnectionFactory;
028    import org.hornetq.jms.client.HornetQXATopicConnectionFactory;
029    
030    /**
031     * A utility class for creating HornetQ client-side JMS managed resources.
032     *
033     * @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
034     */
035    public class HornetQJMSClient
036    {
037       private static final Logger log = Logger.getLogger(HornetQJMSClient.class);
038    
039       /**
040        * Create a HornetQConnectionFactory which will receive cluster topology updates from the cluster as servers leave or join and new backups are appointed or removed.
041        * The discoveryAddress and discoveryPort parameters in this method are used to listen for UDP broadcasts which contain connection information for members of the cluster.
042        * The broadcasted connection information is simply used to make an initial connection to the cluster, once that connection is made, up to date
043        * cluster topology information is downloaded and automatically updated whenever the cluster topology changes. If the topology includes backup servers
044        * that information is also propagated to the client so that it can know which server to failover onto in case of live server failure.
045        * @param discoveryAddress The UDP group address to listen for updates
046        * @param discoveryPort the UDP port to listen for updates
047        * @return the HornetQConnectionFactory
048        */
049       public static HornetQConnectionFactory createConnectionFactoryWithHA(final DiscoveryGroupConfiguration groupConfiguration, JMSFactoryType jmsFactoryType)
050       {
051          HornetQConnectionFactory factory = null;
052          if (jmsFactoryType.equals(JMSFactoryType.CF))
053          {
054             factory = new HornetQJMSConnectionFactory(true, groupConfiguration);
055          }
056          else if (jmsFactoryType.equals(JMSFactoryType.QUEUE_CF))
057          {
058             factory = new HornetQQueueConnectionFactory(true, groupConfiguration);
059          }
060          else if (jmsFactoryType.equals(JMSFactoryType.TOPIC_CF))
061          {
062             factory = new HornetQTopicConnectionFactory(true, groupConfiguration);
063          }
064          else if (jmsFactoryType.equals(JMSFactoryType.XA_CF))
065          {
066             factory = new HornetQXAConnectionFactory(true, groupConfiguration);
067          }
068          else if (jmsFactoryType.equals(JMSFactoryType.QUEUE_XA_CF))
069          {
070             factory = new HornetQXAQueueConnectionFactory(true, groupConfiguration);
071          }
072          else if (jmsFactoryType.equals(JMSFactoryType.TOPIC_XA_CF))
073          {
074             factory = new HornetQXATopicConnectionFactory(true, groupConfiguration);
075          }
076          
077          return factory;
078       }
079    
080       /**
081        * Create a HornetQConnectionFactory which creates session factories from a set of live servers, no HA backup information is propagated to the client
082        * 
083        * The UDP address and port are used to listen for live servers in the cluster
084        * 
085        * @param discoveryAddress The UDP group address to listen for updates
086        * @param discoveryPort the UDP port to listen for updates
087        * @return the HornetQConnectionFactory
088        */
089       public static HornetQConnectionFactory createConnectionFactoryWithoutHA(final DiscoveryGroupConfiguration groupConfiguration, JMSFactoryType jmsFactoryType)
090       {
091          HornetQConnectionFactory factory = null;
092          if (jmsFactoryType.equals(JMSFactoryType.CF))
093          {
094             factory = new HornetQJMSConnectionFactory(false, groupConfiguration);
095          }
096          else if (jmsFactoryType.equals(JMSFactoryType.QUEUE_CF))
097          {
098             factory = new HornetQQueueConnectionFactory(false, groupConfiguration);
099          }
100          else if (jmsFactoryType.equals(JMSFactoryType.TOPIC_CF))
101          {
102             factory = new HornetQTopicConnectionFactory(false, groupConfiguration);
103          }
104          else if (jmsFactoryType.equals(JMSFactoryType.XA_CF))
105          {
106             factory = new HornetQXAConnectionFactory(false, groupConfiguration);
107          }
108          else if (jmsFactoryType.equals(JMSFactoryType.QUEUE_XA_CF))
109          {
110             factory = new HornetQXAQueueConnectionFactory(false, groupConfiguration);
111          }
112          else if (jmsFactoryType.equals(JMSFactoryType.TOPIC_XA_CF))
113          {
114             factory = new HornetQXATopicConnectionFactory(false, groupConfiguration);
115          }
116          
117          return factory;
118       }
119       
120       /**
121        * Create a HornetQConnectionFactory which will receive cluster topology updates from the cluster as servers leave or join and new backups are appointed or removed.
122        * The initial list of servers supplied in this method is simply to make an initial connection to the cluster, once that connection is made, up to date
123        * cluster topology information is downloaded and automatically updated whenever the cluster topology changes. If the topology includes backup servers
124        * that information is also propagated to the client so that it can know which server to failover onto in case of live server failure.
125        * @param initialServers The initial set of servers used to make a connection to the cluster. Each one is tried in turn until a successful connection is made. Once
126        * a connection is made, the cluster topology is downloaded and the rest of the list is ignored.
127        * @return the HornetQConnectionFactory
128        */
129       public static HornetQConnectionFactory createConnectionFactoryWithHA(JMSFactoryType jmsFactoryType, final TransportConfiguration... initialServers)
130       {
131          HornetQConnectionFactory factory = null;
132          if (jmsFactoryType.equals(JMSFactoryType.CF))
133          {
134             factory = new HornetQJMSConnectionFactory(true, initialServers);
135          }
136          else if (jmsFactoryType.equals(JMSFactoryType.QUEUE_CF))
137          {
138             factory = new HornetQQueueConnectionFactory(true, initialServers);
139          }
140          else if (jmsFactoryType.equals(JMSFactoryType.TOPIC_CF))
141          {
142             factory = new HornetQTopicConnectionFactory(true, initialServers);
143          }
144          else if (jmsFactoryType.equals(JMSFactoryType.XA_CF))
145          {
146             factory = new HornetQXAConnectionFactory(true, initialServers);
147          }
148          else if (jmsFactoryType.equals(JMSFactoryType.QUEUE_XA_CF))
149          {
150             factory = new HornetQXAQueueConnectionFactory(true, initialServers);
151          }
152          else if (jmsFactoryType.equals(JMSFactoryType.TOPIC_XA_CF))
153          {
154             factory = new HornetQXATopicConnectionFactory(true, initialServers);
155          }
156          
157          return factory;
158       }
159    
160       /**
161        * Create a HornetQConnectionFactory which creates session factories using a static list of transportConfigurations, the HornetQConnectionFactory is not updated automatically
162        * as the cluster topology changes, and no HA backup information is propagated to the client
163        * 
164        * @param transportConfigurations
165        * @return the HornetQConnectionFactory
166        */
167       public static HornetQConnectionFactory createConnectionFactoryWithoutHA(JMSFactoryType jmsFactoryType, final TransportConfiguration... transportConfigurations)
168       {
169          HornetQConnectionFactory factory = null;
170          if (jmsFactoryType.equals(JMSFactoryType.CF))
171          {
172             factory = new HornetQJMSConnectionFactory(false, transportConfigurations);
173          }
174          else if (jmsFactoryType.equals(JMSFactoryType.QUEUE_CF))
175          {
176             factory = new HornetQQueueConnectionFactory(false, transportConfigurations);
177          }
178          else if (jmsFactoryType.equals(JMSFactoryType.TOPIC_CF))
179          {
180             factory = new HornetQTopicConnectionFactory(false, transportConfigurations);
181          }
182          else if (jmsFactoryType.equals(JMSFactoryType.XA_CF))
183          {
184             factory = new HornetQXAConnectionFactory(false, transportConfigurations);
185          }
186          else if (jmsFactoryType.equals(JMSFactoryType.QUEUE_XA_CF))
187          {
188             factory = new HornetQXAQueueConnectionFactory(false, transportConfigurations);
189          }
190          else if (jmsFactoryType.equals(JMSFactoryType.TOPIC_XA_CF))
191          {
192             factory = new HornetQXATopicConnectionFactory(false, transportConfigurations);
193          }
194          
195          return factory;
196       }
197       
198       /**
199        * Creates a client-side representation of a JMS Topic.
200        *
201        * @param name the name of the topic
202        * @return The Topic
203        */
204       public static Topic createTopic(final String name)
205       {
206          return HornetQDestination.createTopic(name);
207       }
208    
209       /**
210        * Creates a client-side representation of a JMS Queue.
211        *
212        * @param name the name of the queue
213        * @return The Queue
214        */
215       public static Queue createQueue(final String name)
216       {
217          return HornetQDestination.createQueue(name);
218       }
219    
220       private HornetQJMSClient()
221       {
222       }
223    }