use of org.springframework.integration.acks.AcknowledgmentCallback in project spring-integration by spring-projects.
the class SourcePollingChannelAdapter method handleMessage.
@Override
protected void handleMessage(Message<?> message) {
if (this.shouldTrack) {
message = MessageHistory.write(message, this, getMessageBuilderFactory());
}
AcknowledgmentCallback ackCallback = StaticMessageHeaderAccessor.getAcknowledgmentCallback(message);
try {
this.messagingTemplate.send(getOutputChannel(), message);
AckUtils.autoAck(ackCallback);
} catch (Exception e) {
AckUtils.autoNack(ackCallback);
if (e instanceof MessagingException) {
throw (MessagingException) e;
} else {
throw new MessagingException(message, "Failed to send Message", e);
}
}
}
use of org.springframework.integration.acks.AcknowledgmentCallback in project spring-integration by spring-projects.
the class AmqpMessageSource method doReceive.
@Override
protected AbstractIntegrationMessageBuilder<Object> doReceive() {
Connection connection = this.connectionFactory.createConnection();
Channel channel = connection.createChannel(this.transacted);
try {
GetResponse resp = channel.basicGet(this.queue, false);
if (resp == null) {
RabbitUtils.closeChannel(channel);
RabbitUtils.closeConnection(connection);
return null;
}
AcknowledgmentCallback callback = this.ackCallbackFactory.createCallback(new AmqpAckInfo(connection, channel, this.transacted, resp));
MessageProperties messageProperties = this.propertiesConverter.toMessageProperties(resp.getProps(), resp.getEnvelope(), StandardCharsets.UTF_8.name());
messageProperties.setConsumerQueue(this.queue);
Map<String, Object> headers = this.headerMapper.toHeadersFromRequest(messageProperties);
org.springframework.amqp.core.Message amqpMessage = new org.springframework.amqp.core.Message(resp.getBody(), messageProperties);
Object payload = this.messageConverter.fromMessage(amqpMessage);
AbstractIntegrationMessageBuilder<Object> builder = getMessageBuilderFactory().withPayload(payload).copyHeaders(headers).setHeader(IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK, callback);
if (this.rawMessageHeader) {
builder.setHeader(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, amqpMessage);
}
return builder;
} catch (IOException e) {
RabbitUtils.closeChannel(channel);
RabbitUtils.closeConnection(connection);
throw RabbitExceptionTranslator.convertRabbitAccessException(e);
}
}
use of org.springframework.integration.acks.AcknowledgmentCallback in project spring-integration by spring-projects.
the class MessageSourcePollingTemplateTests method testNoAutoAck.
@Test
public void testNoAutoAck() {
AcknowledgmentCallback callback = mock(AcknowledgmentCallback.class);
given(callback.isAutoAck()).willReturn(false);
MessageSource<?> source = () -> new GenericMessage<>("foo", Collections.singletonMap(IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK, callback));
MessageSourcePollingTemplate template = new MessageSourcePollingTemplate(source);
template.poll(h -> {
});
verify(callback, never()).acknowledge(Status.ACCEPT);
verify(callback, never()).acknowledge(Status.REJECT);
}
use of org.springframework.integration.acks.AcknowledgmentCallback in project spring-integration by spring-projects.
the class MessageSourcePollingTemplate method poll.
@Override
public boolean poll(MessageHandler handler) {
Assert.notNull(handler, "'handler' cannot be null");
Message<?> message = this.source.receive();
if (message != null) {
AcknowledgmentCallback ackCallback = StaticMessageHeaderAccessor.getAcknowledgmentCallback(message);
try {
handler.handleMessage(message);
AckUtils.autoAck(ackCallback);
} catch (Exception e) {
AckUtils.autoNack(ackCallback);
throw new MessageHandlingException(message, e);
}
return true;
}
return false;
}
use of org.springframework.integration.acks.AcknowledgmentCallback in project spring-integration by spring-projects.
the class MessageSourcePollingTemplateTests method testAckNack.
@Test
public void testAckNack() {
AcknowledgmentCallback callback = mock(AcknowledgmentCallback.class);
given(callback.isAutoAck()).willReturn(true);
MessageSource<?> source = () -> new GenericMessage<>("foo", Collections.singletonMap(IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK, callback));
MessageSourcePollingTemplate template = new MessageSourcePollingTemplate(source);
template.poll(h -> {
});
verify(callback).acknowledge(Status.ACCEPT);
try {
template.poll(h -> {
throw new RuntimeException("expected");
});
fail("expected exception");
} catch (MessageHandlingException e) {
assertThat(e.getCause().getMessage(), equalTo("expected"));
}
verify(callback).acknowledge(Status.REJECT);
}
Aggregations