Search in sources :

Example 1 with AcknowledgmentCallback

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);
        }
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) AcknowledgmentCallback(org.springframework.integration.acks.AcknowledgmentCallback) MessagingException(org.springframework.messaging.MessagingException) BeanCreationException(org.springframework.beans.factory.BeanCreationException)

Example 2 with AcknowledgmentCallback

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);
    }
}
Also used : Channel(com.rabbitmq.client.Channel) Connection(org.springframework.amqp.rabbit.connection.Connection) AcknowledgmentCallback(org.springframework.integration.acks.AcknowledgmentCallback) IOException(java.io.IOException) GetResponse(com.rabbitmq.client.GetResponse) MessageProperties(org.springframework.amqp.core.MessageProperties)

Example 3 with AcknowledgmentCallback

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);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) AcknowledgmentCallback(org.springframework.integration.acks.AcknowledgmentCallback) Test(org.junit.Test)

Example 4 with AcknowledgmentCallback

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;
}
Also used : AcknowledgmentCallback(org.springframework.integration.acks.AcknowledgmentCallback) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 5 with AcknowledgmentCallback

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);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) AcknowledgmentCallback(org.springframework.integration.acks.AcknowledgmentCallback) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Aggregations

AcknowledgmentCallback (org.springframework.integration.acks.AcknowledgmentCallback)5 Test (org.junit.Test)2 MessageHandlingException (org.springframework.messaging.MessageHandlingException)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 Channel (com.rabbitmq.client.Channel)1 GetResponse (com.rabbitmq.client.GetResponse)1 IOException (java.io.IOException)1 MessageProperties (org.springframework.amqp.core.MessageProperties)1 Connection (org.springframework.amqp.rabbit.connection.Connection)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 MessagingException (org.springframework.messaging.MessagingException)1