Glass
Glass

Reputation: 91

Configure clustered hornetq client with spring

I'm trying to define hornetq client with hornetq core API and spring that will be HA. But when I stop the live server, and the backup server become "live", the client fail with connection error. Any idea what I'm doing wrong?

I defined clustered stand-alone hornetq server (without group or discovery). hornetq-configuration.xml:

...
<connectors>
  <connector name="netty">
     <factory-class>org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</factory-class>
     <param key="host"  value="${hornetq.remoting.netty.host:live-host}"/>
     <param key="port"  value="${hornetq.remoting.netty.port:5445}"/>
  </connector>

  <!-- Connector to this server.s backup. This is needed for failback to work properly in a static cluster -->
  <connector name="netty-backup">
     <factory-class>org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</factory-class>
     <param key="host"  value="${hornetq.remoting.netty.host:backup-host}"/>
     <param key="port" value="${hornetq.remoting.netty.port:5446}"/>
  </connector>

<cluster-connections>
  <cluster-connection name="my-cluster">
     <address>jms</address>
     <connector-ref>netty</connector-ref>
     <retry-interval>500</retry-interval>
     <use-duplicate-detection>true</use-duplicate-detection>

     <forward-when-no-consumers>false</forward-when-no-consumers>
     <max-hops>1</max-hops>
     <static-connectors>
        <!-- Without this the connection factory won.t be able to reconnect on failback -->
        <connector-ref>netty-backup</connector-ref>
     </static-connectors>

  </cluster-connection>

...

I defined hornetq client:

@Test
public void testHA() throws InterruptedException, IOException{
    List<TransportConfiguration> transportConfigurationList = new ArrayList<TransportConfiguration>();
       Map<String, Object> transportProperties = new HashMap<String, Object>();
                    transportProperties.put("host", "live-server");
                    transportProperties.put("port", 5445);
       transportConfigurationList.add(new TransportConfiguration("org.hornetq.core.remoting.impl.netty.NettyConnectorFactory", transportProperties));
       transportProperties = new HashMap<String, Object>();
                    transportProperties.put("host", "backup-server");
                    transportProperties.put("port", 5446);
       transportConfigurationList.add(new TransportConfiguration("org.hornetq.core.remoting.impl.netty.NettyConnectorFactory", transportProperties));
       HornetQJMSConnectionFactory connectionFactory = new HornetQJMSConnectionFactory(true, transportConfigurationList.toArray(new TransportConfiguration[2]));

       JmsTemplate producer = new JmsTemplate(connectionFactory);
       producer.send(new HornetQTopic("deploy"), new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage txtMessage = session.createTextMessage("my message");                   
                return txtMessage;
            }
        });

       System.out.println("Stop live server");
       System.in.read();

       producer.send(new HornetQTopic("deploy"), new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage txtMessage = session.createTextMessage("my message");                   
                return txtMessage;
            }
        });

       Thread.sleep(60000);
}

OUTPUT:

 Stop live server

Apr 30, 2012 5:08:32 PM org.hornetq.core.logging.impl.JULLogDelegate warn
WARNING: Tried 1 times to connect. Now giving up on reconnecting it.

In Addition ,the live server have log error:

Upvotes: 2

Views: 2293

Answers (2)

user4997113
user4997113

Reputation: 1

spring config to set up HornetQ fail-over to backup server

<bean id="hornetQConnectionFactory" class="org.hornetq.jms.client.HornetQJMSConnectionFactory">
        <constructor-arg index="0" name="ha" value="true" />
        <constructor-arg index="1">
            <list>
                <bean class="org.hornetq.api.core.TransportConfiguration">
                    <constructor-arg value="org.hornetq.core.remoting.impl.netty.NettyConnectorFactory" />
                    <constructor-arg>
                        <map key-type="java.lang.String" 
                             value-type="java.lang.Object">
                            <entry key="host" value="Q-server-1"></entry>
                            <entry key="port" value="5445"></entry>
                        </map>
                    </constructor-arg>
                </bean>
                <bean class="org.hornetq.api.core.TransportConfiguration">
                    <constructor-arg value="org.hornetq.core.remoting.impl.netty.NettyConnectorFactory" />
                    <constructor-arg>
                        <map key-type="java.lang.String" 
                             value-type="java.lang.Object">
                            <entry key="host" value="Q-server2"></entry>
                            <entry key="port" value="5446"></entry>
                        </map>
                    </constructor-arg>
                </bean>
            </list>
        </constructor-arg>
    </bean>

The Q1 is the main and Q2 is set up as a backup. Works with HornetQ hornetq-2.4.0.Final standalone installs, and client of hornetq-jms-client version: 2.4.5.Final

Upvotes: 0

Glass
Glass

Reputation: 91

I found how to create hornetq client working with HA, but it require me to use JNDI and to get the connection factory with lookup. I did like this:

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.provider.url">jnp://jndi-host:jndi-port</prop>
            <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
        </props>
    </property>
</bean>

<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate" ref="jndiTemplate" />
    <property name="jndiName" value="ConnectionFactory" />
</bean>

Is there a way I could create such a client without JNDI?

Upvotes: 1

Related Questions