Search code examples
rabbitmqspring-amqpstompsockjs

Java Spring Stomp AMQP


I have three project:

  1. Javascript SockJS STOMP client

  2. Spring-boot STOMP endpoint and AMQP

  3. Spring-boot AMQP (RabbitListener) client for testing

    I am using RabbitMQ message broker (+Stomp plugin) and configured amqp and stomp endpoint normally..When I send message to queue with RabbitTemplate and third project (spring-boot amqp client for testing) normally subscribed this message , everything works fine !! But Javascript STOMP client didn't received this message.. P.S. When I send message with SimpMessagingTemplate , JS client receives message fine !

Javascript SockJS STOMP Client

        var socket = new SockJS('http://localhost:8090/hello');
        stompClient = Stomp.over(socket);
        stompClient.connect('guest','guest', function(frame) {
            setConnected(true);
            console.log('Connected: ' + frame);
            stompClient.subscribe('/topic/testqueue', function(greeting){
                showGreeting(JSON.parse(greeting.body).content);
            });
        });

spring-boot STOMP endpoint and AMQP

    @Controller
    public class SampleController {
        Logger logger = Logger.getLogger(SampleController.class);
    
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        private SimpMessagingTemplate messagingTemplate;
    
        @Autowired
        public SampleController(SimpMessagingTemplate messagingTemplate) {
            this.messagingTemplate = messagingTemplate;
        }
    
        @GetMapping("/emit/{message}")
        @ResponseBody
        String queue1(@PathVariable("message") String message) throws Exception {
            logger.info("Emit to testqueue");
            rabbitTemplate.convertAndSend("/topic/testqueue", new Greeting("Salam olsun " + message));
            Thread.sleep(60000); // simulated delay
            return "Emit to testqueue";
        }
}

spring-boot amqp client for testing

@Component
public class RabbitMqListener {
    Logger logger = Logger.getLogger(RabbitMqListener.class);

    @RabbitListener(queues = "/topic/testqueue")
    public void processQueue1(String message) {
        logger.info("Received from queue : " + message);
    }
}

How I can mix amqp and stomp protocols in RabbitMQ ? I want to send message from another project with amqp protocol (RabbitTemplate) and receive this message from JS STOMP client (SockJS) .. Thanks.


Solution

  • I was changed rabbitTemplate.convertAndSend("/topic/testqueue", ...) to rabbitTemplate.convertAndSend("amq.topic","testqueue" ...) and everythink works fine ))) Especially thanks to Artem Bilan for support. Good Luck