ron
ron

Reputation: 229

Spring boot and embedded activemq host configuration

I have a spring boot application and it receives stomp over websocket topic subscriptions from clients that which will be routed to my embedded activemq broker.

My code to start my embedded activemq broker

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

}

My spring broker relay configuration class

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

But it's showing this when I start up the application

2016-02-25 15:44:34.678 INFO 7604 --- [ main] o.a.activemq.broker.TransportConnector : Connector vm://localhost:0 Started

2016-02-25 15:44:34.694 INFO 7604 --- [ main] o.apache.activemq.broker.BrokerService : Apache ActiveMQ 5.7.0 (event, ID:PC13082-53189-1456386274543-0:1) started

2016-02-25 15:44:34.694 INFO 7604 --- [ main] o.apache.activemq.broker.BrokerService : For help or more information please see: http://activemq.apache.org

2016-02-25 15:44:39.532 INFO 7604 --- [eactor-tcp-io-2] r.io.net.impl.netty.tcp.NettyTcpClient : Failed to connect to vm://localhost:0. Attempting reconnect in 5000ms.

Upvotes: 2

Views: 6983

Answers (1)

ron
ron

Reputation: 229

Problem solved, as the Spring configurer method implies it is a stomp broker relay, it has to be by stomp protocol.

Also the transport protocol prefix apparently is not required. I also need to enter a username and password if it is set in the default activemq installation. But this was done after booting up a standalone ActiveMQ, what I am actually looking for is an embedded solution.

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

}

UPDATE

In response to one of the comments above by Deinum. I also tried just simply setting the following in my application.properties of my spring boot application:

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

But the console showed no evidence of ActiveMQ being started up, nor could I connect to it through my stomp broker relay configuration as posted above. I ended up creating a spring configuration class and it works now:

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

Upvotes: 5

Related Questions