【问题标题】:Sending a message to an embedded HornetQ from an external application从外部应用程序向嵌入式 HornetQ 发送消息
【发布时间】:2015-06-04 06:50:02
【问题描述】:

我使用的是 spring-boot 1.2.2。

我在application.properties 中有一个嵌入式大黄蜂队列设置:

spring.hornetq.mode=embedded
spring.hornetq.embedded.enabled=true
spring.hornetq.embedded.queues=myQueue

我想从外部应用程序(不是具有嵌入式队列的应用程序)向“myQueue”添加一条消息。这可能吗?

在另一个应用程序(没有嵌入式 hornetq 的应用程序)中,我尝试创建一个指向嵌入式 hornetq 服务器的 connectionFactory,但我真的不知道应该使用哪个端口。根据 spring-boot documentation 它说它只对“native”模式有效。

spring.hornetq.mode= # connection mode (native, embedded)
spring.hornetq.host=localhost # hornetQ host (native mode)
spring.hornetq.port=5445 # hornetQ port (native mode)

到目前为止,这是我的代码:

@EnableJms
@Configuration
public class HornetQConfig {

    @Bean
    public CachingConnectionFactory connectionFactory() {
        CachingConnectionFactory cachingConnectionFactory =
                new CachingConnectionFactory();
        cachingConnectionFactory.setSessionCacheSize(10);
        cachingConnectionFactory.setCacheProducers(false);
        cachingConnectionFactory.setTargetConnectionFactory(hornetQConnectionFactory());
        return cachingConnectionFactory;
    }

    @Bean
    public HornetQConnectionFactory hornetQConnectionFactory() {

        HornetQConnectionFactory connectionFactory =
                new HornetQConnectionFactory(false, transportConfiguration());
        return connectionFactory;
    }

    @Bean
    public TransportConfiguration transportConfiguration() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("host", "localhost");
        map.put("port", 5445);
        TransportConfiguration configuration =
                new TransportConfiguration(
                        "org.hornetq.core.remoting.impl.netty.NettyConnectorFactory", map);
        return configuration;
    }

}

然后:

@Autowired
private JmsTemplate jmsTemplate;

@Scheduled(fixedDelay = 1000L)
public void send() {
    this.jmsTemplate.convertAndSend("myQueue", "Hello from external app");
}

但我遇到了连接问题。

Failed to create session factory; nested exception is HornetQNotConnectedException[errorType=NOT_CONNECTED message=HQ119007: Cannot connect to server(s)

【问题讨论】:

  • 我正在寻找类似的东西(最终我想对两个嵌入式 HornetQ 设置进行集群),但也没有弄清楚。我认为首先,您需要在嵌入式服务器上添加一个允许在实际端口上连接的传输,默认情况下只会配置一个 InVMConnectorFactory。

标签: spring-boot hornetq


【解决方案1】:

问题是嵌入式 HornetQ 服务器默认只配置了InVMAcceptorFactory。您需要添加一个实际侦听端口的 AcceptorFactory,例如 NettyAcceptorFactory

您可以使用HornetQConfigurationCustomizer 进行配置。下面的示例使用硬编码的主机/端口,但您可以轻松创建自己的属性以使其可配置。

@Bean
public HornetQConfigurationCustomizer hornetCustomizer() {
    return new HornetQConfigurationCustomizer() {
        @Override
        public void customize(Configuration configuration) {
            Set<TransportConfiguration> acceptors = configuration.getAcceptorConfigurations();
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("host", "localhost");
            params.put("port", "5445");
            TransportConfiguration tc = new TransportConfiguration(NettyAcceptorFactory.class.getName(), params);
            acceptors.add(tc);
        }
    };
}

在您的带有嵌入式服务器的应用程序中,您将其配置为嵌入式(我相信您已经拥有了,只是为了确保):

spring.hornetq.mode=embedded
spring.hornetq.embedded.enabled=true
spring.hornetq.embedded.queues=myQueue

在您想要连接到嵌入式服务器的“其他”应用程序中,您可以在本地模式下配置 HornetQ:

spring.hornetq.mode=native
spring.hornetq.host=localhost
spring.hornetq.port=5445

【讨论】:

  • 我无法正常工作。两个应用程序都可以正常启动,没有错误。当我尝试发送消息时,我会收到org.hornetq.api.core.HornetQNotConnectedException: HQ119007: Cannot connect to server(s). Tried with all available servers.
  • 我开始使用不同的端口“5455”,一切都开始工作了......不知道为什么。
猜你喜欢
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多