Use #SentTo to send a message with Spring Boot and RabbitMq - java

Is it possible to send a return value of any method to a queue using an annotation, like
#SentTo("my.queue.name")
String send() {
return myString;
}
Do I definitely need a #RabbitListener to use #SendTo? Maybe another way out?
I'm trying to simplify my code.

#SendTo is only currently for replies from a #RabbitListener where the sender didn't set a replyTo header.
You could do what you want with a Spring Integration #Publisher annotation with its channel wired to a rabbitmq outbound channel adapter...
#Publisher(channel = "amqpOutboundChannel")
public String send() {
return myString;
}
#Bean
#ServiceActivator(inputChannel = "amqpOutboundChannel")
public AmqpOutboundEndpoint amqpOutbound(AmqpTemplate amqpTemplate) {
AmqpOutboundEndpoint outbound = new AmqpOutboundEndpoint(amqpTemplate);
outbound.setRoutingKey("my.queue.name"); // default exchange - route to queue 'my.queue.name'
return outbound;
}
The method has to be public and invoked from outside the bean itself.

Related

Spring Integration: How to send messages from pubsub subscribers to external systems/servers with Http Methods

I have been trying to send messages to external systems(using rest template POST, PUT etc) from the service activators as below.
Below is my pubsub consumer class
public class MyConsumer{
#Autowired
ExternalService externalService;
#Bean
public PubSubInboundChannelAdapter messageChannelAdapter(final #Qualifier("myInputChannel") MessageChannel inputChannel,
PubSubTemplate pubSubTemplate)
{
PubSubInboundChannelAdapter adapter = new PubSubInboundChannelAdapter(pubSubTemplate, pubSubSubscriptionName);
adapter.setOutputChannel(inputChannel);
adapter.setAckMode(AckMode.AUTO_ACK);
adapter.setErrorChannelName("pubsubErrors");
return adapter;
}
#ServiceActivator(inputChannel = "pubsubErrors")
public void pubsubErrorHandler(Message<MessagingException> exceptionMessage) {
BasicAcknowledgeablePubsubMessage originalMessage = (BasicAcknowledgeablePubsubMessage) exceptionMessage
.getPayload().getFailedMessage().getHeaders().get(GcpPubSubHeaders.ORIGINAL_MESSAGE);
originalMessage.nack();
}
#Bean
public MessageChannel myInputChannel() {
return new DirectChannel();
}
#Bean
#ServiceActivator(inputChannel = "myInputChannel")
public MessageHandler messageReceiver_AddCustomer() {
return message -> {
externalService.postDataTOExternalSystems(new String((byte[]) message.getPayload());
};
}
#Bean
#ServiceActivator(inputChannel = "myInputChannel")
public MessageHandler messageReceiver_DeleteCustomer() {
return message -> {
externalService.deleteCustomer(new String((byte[]) message.getPayload());
BasicAcknowledgeablePubsubMessage originalMessage =
message.getHeaders().get(GcpPubSubHeaders.ORIGINAL_MESSAGE, BasicAcknowledgeablePubsubMessage.class);
originalMessage.ack();
};
}
}
ExternalService below is the service which sends data to the external systems.
public class ExternalService{
void postDataTOExternalSystems(Object obj){
// RequestEntity object formed with HttpEntity object using obj(in json) and headers
restTemplate.exchange("https://externalsystems/",HttpMethod.POST,requestEntity,Object.class);
}
void deleteDatafromExternalSystems(Object obj){
// RequestEntity object formed with HttpEntity object using obj(in json) and headers
restTemplate.exchange("https://externalsystems/",HttpMethod.Detele,requestEntity,Object.class);
}
}
Since both the methods messageReceiver_AddCustomer and messageReceiver_deleteCustomer are using same channel whats happening is when I try to just addcustomer, the deleteCustomer is also called by default.
I was thinking of creating a seperate channel for deleteCustomer, but creating in this way leads to creating channels for every usecase.
Hence would like to know three things here.
Is there is any other approach of sending through Spring integration through which I can send data to external systems using a single Channel or a different utilization of Channels.
If any error in the external service calls leads to unending of failure logs in the console
message_id: "6830962001745961"
publish_time {
seconds: 1675783352
nanos: 547000000
}
}, timestamp=1675783353720}]': error occurred in message handler
It's not clear what is your expectation for such a logic. You have two contradicting subscribers for the same input channel. It sounds more like you need a router first to determine where to proceed next with an input message from Pub/Sub: https://docs.spring.io/spring-integration/reference/html/message-routing.html#messaging-routing-chapter.
but creating in this way leads to creating channels for every usecase.
Sure! You can go without Spring Integration and just do everything with the plain if..else. So, what's a difference? Therefore I don't see a reasonable argument in your statement. Yo have different HTTP methods, and that is OK to map them via Spring Integration router to respective sub-flows. The MessageChannel is really first-class citizen in Spring Integration. It makes a solution as loosely-coupled as possible.
Several subscribers on your current DirectChannel will lead to a round-robin logic by default, so you'll be surprised that one message from Pub/Sub creates a customer, another deletes and so on in rounds. The PublishSubscribeChannel will make it bad as well: both of your subscribers are going to be called, so created first, then deleted immediately.

