1

I have an application which receive some data from RabbitMQ. Everything works fine, I mean in class where I have annotation @EnableScheduling.

@Scheduled(fixedDelay = 5000)
public void volumeGraphData() {

    Random r = new Random();

    Graph graph = new Graph();
    graph.setVolume(r.nextInt(500));
    String json = gson.toJson(graph);

    MessageBuilder<byte[]> messageBuilder = MessageBuilder.withPayload(json.getBytes());
    simpMessagingTemplate.send("/" + volumeGraph, messageBuilder.build());
}

But when I would like to process messages received by Queue Listener from RabbitMQ (this works too) and pass them through to specific context for Stomp WebSocket using SimpMessagingTemplate I cannot do that. SimpMessagingTemplate is defined in dispatcher-servlet.xml, but configuration related with RabbitMQ is in root context. I tried to move everything to one context, but it does not work. Anyone has similar case that one I have ?

Lukasz Ciesluk
  • 718
  • 1
  • 17
  • 29
  • "it does not work" is no help at all; you need to describe symptoms. You should be able to simply move the `SimpMessagingTemplate` to the root context. – Gary Russell Jun 09 '15 at 14:36
  • Yes, you are right ;) I mean that I am not able to pass my message received from RabbitMQ to one of the Stomp Endpoint to see that message from Frontend. WebSocket configuration will be in the DispatcherServlet context and it's not possible to inject the SimpMessagingTemplate into beans in the root context. – Lukasz Ciesluk Jun 09 '15 at 14:39
  • I must to add that I moved some bean from disaptcher-servlet.xml to root context and this flow finally works. I mean that Queue Listener receives message from RabbitMQ, then send it over to class where SimpMessagingTemplate is annotated. Message was sent and ... on frontend nothing happened.... – Lukasz Ciesluk Jun 09 '15 at 17:40

1 Answers1

1

I finally managed to fix this. So, basically you need move your beans related with Spring Messaging/WebSocket to one common bean.

That's why in my root context I have such lines :

<!-- Fix for IntelliJ 13.1 https://youtrack.jetbrains.com/issue/IDEA-123964 -->
<context:component-scan base-package="org.springframework.web.socket.config"/>

where in package pl.garciapl.program.service.config is located class responsible for configuration of WebSockets :

@Configuration
@EnableWebSocketMessageBroker
@Component("messageBroker")
public class MessageBrokerConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        stompEndpointRegistry.addEndpoint("/test").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry messageBrokerRegistry) {
    }

    @Override
    public void configureClientInboundChannel(ChannelRegistration channelRegistration) {
    }

    @Override
    public void configureClientOutboundChannel(ChannelRegistration channelRegistration) {
    }

    @Override
    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
        messageConverters.add(new MappingJackson2MessageConverter());
        return false;
    }
}

Remember to store your beans which use SimpMessagingTemplate in the same context where you defined this above class.

Lukasz Ciesluk
  • 718
  • 1
  • 17
  • 29