I am publishing messages to request queue and getting those messages in reply queue. Here i want to further process the all received messages, so i want to add all the messages to List before sending to my processor(To do further operations). Below is my code.
@Autowired
private GeneralProcess generalProcess;
List <RequestPojo> requestPojoGeneral = new ArrayList<RequestPojo>();
@RabbitHandler
@RabbitListener(containerFactory = "simpleMessageListenerContainerFactory", queues ="BulkSolve_GeneralrequestQueue")
public void subscribeToRequestQueue(@Payload RequestPojo sampleRequestMessage, Message message) throws InterruptedException {
requestPojoGeneral.add(sampleRequestMessage);
System.out.println("List size issssss:" +requestPojoGeneral.size() );
generalProcess.processRequestObjectslist(requestPojoGeneral);
/*System.out.println("message in general listener is:" + sampleRequestMessage.getDistance());
System.out.println("Message payload is:" + sampleRequestMessage);
System.out.println("Message payload1111 is:" + message );*/
}
The above code i am unable to get all messages at a time. I have to wait untill i get all the messages and populate list before calling generalProcess.processRequestObjectslist(requestPojoGeneral); .Can any one suggest best way to wait calling processor method until get all messages and add to list.
Thanks.
You can add a header to the messages eg messageCount
.
Then
if ((int) message.getMessageProperties().getHeader("messageCount") == list.size() {
process();
}
(Or some other technique such as adding a header "lastMessage=true").
Bear in mind, if your server crashes before you get the full list, you will lose messages unless you set the acknowledgeMode to MANUAL
.
Then, after you have processed all the messages, call basicAck
on the Channel for the last message.
You can get access to the Channel
as a method parameter and the deliveryTag is in the MessageProperties
.
The container prefetchCount
must be at least as big as your biggest batch because the broker will only allow that number of not-ack'd messages outstanding.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.