Spring SimpMessagingTemplate

1.7k views Asked by At

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 ?

1

There are 1 answers

0
Lukasz Ciesluk On

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.