2015-03-31 77 views
1

我正在使用spring-boot 1.2.2。从外部应用程序向嵌入式HornetQ发送消息

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

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

我要添加一个消息到从外部应用程序(而不是一个具有嵌入的队列)“myQueue中”。这可能吗?

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

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) 
+0

我在寻找类似的东西(最终我要群集两个嵌入式HornetQ的设置),但还没有想通出来呢要么。我想一开始,您需要在嵌入式服务器上添加一个允许连接实际端口的传输,默认情况下,只会配置一个InVMConnectorFactory。 – 2015-03-31 08:35:18

回答

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

我无法得到这个工作。两个应用程序都可以正常启动,不会出错当我尝试发送消息时,我得到'org.hornetq.api.core.HornetQNotConnectedException:HQ119007:无法连接到服务器。尝试所有可用的服务器.' – jax 2015-04-10 00:34:43

+0

我开始使用不同的端口“5455”,并开始工作......不知道为什么。 – jax 2015-04-10 02:21:56

相关问题