JMS request/reply pattern in grails - spring

I am creating a grails web app which makes use of JMS messaging. I have installed the JMS plugin for grails and using activemq as the messaging provider. I want to implement a request/response pattern in grails.
I was successfully able to send a message to the queue using the
sendQueueJMSMessage("queueName",Map message) from a controller.
I then created a service which contains the onMessage() method that listens to the "queueName" as stated above.
The onMessage() method does some processing and successfully sends an email to the user.
The above scenario has been implemented successfully.
Now, I would like to receive a response from this onMessage() method.
Lets say I want to implement the below scenario.
The request is added to the queue and waits for a response. I looked around but I couldnt find any help.
Please give me a lead on this. I really appreciate it.

Spring JMS adds support for auto replies, which the Grails plugin supports. See: http://gpc.github.com/grails-jms/docs/manual/guide/5.%20Receiving%20Messages.html#5.3%20Listener%20Return%20Values
Here is a test exercising this stuff: https://github.com/gpc/grails-jms/blob/master/test/integration/grails/plugin/jms/test/reply/ReplyingListenerServiceSpec.groovy#L12
The other option is to just send another message from your first message receiving method.

you did not provide much to work with here so the my suggestion is to take a look at the samples in the Grails JMS Plugin - Reference Documentation: 5. Receiving Messages, or provide some code

Related

jms queue receiver which always run in backgrond

I want a Java class (jms receiver) which always run in background and check for any message arrive in jms queue.How it can be possible? Please help.
What you want is a JMS MessageListener. Create your JMS client resources (e.g. connection/context, session, etc.) like normal and then invoke setMessageListener on your consumer passing in your implementation of the MessageListener interface. This is pretty basic JMS stuff so you can find more detailed tutorials online.

Camel routes from activemq to rest endpoint

I am trying to use Spring Boot 1.5.2.RELEASE + Camel (Spring Boot starter) 2.19.2 to listen to ActiveMQ queue and then post the message to a rest endpoint URL (POST method) as its body. What would be the best possible way to achieve this?
I have gathered pieces of information and am trying to tie it all together but getting a bit confused.
Here is what I have gathered for Camel Rest DSL, I am not too sure if camel below is creating these rest services via this or is it just an already exposed endpoint, in my case it is an already exposed endpoint
rest("/basePath")
post("/someEndpoint").to("direct:restEndpoint")
Using the above is what I have gathered for ActiveMQ which I am not too sure is correct
from("activemq:queue:<queue_name>").to("direct:restEndpoint")
But again, I am not too sure how to listen to the ActiveMQ queue for new messages or is it something that Camel would do by default always? Additionally, I need to pass the message as a post body to my rest endpoint. I also saw some references to camel-http4 and camel-http as well and I am completely confused.
Any assistance would be greatly appreciated.
Some confusion is common when starting to use Camel, but your final solution will look something like:
from("activemq:queue:my-route")
.process(/* change the in/out messages if you need to */)
.to("http4://your-endpoint.com");
Don't try to simply copy/paste this code until it works. My Camel rule of thumb is: always read the component documentation and try playing with it using it in your software. In your case I suggest:
Read ActiveMQ component docs and try reading from ActiveMQ / writing to a Log;
Generate some input from a Timer and send to your Rest endpoint using HTTP4 Component;
Your first routes will take some time for simple things but you will get on flow quickly.

Spring-boot app displaying JMS messages on WebPage via WebSocket

