Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 从外部应用程序向嵌入式HornetQ发送消息_Spring Boot_Hornetq - Fatal编程技术网

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

Spring boot 从外部应用程序向嵌入式HornetQ发送消息,spring-boot,hornetq,Spring Boot,Hornetq,我使用的是spring boot 1.2.2 我在应用程序中有一个嵌入式hornet队列设置。属性: spring.hornetq.mode=embedded spring.hornetq.embedded.enabled=true spring.hornetq.embedded.queues=myQueue 我想从外部应用程序(不是嵌入队列的应用程序)向“myQueue”添加消息。这可能吗 在另一个应用程序(没有嵌入式hornetq的应用程序)中,我尝试创建一个指向嵌入式hornetq服务器

我使用的是spring boot 1.2.2

我在
应用程序中有一个嵌入式hornet队列设置。属性

spring.hornetq.mode=embedded
spring.hornetq.embedded.enabled=true
spring.hornetq.embedded.queues=myQueue
我想从外部应用程序(不是嵌入队列的应用程序)向“myQueue”添加消息。这可能吗

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

以下是我目前的代码:

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

}
但是我遇到了一个连接问题

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

问题是嵌入式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);
        }
    };
}
在要连接到嵌入式服务器的“其他”应用程序中,您可以在本机模式下配置HornetQ:

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

我正在研究类似的东西(最终我想将两个嵌入式HornetQ设置进行集群),但也没有弄清楚。首先,我认为您需要在嵌入式服务器上添加一个传输,允许在实际端口上进行连接,默认情况下,只配置InVMConnectorFactory。我无法实现这一点。两个应用程序都可以正常启动,没有错误。当我尝试发送消息时,我会收到
org.hornetq.api.core.HornetQNotConnectedException:HQ119007:无法连接到服务器。尝试了所有可用的服务器。
我开始使用另一个端口“5455”,一切都开始工作。。。不知道为什么。
@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
spring.hornetq.mode=native
spring.hornetq.host=localhost
spring.hornetq.port=5445