2
votes

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.

1
You are starting the broker AFTER you start the application. That isn't going to work. Also why aren't you letting Spring to start the broker? Just add a couple of properties to the application.properties and you are done. - M. Deinum
What properties can I add specifically to the application.properties? is there a reference guide that points to this? - ron
nevermind i think i found it: docs.spring.io/spring-boot/docs/current/reference/html/…. There is a section in the example that points to activemq configuration, not sure how complete it is though - ron
Please check this link would be of great help to you. - Praveen Kumar K S
Thanks Praveen I saw this before as I was searching google but I got thrown off when it says JMS messaging. Basically my clients will be connecting to my spring application through STOMP protocol. Perhaps JMS is the format that will be used between the broker and my main application before being routed back as a stomp message to my client? Hope someone can help me make this distinction. - ron

1 Answers

5
votes

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