Spring Integration: sending response to temp JMS queue - jms

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.

Related

Create thread and route message from Camel to microservice and back

I'm using Camel with JMS and Spring Boot and would like to build a route for next scenario:
User 1 (MQTT client) sends a message (topic) to ActiveMQ Artemis.
Camel (by using from) catch that message and print it out with a help of log.
Thing I would like to do is - create a new thread (asynchronous) for caught message. Send that message from Camel to microservice (python program) that should take message and insert some extra strings, and then send changed message back to Camel and ActiveMQ.
On the end, from ActiveMQ changed message will be sent to User 2.
Can you give me some directions or route examples of how to do something like that?
So, key points are to create new thread for every message and create route to and back from that microservice.
The route could look like this
from("jms:queue:inputQueue")
.log("${body}")
.to("http://oldhost")
.to("jms:queue:outputQueue")
Some notes:
You can call a downstream HTTP endpoint with .to(). The current message body is used as request body. The response of the service overwrites the message body.
I don't know why you want to create a new thread for a synchronous call. You can leverage parallel processing by consuming multiple messages from Artemis in parallel with multiple consumers. Like this, every message is processed in its own thread. If your motivation is resilience, there is also a Circuit Breaker EIP in Camel
If you use Camel 2.x, use the HTTP4 Component ("http4://") to use the newer HTTP client lib. In Camel 3.x the old one was dropped and the new one is simply called HTTP component

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.

JMS with Spring Integration or Spring Batch

Our project is to integrate two applications, using the REST API of each and using JMS (to provide asynchronous nature). Application-1 writes the message on the queue. The next step is to read the message from the queue, process it, and send it to application2.
I have two questions:
Should we use one more queue for storing messages after processing and before sending them to application2?
Should we use spring batch or spring integration to read/process the data?
Or you don't show the whole premise, or you really try to overhead your app. If there is just need to read messages from the queue, there is just enough to use Spring JMS directly... From other side with the Spring Integration and its Adapters power you can just process messes from the <int-jms:message-driven-channel-adapter> to the <int-http:outbound-channel-adapter>.
Don't see reason to store message somewhere else in the reading and sending process. Just because with some exception here you just rollback your message to the JMS queue back.

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.

JMS request/reply pattern in grails

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

Resources