36.5. Specifying Members of a Cluster Explicitly
Sometimes UDP is not enabled on a network so it is not possible to use UDP server discovery for clients to discover the list of servers in the cluster, or for servers to discover what other servers are in the cluster.
In this case, the list of servers in the cluster can be specified explicitly on each node and on the client side. This is done as follows:
36.5.1. Specify List of Servers on the Client Side
This differs depending on whether you are using JMS or the Core API.
36.5.1.1. Specifying List of Servers using JMS
If using JMS and the JMS Service to load your JMS connection factory instances directly into JNDI on the server, then you can specify the list of servers in the server side configuration file
JBOSS_DIST/jboss-as/server/<PROFILE>/deploy/hornetq/hornetq-jms.xml
. Let us take a look at an example:
<connection-factory name="ConnectionFactory"> <connectors> <connector-ref connector-name="my-connector1" backup-connector-name="my-backup-connector1"/> <connector-ref connector-name="my-connector2" backup-connector-name="my-backup-connector2"/> <connector-ref connector-name="my-connector3" backup-connector-name="my-backup-connector3"/> </connectors> <entries> <entry name="/ConnectionFactory"/> </entries> </connection-factory>
The
connection-factory
element can contain zero or more connector-ref
elements, each one of which specifies a connector-name
attribute and an optional backup-connector-name
attribute. The connector-name
attribute references a connector defined in <JBOSS_HOME>/jboss-as/server/<PROFILE>/deploy/hornetq/hornetq-configuration.xml
which will be used as a live connector. The backup-connector-name
is optional, and if specified it also references a connector defined in hornetq-configuration.xml
. For more information on connectors refer to Chapter 14, Configuring the Transport.
The connection factory thus maintains a list of [connector, backup connector] pairs, these pairs are then used by the client connection load balancing policy on the client side when creating connections to the cluster.
If you are using JMS but you are not using JNDI then you can also specify the list of [connector, backup connector] pairs directly when instantiating the
HornetQConnectionFactory
. Here is an example:
List<Pair<TransportConfiguration, TransportConfiguration>> serverList = new ArrayList<Pair<TransportConfiguration, TransportConfiguration>>(); serverList.add(new Pair<TransportConfiguration, TransportConfiguration>(liveTC0, backupTC0)); serverList.add(new Pair<TransportConfiguration, TransportConfiguration>(liveTC1, backupTC1)); serverList.add(new Pair<TransportConfiguration, TransportConfiguration>(liveTC2, backupTC2)); ConnectionFactory jmsConnectionFactory = HornetQJMSClient.createConnectionFactory(serverList); Connection jmsConnection1 = jmsConnectionFactory.createConnection(); Connection jmsConnection2 = jmsConnectionFactory.createConnection();
The above snippet creates a list of pairs of
TransportConfiguration
objects. Each TransportConfiguration
object contains knowledge of how to make a connection to a specific server.
Create a
HornetQConnectionFactory
instance, passing the list of servers in the constructor. Any connections subsequently created by this factory will create connections according to the client connection load balancing policy applied to that list of servers.
36.5.1.2. Specifying List of Servers using the Core API
Specify the list of servers directly when creating the
ClientSessionFactory
instance as in the following example:
List<Pair<TransportConfiguration, TransportConfiguration>> serverList = new ArrayList<Pair<TransportConfiguration, TransportConfiguration>>(); serverList.add(new Pair<TransportConfiguration, TransportConfiguration>(liveTC0, backupTC0)); serverList.add(new Pair<TransportConfiguration, TransportConfiguration>(liveTC1, backupTC1)); serverList.add(new Pair<TransportConfiguration, TransportConfiguration>(liveTC2, backupTC2)); ClientSessionFactory factory = HornetQClient.createClientSessionFactory(serverList); ClientSession session1 = factory.createClientSession(...); ClientSession session2 = factory.createClientSession(...);
The above snippet creates a list of pairs of
TransportConfiguration
objects. Each TransportConfiguration
object contains knowledge of how to make a connection to a specific server. For more information on this, refer to Chapter 14, Configuring the Transport.
A
ClientSessionFactoryImpl
instance is then created passing the list of servers in the constructor. Any sessions subsequently created by this factory will create sessions according to the client connection load balancing policy applied to that list of servers.