1
votes

I have a simple distributable chat application. It works with MySQL backend exposed thru REST API servlets. Client is written in HTML / JS using websockets with SockJS and STOMP protocol connecting to RabbitMQ server. I would like to expose RabbitMQ server also thru the java backend, is there a way to do it with a servlet?

I found some tutorials how to do this with Spring, but I don't want to rewrite the whole application just because of this. Maybe there is some way to use only Spring AMQP I'm missing...

I managed to create SockJS servlet endpoint using https://github.com/projectodd/sockjs-servlet, but the server endpoint is build once when deploying application, when I don't know anything. I would like to build endpoint based on URL (e.g. localhost/ws/room-4 to create endpoint for ROOM id#4, where you can listen for incoming messages).

1
So, you want avoid to use Rabbitmq directly, and use a java back-end application, is that right? - Gabriele Santomaggio
Yes, I don't want to expose RabbitMQ to client application. I would like to listen on RabbitMQ inside java backend and send it to client. I managed to do this with the SockJS servlet linked above, but like I said, I need to create those endpoints based on user requests, not just once when deploying application. - Martin Adámek

1 Answers

1
votes

I think it is right to use RMQ through java back end application. In this way you could remove STOMP plug-in from rabbitmq

Now, as you read around it is possible to do that easily using spring-framework because it contains http://projects.spring.io/spring-amqp/ and also contains lot of features can help you, such as: DeferredResult please read: https://spring.io/blog/2012/05/07/spring-mvc-3-2-preview-introducing-servlet-3-async-support I really suggest to read about Spring.

Anyway, if you want to do that manually:

Suppose you have a function “create room” like yourservlet/createroom= “room4” when you have this “get” you can create anonymous and autodelete queue and bound it to exchange whit the routingkey=”room4”, then just redirect the messages consumed for this queue to all websocket connected with the same key.

For example if you want to use tomcat websocket (here an example https://gist.github.com/chitan/3063774 ) you can do something like that:

 private class MyMessageInbound extends MessageInbound{
        WsOutbound myoutbound;
        String rouutKey; 

You register the websocket with the same routing-key. (routing-key can be the roomName)

I used a similar situation on my project (not chat but similar).

Hope it helps