2015-03-31 134 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

相關問題