I am starting with Saga pattern using Spring cloud and rabbit mq. Following is the problem statement:
I call /service1 (producer) which publishes a message in rabbit mq and that message is consumed by the consumer service.
Now occurs tow cases:
Case 1: Consumer service does its part successfully.
Case 2: Consumer service fails to do its part, thus /service1 has to rollback its changes.
How does /service1 know if consumer is successful or not, so that it can send a success/failure response. Following is the project structure:
Producer:
#RestController
public class ProducerController {
private MessageChannel greet;
public ProducerController(HelloBinding binding) {
greet = binding.greeting();
}
#GetMapping("/greet/{name}")
public void publish(#PathVariable String name) {
String greeting = "Hello, "+name+"!";
Message<String> msg = MessageBuilder.withPayload(greeting)
.build();
this.greet.send(msg);
System.out.println("Message sent to the queue");
AMQP.Basic.Ack;
}
Consumer:
#EnableBinding(HelloBinding.class)
public class HelloListener {
#StreamListener(target=HelloBinding.GREETING)
public void processHelloChannelGreeting(String msg) {
System.out.println("Message received:- "+msg);
}
}
Now how do I tell the producer whether consumer's action is a success or a failure so that producer service sends appropriate response?
The producer can not know what happens after a message has been successfully published to a topic. If you want feedback from the consumer then you need to create a new "response" topic on which the consumer communicates success or failure of processing that message.
You can map the messages by keys.
Related
Below is my Message Listener which listens to Redis PUBSUB. It is working fine. I have to implement a feature in such a way that if no messages are received after threshold/specified time, then I have to send a proper message to websocket(subscription URL same as PUBSUB channel). I don't see any way to that as there will be many clients which subscribes to different channels at different times.
Any help or suggestion would be appreciable.
public class PubSubListener implements MessageListener {
#Autowired
private SimpMessagingTemplate simpMessagingTemplate ;
#Override
public void onMessage(Message message, byte[] pattern) {
logger.info(" MESSAGE RECEIVED FROM PUBSUB ");
simpMessagingTemplate.convertAndSend(new String(pattern), message.toString());
}
}
In my Spring Boot 1.5.9 application with Spring Websockets, web-socket subscriptions are intercepted with an implementation of ChannelInterceptorAdapter:
#Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = StompHelper.accessor(message);
StompCommand command = accessor.getCommand();
if (null != command) {
StompHelper.authentication(message, tokenHandler);
switch (command) {
case SUBSCRIBE:
this.stompHandler.subscribe(message);
break;
case UNSUBSCRIBE:
this.stompHandler.unsubscribe(message);
break;
// ...
}
}
return super.preSend(message, channel);
}
which captures the subscriber and destination so subscriber-specific messages can be sent. In the web-socket controller, the #SubscribeMapping implementation publishes initialization data to the subscriber. But the #SubscribeMapping and ChannelInterceptorAdapter.preSend are invoked in different threads, so occasionally the #SubscribeMapping executes before the completion of ChannelInterceptorAdapter.preSend which results in the subscriber not getting the initialization data.
This seems like a Spring Websocket design problem, but is there an (elegant-ish) work-around?
Update: submitted a bug: SPR-16323
In the Spring WebSocket docs I found this sentence:
It is important to know that a server cannot send unsolicited messages.
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html
(25.4.1)
However I tried this code:
#Controller
public class WebsocketTest {
#Autowired
public SimpMessageSendingOperations messagingTemplate;
#PostConstruct
public void init(){
ScheduledExecutorService statusTimerExecutor=Executors.newSingleThreadScheduledExecutor();
statusTimerExecutor.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
messagingTemplate.convertAndSend("/topic/greetings", new Object());
}
}, 5000,5000, TimeUnit.MILLISECONDS);
}
}
And the message is broadcasted every 5000ms as expected.
So why Spring docs says that a server cannot send unsollicited messages?
The next sentence might mean that in the stomp.js client you are required to set a subscription:
All messages from a server must be in response to a specific client
subscription
But this does not necessarily mean in response to a request. For example a web socket could send information to the following:
Javascript:
stompClient.subscribe('/return/analyze', function(data) {
generateTableData(JSON.parse(data.body));
});
Spring:
#Autowired
private SimpMessagingTemplate simpMessagingTemplate;
public void sendSetpoint(String data) throws Exception {
this.simpMessagingTemplate.convertAndSend("/return/analyze", data);
}
But it cannot send unsolicited messages to the client unless that subscription exists. If this is their intended point it is a little poorly worded.
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.
I have a rest service that sends messages to my queue, and these are routed to file:
from("test-jms:queue:test.queue").to("file://test");
Also, I have an event-driven consumer on the endpoint. For now this only writes to the log if a message is consumed:
final Consumer consumer = endpoint.createConsumer(new Processor() {
#Override
public void process(Exchange exchange) throws Exception {
String message = exchange.getIn().getBody(String.class);
LOG.info("Message processed: " + message);
}
});
This is all working fine. In the /test folder I am getting a new file for every message I recieve, and additionally the consumer creates a marker file appended with .camelLock. Using the readLock=none option prevents the consumer from making these marker files, as expected.
However, neither the message files nor the marker files are deleted after consumption. Am I perhaps missing something in my consumer implementation?
When you manually create a consumer like that with an inlined processor, you need to manually done the UoW of the Exchange when you are done to trigger work that would delete/move the file etc.
exchange.getUnitOfWork().done(exchange);
You can also try wrapping your processor with the UnitOfWorkProducer that should do the done of the UnitOfWork for you.
The key here, as Claus Ibsen pointed out, was to done the UnitOfWork (UoW). Now my event-driven consumer looks like this:
final Consumer consumer = endpoint.createConsumer(new Processor() {
#Override
public void process(Exchange exchange) throws Exception {
String message = exchange.getIn().getBody(String.class);
LOG.info("Message processed: " + message);
ConsumerTemplate consumerTemplate = camelContext.createConsumerTemplate();
consumerTemplate.doneUoW(exchange);
}
});
Also, the delete=true option must be used when creating the endpoint:
Endpoint endpoint = camelContext.getEndpoint("file://test?delete=true");