How to receive message from Spring Integration using Direct Channel

I have created DirectChannel and have sent an object to my channel and want to receive it abck to store it in DB and send it in another service bus queue. Can you suggest how to receive the object from channel?
My Channel -
#Bean("tempChannel")
public MessageChannel tempChannel() {
return new DirectChannel();
}
Integration flow -
#Bean
public IntegrationFlow tempMessageFlow() {
return IntegrationFlows.from("tempChannel").handle().get();
}
For handle method I need to pass MessageHandler, how to I declare it and pass here?
I am sending message to channel using below piece of code, please do tell if this is alright-
tempChannel().send(messageObj);
The DirectChannel implements a SubscribableChannel. So, to get messages sent to this channel you need to subscribe(MessageHandler handler). What you have so far with that IntegrationFlow definition is OK: adding that handle() you subscribe to the tempChannel. Just handle message and forget you can do this:
.handle(m - > System.out.println("Processed message: " + m))
This is a lambda for that MessageHandler functional interface. There are many other handle() variants for other use-cases. For example process-n-reply is like this:
.handle((p, h) - > {
System.out.println("Processed message: " + m);
return "My new payload";
})
If you say that you need to do several operations on the same message, then look into a PublishSubscribeChannel. In Java DSL we have a publishSubscribeChannel(Consumer<PublishSubscribeSpec> publishSubscribeChannelConfigurer) to configure several subscribers as sub-flows.
To store into DB, you can use a JdbcMessageHandler: https://docs.spring.io/spring-integration/docs/current/reference/html/jdbc.html#jdbc-outbound-channel-adapter

How to create an asynchronous singleton socket server with spring-integration?

I want to achieve the following using spring-integration: having a singleton open socket that constantly receives and writes data, asyncrhon!
This means I have to open a socket that constantly reads from the single socket, dispatches each message for async processing, and return the responses over the socket also async.
How can I achieve that asynchron pattern?
Especially: how can I use Serializer/Deserializer? As far as I understood, a serializer is only invoked on a new socket connection, so in my case only once at start of the first message?
#Configuration
public class SocketConfig {
#Bean
public TcpConnectionFactoryFactoryBean tcpFactory(MyConverter converter) {
TcpConnectionFactoryFactoryBean fact = new TcpConnectionFactoryFactoryBean();
fact.setType("server");
fact.setPort(PORT);
fact.setUsingNio(true); //should I use true or false?
fact.setSingleUse(false); //keep socket constantly open
fact.setSerializer(converter);
fact.setDeserializer(converter);
return fact;
}
#Bean
public TcpInboundGateway serverGateway(
#Qualifier("tcpFactory") TcpConnectionFactoryFactoryBean factory,
#Qualifier("serverChannel") MessageChannel serverChannel) throws Exception {
TcpInboundGateway g = new TcpInboundGateway();
g.setConnectionFactory(factory.getObject());
g.setRequestChannel(serverChannel);
return g;
}
}
#MessageEndpoint
public class SocketEndpoint {
#ServiceActivator(inputChannel = "serverChannel")
public Object run(Object obj) {
}
}
#Service
public class MyConverter implements Serializer<Object>, Deserializer<Object> {
//read from socket
#Override
public Object deserialize(InputStream inputStream) {
}
//send back to socket
#Override
public void serialize(Object message, OutputStream outputStream) {
}
}
A gateway is used for individual request/response pairs.
If you need to send multiple responses for a single request, you must use collaborating channel adapters as described in the documentation.
Collaborating adapters can also be used (server-side or client-side) for totally asynchronous communication (rather than with request/reply semantics).
On the server side, care must be taken to populate the ip_connectionId header because it is used to correlate the message to a connection. Messages that originate at the inbound adapter will automatically have the header set. If you wish to construct other messages to send, you will need to set the header. The header value can be captured from an incoming message.

Spring Integration - Get info from 3rd party services using replyChannel

Im new to Spring Integration, I have to get a list of online agents from 3rd party web services, i tried to configure spring integration to get it, but for the channel part, i not really sure how to configure it.
My original configuration was the following, i copied from a sample that use to send request to 3rd party web services:
public interface WebServiceGateway {
#Gateway(requestChannel = "getStatusChannel")
public String getStatus(String var); <------ being forced to send something
}
In my integration configuration,
#Configuration
public class IntegrationConfiguration {
#Bean
public MessageChannel getStatusChannel() {
return MessageChannels.direct().get();
}
}
The problem is, im not sending any parameter to the webservices, in requestChannel it force me to do so, so i modified the gateway part:
public interface WebServiceGateway {
#Gateway(replyChannel = "getStatusChannel")
public String getStatus();
}
This part remains unchanged:
#Configuration
public class IntegrationConfiguration {
#Bean
public MessageChannel getStatusChannel() {
return MessageChannels.direct().get();
}
}
It prompted me java.lang.IllegalStateException: receive is not supported, because no pollable reply channel has been configured, why can't i use MessageChannel as the reply channel? How should i configure the IntegrationConfiguration?
Please go through this https://spring.io/blog/2014/11/25/spring-integration-java-dsl-line-by-line-tutorial
All you need is to define an IntegrationFlow like below:
IntegrationFlows.from(requestchannel())
.handle("requestHandler","handleInput")
.channel(replyChannel())
.get();

