public void testReceiveAndReply() {
this.template.setQueue(ROUTE);
this.template.setRoutingKey(ROUTE);
this.template.convertAndSend(ROUTE, "test");
boolean received = this.template.receiveAndReply(new ReceiveAndReplyMessageCallback() {
@Override
public Message handle(Message message) {
message.getMessageProperties().setHeader("foo", "bar");
return message;
}
});
assertTrue(received);
Message receive = this.template.receive();
assertEquals("bar", receive.getMessageProperties().getHeaders().get("foo"));
this.template.convertAndSend(ROUTE, 1);
received = this.template.receiveAndReply(ROUTE, new ReceiveAndReplyCallback<Integer, Integer>() {
@Override
public Integer handle(Integer payload) {
return payload + 1;
}
});
assertTrue(received);
Object result = this.template.receiveAndConvert(ROUTE);
assertTrue(result instanceof Integer);
assertEquals(2, result);
this.template.convertAndSend(ROUTE, 2);
received = this.template.receiveAndReply(ROUTE, new ReceiveAndReplyCallback<Integer, Integer>() {
@Override
public Integer handle(Integer payload) {
return payload * 2;
}
}, "", ROUTE);
assertTrue(received);
result = this.template.receiveAndConvert(ROUTE);
assertTrue(result instanceof Integer);
assertEquals(4, result);
received = this.template.receiveAndReply(new ReceiveAndReplyMessageCallback() {
@Override
public Message handle(Message message) {
return message;
}
});
assertFalse(received);
this.template.convertAndSend(ROUTE, "test");
received = this.template.receiveAndReply(new ReceiveAndReplyMessageCallback() {
@Override
public Message handle(Message message) {
return null;
}
});
assertTrue(received);
result = this.template.receive();
assertNull(result);
this.template.convertAndSend(ROUTE, "TEST");
received = this.template.receiveAndReply(new ReceiveAndReplyMessageCallback() {
@Override
public Message handle(Message message) {
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(message.getMessageProperties().getContentType());
messageProperties.setHeader("testReplyTo", new Address("", ROUTE));
return new Message(message.getBody(), messageProperties);
}
}, new ReplyToAddressCallback<Message>() {
@Override
public Address getReplyToAddress(Message request, Message reply) {
return (Address) reply.getMessageProperties().getHeaders().get("testReplyTo");
}
});
assertTrue(received);
result = this.template.receiveAndConvert(ROUTE);
assertEquals("TEST", result);
assertEquals(null, template.receive(ROUTE));
template.setChannelTransacted(true);
this.template.convertAndSend(ROUTE, "TEST");
result = new TransactionTemplate(new TestTransactionManager())
.execute(new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
final AtomicReference<String> payloadReference = new AtomicReference<String>();
boolean received = template.receiveAndReply(new ReceiveAndReplyCallback<String, Void>() {
@Override
public Void handle(String payload) {
payloadReference.set(payload);
return null;
}
});
assertTrue(received);
return payloadReference.get();
}
});
assertEquals("TEST", result);
assertEquals(null, template.receive(ROUTE));
this.template.convertAndSend(ROUTE, "TEST");
try {
new TransactionTemplate(new TestTransactionManager())
.execute(new TransactionCallbackWithoutResult() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
template.receiveAndReply(new ReceiveAndReplyMessageCallback() {
@Override
public Message handle(Message message) {
return message;
}