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.core.client;
014    
015    import org.hornetq.api.core.Pair;
016    import org.hornetq.api.core.TransportConfiguration;
017    import org.hornetq.api.core.client.loadbalance.RoundRobinConnectionLoadBalancingPolicy;
018    import org.hornetq.core.client.impl.ClientSessionFactoryImpl;
019    
020    import java.util.List;
021    
022    /**
023     * Utility class for creating HornetQ {@link ClientSessionFactory} objects.
024     * 
025     * Once a {@link ClientSessionFactory} has been created, it can be further configured
026     * using its setter methods before creating the sessions. Once a session is created,
027     * the factory can no longer be modified (its setter methods will throw a {@link IllegalStateException}.
028     * 
029     * @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
030     */
031    public class HornetQClient
032    {
033       public static final String DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME = RoundRobinConnectionLoadBalancingPolicy.class.getCanonicalName();
034    
035       public static final long DEFAULT_CLIENT_FAILURE_CHECK_PERIOD = 30000;
036    
037       // 1 minute - this should be higher than ping period
038    
039       public static final long DEFAULT_CONNECTION_TTL = 1 * 60 * 1000;
040    
041       // Any message beyond this size is considered a large message (to be sent in chunks)
042       
043       public static final int DEFAULT_MIN_LARGE_MESSAGE_SIZE = 100 * 1024;
044    
045       public static final int DEFAULT_CONSUMER_WINDOW_SIZE = 1024 * 1024;
046    
047       public static final int DEFAULT_CONSUMER_MAX_RATE = -1;
048    
049       public static final int DEFAULT_CONFIRMATION_WINDOW_SIZE = -1;
050    
051       public static final int DEFAULT_PRODUCER_WINDOW_SIZE = 64 * 1024;
052    
053       public static final int DEFAULT_PRODUCER_MAX_RATE = -1;
054    
055       public static final boolean DEFAULT_BLOCK_ON_ACKNOWLEDGE = false;
056    
057       public static final boolean DEFAULT_BLOCK_ON_DURABLE_SEND = true;
058    
059       public static final boolean DEFAULT_BLOCK_ON_NON_DURABLE_SEND = false;
060    
061       public static final boolean DEFAULT_AUTO_GROUP = false;
062    
063       public static final long DEFAULT_CALL_TIMEOUT = 30000;
064    
065       public static final int DEFAULT_ACK_BATCH_SIZE = 1024 * 1024;
066    
067       public static final boolean DEFAULT_PRE_ACKNOWLEDGE = false;
068    
069       public static final long DEFAULT_DISCOVERY_INITIAL_WAIT_TIMEOUT = 10000;
070    
071       public static final long DEFAULT_DISCOVERY_REFRESH_TIMEOUT = 10000;
072    
073       public static final long DEFAULT_RETRY_INTERVAL = 2000;
074    
075       public static final double DEFAULT_RETRY_INTERVAL_MULTIPLIER = 1d;
076    
077       public static final long DEFAULT_MAX_RETRY_INTERVAL = 2000;
078    
079       public static final int DEFAULT_RECONNECT_ATTEMPTS = 0;
080       
081       public static final boolean DEFAULT_FAILOVER_ON_INITIAL_CONNECTION = false;
082    
083       public static final boolean DEFAULT_FAILOVER_ON_SERVER_SHUTDOWN = false;
084    
085       public static final boolean DEFAULT_USE_GLOBAL_POOLS = true;
086    
087       public static final int DEFAULT_THREAD_POOL_MAX_SIZE = -1;
088    
089       public static final int DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE = 5;
090    
091       public static final boolean DEFAULT_CACHE_LARGE_MESSAGE_CLIENT = false;
092    
093       public static final int DEFAULT_INITIAL_MESSAGE_PACKET_SIZE = 1500;
094       
095       /**
096        * Creates a ClientSessionFactory using all the defaults.
097        *
098        * @return the ClientSessionFactory.
099        */
100       public static ClientSessionFactory createClientSessionFactory()
101       {
102          return new ClientSessionFactoryImpl();
103       }
104    
105       /**
106        * Creates a new ClientSessionFactory using the same configuration as the one passed in.
107        *
108        * @param other The ClientSessionFactory to copy
109        * @return The new ClientSessionFactory
110        */
111       public static ClientSessionFactory createClientSessionFactory(final ClientSessionFactory other)
112       {
113          return new ClientSessionFactoryImpl(other);
114       }
115    
116       /**
117        * Creates a ClientSessionFactory that uses discovery to connect to the servers.
118        *
119        * @param discoveryAddress The address to use for discovery
120        * @param discoveryPort The port to use for discovery.
121        * @return The ClientSessionFactory.
122        */
123       public static ClientSessionFactory createClientSessionFactory(final String discoveryAddress, final int discoveryPort)
124       {
125          return new ClientSessionFactoryImpl(discoveryAddress, discoveryPort);
126       }
127    
128       /**
129        * Creates a ClientSessionFactory using a List of TransportConfigurations and backups.
130        *
131        * @param staticConnectors The list of TransportConfiguration's to use.
132        * @return The ClientSessionFactory.
133        */
134       public static ClientSessionFactory createClientSessionFactory(final List<Pair<TransportConfiguration, TransportConfiguration>> staticConnectors)
135       {
136          return new ClientSessionFactoryImpl(staticConnectors);
137       }
138    
139       /**
140        * Creates a ClientConnectionFactory using a TransportConfiguration of the server and a backup if needed.
141        *
142        * @param connectorConfig The TransportConfiguration of the server to connect to.
143        * @param backupConnectorConfig The TransportConfiguration of the backup server to connect to (or {@code null} if there is no backup)
144        * @return The ClientSessionFactory.
145        */
146       public static ClientSessionFactory createClientSessionFactory(final TransportConfiguration connectorConfig,
147                                       final TransportConfiguration backupConnectorConfig)
148       {
149          return new ClientSessionFactoryImpl(connectorConfig, backupConnectorConfig);
150       }
151    
152       /**
153        * Creates a ClientSessionFactory using the TransportConfiguration of the server to connect to.
154        *
155        * @param connectorConfig The TransportConfiguration of the server.
156        * @return The ClientSessionFactory.
157        */
158       public static ClientSessionFactory createClientSessionFactory(final TransportConfiguration connectorConfig)
159       {
160          return new ClientSessionFactoryImpl(connectorConfig);
161       }
162    
163       private HornetQClient()
164       {
165       }
166    }