Reply-To in SpringAMQP being set beforehand?

I am using SpringBoot to start a SpringAMQP application that connect to RabbitMQ queues. I would like to be able to send a message from the producer, specifying the reply-queue so that the consumer would only need to send without having to investigate the destination (hence not having to pass the reply data in the message itself).
this is the configuration I have (shared between producer and consumer)
private static final String QUEUE_NAME = "testQueue";
private static final String ROUTING_KEY = QUEUE_NAME;
public static final String REPLY_QUEUE = "replyQueue";
private static final String USERNAME = "guest";
private static final String PASSWORD = "guest";
private static final String IP = "localhost";
private static final String VHOST = "/";
private static final int PORT = 5672;
#Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
amqpAdmin().declareQueue(new Queue(QUEUE_NAME));
amqpAdmin().declareQueue(new Queue(REPLY_QUEUE));
return template;
}
#Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
#Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(IP);
connectionFactory.setUsername(USERNAME);
connectionFactory.setPassword(PASSWORD);
connectionFactory.setVirtualHost(VHOST);
connectionFactory.setPort(PORT);
return connectionFactory;
}
I am sending a message as follows :
public Object sendAndReply(String queue, String content){
return template.convertSendAndReceive(queue, new Data(content), new MessagePostProcessor() {
#Override
public Message postProcessMessage(Message message) throws AmqpException {
message.getMessageProperties().setReplyTo(ReplyTester.REPLY_QUEUE);
return message;
}
});
}
and awaiting a reply as follows:
public void replyToQueue(String queue){
template.receiveAndReply(queue, new ReceiveAndReplyCallback<Data, Data>() {
#Override
public Data handle(Data payload) {
System.out.println("Received: "+payload.toString());
return new Data("This is a reply for: "+payload.toString());
}
});
}
When sending however, I get the following exception:
Exception in thread "main" org.springframework.amqp.UncategorizedAmqpException: java.lang.IllegalArgumentException: Send-and-receive methods can only be used if the Message does not already have a replyTo property.
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:66)
at org.springframework.amqp.rabbit.connection.RabbitAccessor.convertRabbitAccessException(RabbitAccessor.java:112)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:841)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:820)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doSendAndReceiveWithTemporary(RabbitTemplate.java:705)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doSendAndReceive(RabbitTemplate.java:697)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertSendAndReceive(RabbitTemplate.java:673)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertSendAndReceive(RabbitTemplate.java:663)
at prodsend.Prod.sendAndReply(ReplyTester.java:137)
at prodsend.ReplyTester.sendMessages(ReplyTester.java:49)
at prodsend.ReplyTester.main(ReplyTester.java:102)
Caused by: java.lang.IllegalArgumentException: Send-and-receive methods can only be used if the Message does not already have a replyTo property.
at org.springframework.util.Assert.isNull(Assert.java:89)
at org.springframework.amqp.rabbit.core.RabbitTemplate$6.doInRabbit(RabbitTemplate.java:711)
at org.springframework.amqp.rabbit.core.RabbitTemplate$6.doInRabbit(RabbitTemplate.java:705)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:835)
... 8 more
the line ReplyTest.137 points to the return line in the sendAndReply method above.
EDIT:
Here is the Data class that is mentioned above :)
class Data{
public String d;
public Data(String s){ d = s; }
public String toString() { return d; }
}
From the documentation:
Basic RPC pattern. Send a message to a default exchange with a specific routing key and attempt to receive a response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
So the method convertSendAndReceive handles setting the replyTo header and returns a Messaage - the response. This is a synchronous pattern - RPC.
If you want to do this asynchronously - which you seem to - do not use this method. Use the appropriate convertAndSend method and use the appropriate MessagePostProcessor to add your replyTo header.
As this is asynchronous, you need to register a separate handler for receiving the reply. This needs to be done before sending the message to the other party. This handler will then be called at some point after sending the message - when is unknown. Read section 3.5.2 Asynchronous Consumer of the Spring AQMP Documentation.
So, asynchronous process flow:
sender registers a handler on replyTo queueue
sender sends message with replyTo set
client calls receiveAndReply, processes the message, and sends a reply to the replyTo
sender callback method is triggered
The synchronous process flow is:
sender sends message using sendAndReceive and blocks
client calls receiveAndReply, processes the message, and sends a reply to the replyTo
sender receives the reply, wakes and processes it
So the latter case requires the sender to wait. As you are using receiveXXX rather than registering asynchronous handlers, the sender could be waiting a very long time if the client takes a while to get around to calling receiveXXX.
Incidentally, if you want to use the synchronous approach but use a specific replyTo you can always call setReplyQueue. There is also a setReplyTimeout for the case I mention where the client either doesn't bother to read the message or forgets to reply.

Categories