从外部应用程序向嵌入式 HornetQ 发送消息

Sending a message to an embedded HornetQ from an external application

我正在使用 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 服务器默认只配置了一个 InVMAcceptorFactory。您需要添加一个实际侦听端口的 AcceptorFactory,例如 NettyAcceptorFactory.

您可以使用 HornetQConfigurationCustomizer 来配置它。下面的示例使用硬编码 host/port,但您可以轻松创建自己的属性以使其可配置。

@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

并且在您想要连接到嵌入式服务器的 "other" 应用程序中,您将 HornetQ 配置为本地模式:

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