Search in sources :

Example 6 with Connection

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

the class AmqpOutboundChannelAdapterParserTests method testInt2773UseDefaultAmqpTemplateExchangeAndRoutingLey.

@Test
public void testInt2773UseDefaultAmqpTemplateExchangeAndRoutingLey() 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("toRabbitOnlyWithTemplateChannel", MessageChannel.class);
    requestChannel.send(MessageBuilder.withPayload("test").build());
    Mockito.verify(mockChannel, Mockito.times(1)).basicPublish(Mockito.eq("default.test.exchange"), Mockito.eq("default.routing.key"), 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 7 with Connection

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

the class AmqpOutboundChannelAdapterParserTests method testInt2773WithDefaultAmqpTemplateExchangeAndRoutingKey.

@Test
public void testInt2773WithDefaultAmqpTemplateExchangeAndRoutingKey() 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("withDefaultAmqpTemplateExchangeAndRoutingKey", 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 8 with Connection

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

the class InboundEndpointTests method testGatewayConversionError.

@Test
public void testGatewayConversionError() throws Exception {
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpInboundGateway adapter = new AmqpInboundGateway(container);
    QueueChannel outputChannel = new QueueChannel();
    adapter.setRequestChannel(outputChannel);
    QueueChannel errorChannel = new QueueChannel();
    adapter.setErrorChannel(errorChannel);
    adapter.setMessageConverter(new MessageConverter() {

        @Override
        public org.springframework.amqp.core.Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
            throw new MessageConversionException("intended");
        }

        @Override
        public Object fromMessage(org.springframework.amqp.core.Message message) throws MessageConversionException {
            return null;
        }
    });
    adapter.afterPropertiesSet();
    ((ChannelAwareMessageListener) container.getMessageListener()).onMessage(null, null);
    assertNull(outputChannel.receive(0));
    assertNotNull(errorChannel.receive(0));
}
Also used : MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) PollableChannel(org.springframework.messaging.PollableChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) QueueChannel(org.springframework.integration.channel.QueueChannel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageProperties(org.springframework.amqp.core.MessageProperties) Test(org.junit.Test)

Example 9 with Connection

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

the class InboundEndpointTests method testInt2809JavaTypePropertiesToAmqp.

@Test
public void testInt2809JavaTypePropertiesToAmqp() throws Exception {
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
    adapter.setMessageConverter(new Jackson2JsonMessageConverter());
    PollableChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    Object payload = new Foo("bar1");
    Transformer objectToJsonTransformer = new ObjectToJsonTransformer();
    Message<?> jsonMessage = objectToJsonTransformer.transform(new GenericMessage<Object>(payload));
    MessageProperties amqpMessageProperties = new MessageProperties();
    amqpMessageProperties.setDeliveryTag(123L);
    org.springframework.amqp.core.Message amqpMessage = new SimpleMessageConverter().toMessage(jsonMessage.getPayload(), amqpMessageProperties);
    DefaultAmqpHeaderMapper.inboundMapper().fromHeadersToRequest(jsonMessage.getHeaders(), amqpMessageProperties);
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    Channel rabbitChannel = mock(Channel.class);
    listener.onMessage(amqpMessage, rabbitChannel);
    Message<?> result = channel.receive(1000);
    assertEquals(payload, result.getPayload());
    assertSame(rabbitChannel, result.getHeaders().get(AmqpHeaders.CHANNEL));
    assertEquals(123L, result.getHeaders().get(AmqpHeaders.DELIVERY_TAG));
}
Also used : ObjectToJsonTransformer(org.springframework.integration.json.ObjectToJsonTransformer) Transformer(org.springframework.integration.transformer.Transformer) JsonToObjectTransformer(org.springframework.integration.json.JsonToObjectTransformer) ObjectToJsonTransformer(org.springframework.integration.json.ObjectToJsonTransformer) QueueChannel(org.springframework.integration.channel.QueueChannel) PollableChannel(org.springframework.messaging.PollableChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) QueueChannel(org.springframework.integration.channel.QueueChannel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageProperties(org.springframework.amqp.core.MessageProperties) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) Test(org.junit.Test)

Example 10 with Connection

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

the class InboundEndpointTests method testInt2809JavaTypePropertiesFromAmqp.

@Test
public void testInt2809JavaTypePropertiesFromAmqp() throws Exception {
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
    PollableChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    Object payload = new Foo("bar1");
    MessageProperties amqpMessageProperties = new MessageProperties();
    org.springframework.amqp.core.Message amqpMessage = new Jackson2JsonMessageConverter().toMessage(payload, amqpMessageProperties);
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    listener.onMessage(amqpMessage, null);
    Message<?> receive = channel.receive(1000);
    Message<?> result = new JsonToObjectTransformer().transform(receive);
    assertEquals(payload, result.getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) JsonToObjectTransformer(org.springframework.integration.json.JsonToObjectTransformer) PollableChannel(org.springframework.messaging.PollableChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) QueueChannel(org.springframework.integration.channel.QueueChannel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageProperties(org.springframework.amqp.core.MessageProperties) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) 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