Search in sources :

Example 1 with Connection

use of org.springframework.amqp.rabbit.connection.Connection in project spring-integration by spring-projects.

the class AmqpOutboundChannelAdapterParserTests method testInt2773WithOverrideToDefaultAmqpTemplateExchangeAndRoutingLey.

@Test
public void testInt2773WithOverrideToDefaultAmqpTemplateExchangeAndRoutingLey() throws IOException {
    ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);
    when(connectionFactory.createConnection()).thenReturn(mockConnection);
    PublisherCallbackChannelImpl publisherCallbackChannel = new PublisherCallbackChannelImpl(mockChannel);
    when(mockConnection.createChannel(false)).thenReturn(publisherCallbackChannel);
    MessageChannel requestChannel = context.getBean("overrideTemplateAttributesToEmpty", MessageChannel.class);
    requestChannel.send(MessageBuilder.withPayload("test").build());
    Mockito.verify(mockChannel, Mockito.times(1)).basicPublish(Mockito.eq(""), Mockito.eq(""), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class));
}
Also used : ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageChannel(org.springframework.messaging.MessageChannel) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) NullChannel(org.springframework.integration.channel.NullChannel) MessageChannel(org.springframework.messaging.MessageChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) Connection(org.springframework.amqp.rabbit.connection.Connection) PublisherCallbackChannelImpl(org.springframework.amqp.rabbit.support.PublisherCallbackChannelImpl) Test(org.junit.Test)

Example 2 with Connection

use of org.springframework.amqp.rabbit.connection.Connection 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 Connection

use of org.springframework.amqp.rabbit.connection.Connection in project spring-integration by spring-projects.

the class AbstractAmqpOutboundEndpoint method start.

@Override
public synchronized void start() {
    if (!this.running) {
        if (!this.lazyConnect && this.connectionFactory != null) {
            try {
                // NOSONAR (close)
                Connection connection = this.connectionFactory.createConnection();
                if (connection != null) {
                    connection.close();
                }
            } catch (RuntimeException e) {
                logger.error("Failed to eagerly establish the connection.", e);
            }
        }
        doStart();
        this.running = true;
    }
}
Also used : Connection(org.springframework.amqp.rabbit.connection.Connection)

Example 4 with Connection

use of org.springframework.amqp.rabbit.connection.Connection in project spring-integration by spring-projects.

the class DispatcherHasNoSubscribersTests method testPubSub.

@Test
public void testPubSub() {
    final Channel channel = mock(Channel.class);
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpTemplate amqpTemplate = mock(AmqpTemplate.class);
    PublishSubscribeAmqpChannel amqpChannel = new PublishSubscribeAmqpChannel("noSubscribersChannel", container, amqpTemplate);
    amqpChannel.setBeanName("noSubscribersChannel");
    amqpChannel.setBeanFactory(mock(BeanFactory.class));
    amqpChannel.afterPropertiesSet();
    List<String> logList = insertMockLoggerInListener(amqpChannel);
    MessageListener listener = (MessageListener) container.getMessageListener();
    listener.onMessage(new Message("Hello world!".getBytes(), null));
    verifyLogReceived(logList);
}
Also used : ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) Channel(com.rabbitmq.client.Channel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageListener(org.springframework.amqp.core.MessageListener) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate) Test(org.junit.Test)

Example 5 with Connection

use of org.springframework.amqp.rabbit.connection.Connection in project spring-integration by spring-projects.

the class DispatcherHasNoSubscribersTests method testPtP.

@SuppressWarnings("unchecked")
@Test
public void testPtP() throws Exception {
    final Channel channel = mock(Channel.class);
    DeclareOk declareOk = mock(DeclareOk.class);
    when(declareOk.getQueue()).thenReturn("noSubscribersChannel");
    when(channel.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), isNull())).thenReturn(declareOk);
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpTemplate amqpTemplate = mock(AmqpTemplate.class);
    PointToPointSubscribableAmqpChannel amqpChannel = new PointToPointSubscribableAmqpChannel("noSubscribersChannel", container, amqpTemplate);
    amqpChannel.setBeanName("noSubscribersChannel");
    amqpChannel.setBeanFactory(mock(BeanFactory.class));
    amqpChannel.afterPropertiesSet();
    MessageListener listener = (MessageListener) container.getMessageListener();
    try {
        listener.onMessage(new Message("Hello world!".getBytes(), null));
        fail("Exception expected");
    } catch (MessageDeliveryException e) {
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'."));
    }
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Queue.DeclareOk) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) Channel(com.rabbitmq.client.Channel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageListener(org.springframework.amqp.core.MessageListener) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate) Test(org.junit.Test)

Aggregations

Connection (org.springframework.amqp.rabbit.connection.Connection)12 Channel (com.rabbitmq.client.Channel)11 Test (org.junit.Test)10 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)10 DirectChannel (org.springframework.integration.channel.DirectChannel)8 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)7 MessageProperties (org.springframework.amqp.core.MessageProperties)6 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)5 Jackson2JsonMessageConverter (org.springframework.amqp.support.converter.Jackson2JsonMessageConverter)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 QueueChannel (org.springframework.integration.channel.QueueChannel)5 PollableChannel (org.springframework.messaging.PollableChannel)5 SimpleMessageConverter (org.springframework.amqp.support.converter.SimpleMessageConverter)4 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)3 PublisherCallbackChannelImpl (org.springframework.amqp.rabbit.support.PublisherCallbackChannelImpl)3 MessageConversionException (org.springframework.amqp.support.converter.MessageConversionException)3 MessageConverter (org.springframework.amqp.support.converter.MessageConverter)3 NullChannel (org.springframework.integration.channel.NullChannel)3 JsonToObjectTransformer (org.springframework.integration.json.JsonToObjectTransformer)3 Message (org.springframework.messaging.Message)3