2

我正在尝试使用 hornetq 核心 API 和 spring 来定义 hornetq 客户端,这将是 HA。但是当我停止实时服务器并且备份服务器变为“实时”时,客户端失败并出现连接错误。知道我做错了什么吗?

我定义了集群的独立 hornetq 服务器(没有组或发现)。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>

...

我定义了hornetq客户端:

@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);
}

输出:

 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.

此外,现场服务器有日志错误:

  • [Thread-4 (HornetQ-server-HornetQServerImpl::serverUUID=70ca0e65-8ecf-11e1-82e8-d5eac4a07302-1433965066)] 1-May 8:27:57,976 FINE [ServerLocatorImpl] 连接器 [initialConnector=org-hornetq-core-remoting -impl-netty-NettyConnectorFactory?port=5446&host=10-45-7-150]:: 建立连接器初始连接时出现异常 HornetQException[errorCode=2 message=无法使用配置 org-hornetq-core-remoting-impl 连接到服务器-netty-NettyConnectorFactory?port=5446&host=backup-server] 在 org.hornetq.core.client.impl.ClientSessionFactoryImpl.connect(ClientSessionFactoryImpl.java:234) 在 org.hornetq.core.client.impl.ServerLocatorImpl$StaticConnector$Connector .tryConnect(ServerLocatorImpl.java:1777) 在 org.hornetq.core.client.impl.ServerLocatorImpl$StaticConnector.connect(ServerLocatorImpl.java:1614) 在 org.hornetq。core.client.impl.ServerLocatorImpl.connect(ServerLocatorImpl.java:587) 在 org.hornetq.core.client.impl.ServerLocatorImpl$3.run(ServerLocatorImpl.java:554) 在 org.hornetq.utils.OrderedExecutorFactory$OrderedExecutor$1。 run(OrderedExecutorFactory.java:100) 在 java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 在 java.lang .Thread.run(Thread.java:662)concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 在 java.lang.Thread.run(Thread.java:662)concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 在 java.lang.Thread.run(Thread.java:662)
4

2 回答 2

1

我找到了如何创建与 HA 一起使用的 hornetq 客户端,但它需要我使用 JNDI 并通过查找获取连接工厂。我确实喜欢这样:

<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>

有没有办法在没有 JNDI 的情况下创建这样的客户端?

于 2012-05-03T07:00:12.803 回答
0

spring config 设置 HornetQ 故障转移到备份服务器

<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>

Q1 为主,Q2 设置为备用。适用于 HornetQ hornetq-2.4.0.Final 独立安装,客户端 hornetq-jms-client 版本:2.4.5.Final

于 2015-06-11T00:06:12.443 回答