36.2.4. Discovery Groups on the Client Side

Let us discuss how to configure a HornetQ client to use discovery to discover a list of servers to which it can connect. The way to do this differs depending on whether you are using JMS or the core API.

36.2.4.1. Configuring client discovery using JMS

If you are using JMS and you are also using the JMS Service on the server to load your JMS connection factory instances into JNDI, then you can specify which discovery group to use for your JMS connection factory in the server side XML configuration JBOSS_DIST/jboss-as/server/<PROFILE>/deploy/hornetq/hornetq-jms.xml. Let us take a look at an example:
<connection-factory name="ConnectionFactory">
   <discovery-group-ref discovery-group-name="my-discovery-group"/>
    <entries>
       <entry name="/ConnectionFactory"/>
    </entries>
</connection-factory>
The element discovery-group-ref specifies the name of a discovery group defined in <JBOSS_HOME>/jboss-as/server/<PROFILE>/deploy/hornetq/hornetq-configuration.xml.
When this connection factory is downloaded from JNDI by a client application and JMS connections are created from it, those connections will be load-balanced across the list of servers that the discovery group maintains by listening on the multicast address specified in the discovery group configuration.
If you are using JMS, but you are not using JNDI to lookup a connection factory - you are instantiating the JMS connection factory directly then you can specify the discovery group parameters directly when creating the JMS connection factory. Here is an example:
final String groupAddress = "231.7.7.7";

final int groupPort = 9876;

ConnectionFactory jmsConnectionFactory = 
   HornetQJMSClient.createConnectionFactory(groupAddress, groupPort);

Connection jmsConnection1 = jmsConnectionFactory.createConnection();

Connection jmsConnection2 = jmsConnectionFactory.createConnection();
The refresh-timeout can be set directly on the connection factory by using the setter method setDiscoveryRefreshTimeout() if you want to change the default value.
There is also a further parameter settable on the connection factory using the setter method setDiscoveryInitialWaitTimeout(). If the connection factory is used immediately after creation then it may not have had enough time to receive broadcasts from all the nodes in the cluster. On first usage, the connection factory will make sure it waits this long since creation before creating the first connection. The default value for this parameter is 10000 milliseconds.