I need a simple web-app in spring-boot that listens for messages on a JMS queue and when arriving it should appear on a webpage via WebSocket.
I have searched for examples and found several individual; either WebSocket or JMS which I have tested on their own but have not succeeded in wiring it together.
I have searched for an example but not found any and in my mind I think it should be pretty easy since it's a very basic requirement.
Do you know about any example with JMS and HTML display via WebSocket that you can share or can give some hints or help for me to solve it?
The Spring Integration comes to the rescue.
You can write <int-jms:message-driven-channel-adapter> to read messages from JMS queue and forward them to the <int-websocket:outbound-channel-adapter>. Where the last one just sends messages to the connected WebSocket session(s).
See these Spring Integration samples on the matter:
https://github.com/spring-projects/spring-integration-samples/tree/master/basic/jms
https://github.com/spring-projects/spring-integration-samples/tree/master/basic/web-sockets
UPDATE
To send the message to all subscribed WebSocket session you should do something like this:
<int:splitter input-channel="enricheMessage" output-channel="sendMessage" apply-sequence="false">
<int-groovy:script>
#serverWebSocketContainer.sessions.keySet().collect {
org.springframework.integration.support.MessageBuilder.withPayload(payload)
.copyHeaders(headers)
.setHeader('simpSessionId', it)
.build()
}
</int-groovy:script>
</int:splitter>
With this Groovy script I retrieve session ids from the serverWebSocketContainer (all those connected clients), iterate over them to build messages to send them over their websocket. And split finally, to send to the <int-websocket:outbound-channel-adapter> one by one.

Pattern for handeling multiple message types with spring JMS in a multi module maven setup

I' m currently investigation the implementation of queue's or topic, based on activeMQ within our project. The setup is pretty straight forward in which we are using maven modules to seprate business logic according to the business domain. One common module allows us to assemble common logic.
simplified Example:
common module
products module
clients module
One of the requirements is that certain operations are asynchronous towards a backend (by means of a activeMQ) which in turn responds with a result message.
The second requirement is that it should be possible to horizontally scale the application by creating a new deployable artifact with only that module that needs more juice.
We are using spring 4 with of course jms and activeMQ.
On to my question. We would like to use just one queue or topic for backend connectivity. Which would mean our common module would handle jms configuration (jms factory, jms-configuration) and different types of messages will be send over that one queue/topic.
How would can I make sure that product related messages get handled by the "products" module and client related messages get handled by the "clients" module? How can I make sure only one of the "products" modules logic would handle a message if the module was deployed two times? What approach would you recommend or is this one queue/topic "nuts"?
I was myself thinking in the direction of using a topic because of the publ/subsc pattern... , or maybe queue listener acting as publisher in a observer pattern to which product- or client subscribers might subscribe to take over handling the message?
Thanks for your help.
Why using a Topic if you want just one of the "products" modules to handle one message? With Topic you'd have one message - multiple subscribers each getting the message. The Queue ensures only one consumer gets a message (point-to-point), with Topic you'd have one message dispatched to all subscribers.
Regarding the "filtering" of messages, this should be something provider specific. Looking at ActiveMQ docs I see this. So, basically each consumer should get the messages the broker, based on selector, will dispatch. I don't know the specifics, but this would be the first place I would start this investigation.
On the same idea, have a look at this discussion from the Spring forum. It is, indeed, related to Spring Integration, but it's on the same idea of message selection. On the other hand, you could consider adopting Spring Integration in your project: it's kind of an abstraction of integration patterns and fits well with everything message oriented.
Few interesting ideas from that forum post:
JmsTemplate from Spring contains a doReceive method that takes as a parameter a String as message selector
Message selectors are in the JMS spec (scroll down to "Message Properties" section)
Relevant section in the Spring Integration documentation is here

Spring Integration: sending response to temp JMS queue

I am using Spring Integrations 2.0.0 and am trying to configure the following:
An application places an object on an ActiveMQ JMS queue which is processed by the Spring-integration-driven backend. The sending application is awaiting a reply on a temporary response queue since the backend places the response in this particular queue to be consumed by this application only. The message is received and processed in the backend and the response shall be placed on the temp response queue given in the JMS request message (reply-to). In the backend the message is running through a chain of services before the response is eventually placed on the response queue.
Question: how do I configure SI to place the response to the temp queues? Is there any way that SI does this automatically or do I need to use DestinationResolver or things like that? I understood that the jms-gateway might be the right solution for this but couldn't quite figure out how to put it in place. Any ideas?
I think you're referring to the online documentation for SI that still points to version 1.0.x. If you instead download the spring integration zip and unpack you will find the latest documentation.
What you're looking for is described in chapter 17.
If things are still unclear, then please create a documentation issue so we can tackle this for 2.0.0.RELEASE.

Resources