Spring启动和嵌入式activemq主机配置

时间:2016-02-25 07:59:44

标签: java spring-boot activemq

我有一个spring boot应用程序,它接收来自客户端的websocket主题订阅,这些订阅将被路由到我的嵌入式activemq代理。

启动嵌入式activemq代理的代码

@SpringBootApplication
public class RttApplication {

public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext ctx = SpringApplication.run(RttApplication.class, args);
    BrokerService brokerService = new BrokerService();
    brokerService.setPersistent(false);
    brokerService.setUseJmx(false);
    brokerService.addConnector("vm://localhost:0");
    brokerService.setBrokerName("event");
    brokerService.start();
}

}

我的春季经纪人接力配置类

@Configuration
@EnableWebSocketMessageBroker
public class MessageBrokerConfigurer extends AbstractWebSocketMessageBrokerConfigurer {
   @Override
   public void registerStompEndpoints(StompEndpointRegistry registry) {
       registry.addEndpoint("/event").withSockJS();
   }

   @Override
   public void configureMessageBroker(MessageBrokerRegistry registry) {
       registry.enableStompBrokerRelay("/topic").setRelayHost("vm://localhost").setRelayPort(0);
       registry.setApplicationDestinationPrefixes("/app");
   }
}

但是当我启动应用程序时它会显示出来

  

2016-02-25 15:44:34.678 INFO 7604 --- [主要]   o.a.activemq.broker.TransportConnector:Connector vm:// localhost:0   开始

     

2016-02-25 15:44:34.694 INFO 7604 --- [主要]   o.apache.activemq.broker.BrokerService:Apache ActiveMQ 5.7.0   (事件,ID:PC13082-53189-1456386274543-0:1)开始

     

2016-02-25 15:44:34.694 INFO 7604 --- [主要]   o.apache.activemq.broker.BrokerService:寻求帮助或更多   有关信息,请参阅:http://activemq.apache.org

     

2016-02-25 15:44:39.532 INFO 7604 --- [eactor-tcp-io-2]   r.io.net.impl.netty.tcp.NettyTcpClient:无法连接   VM:// localhost:0程序。尝试重新连接5000毫秒。

1 个答案:

答案 0 :(得分:5)

问题解决了,因为Spring配置器方法意味着它是一个stomp代理中继,它必须是通过stomp协议。

显然也不需要传输协议前缀。如果在默认的activemq安装中设置了用户名和密码,我还需要输入用户名和密码。但这是在启动独立的ActiveMQ之后完成的,我实际上正在寻找的是嵌入式解决方案。

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableStompBrokerRelay("/topic")
        .setRelayHost("127.0.0.1")
        .setRelayPort(61613)
        .setClientLogin("system")
        .setClientPasscode("password")
    registry.setApplicationDestinationPrefixes("/app");

}

<强>更新

回应Deinum上述评论之一。我还尝试在我的spring boot应用程序的application.properties中设置以下内容:

spring.activemq.broker-url=stomp://127.0.0.1:61614
spring.activemq.user=system
spring.activemq.password=password

但控制台显示没有启动ActiveMQ的证据,也无法通过我上面发布的stomp broker继电器配置连接到它。我最终创建了一个spring配置类,它现在可以工作了:

//@Configuration
public class TestBrokerConfig {

@Bean( initMethod = "start", destroyMethod = "stop" )
public BrokerService broker() throws Exception {
    final BrokerService broker = new BrokerService();               
    broker.addConnector( "stomp://localhost:61614" );

    broker.setPersistent( false );
    broker.setShutdownHooks( Collections.< Runnable >singletonList( new SpringContextHook() ) );
    final ManagementContext managementContext = new ManagementContext();
    managementContext.setCreateConnector( true );
    broker.setManagementContext( managementContext );

    return broker;
}
}