Search in sources :

Example 6 with MessageConverter

use of org.springframework.amqp.support.converter.MessageConverter in project spring-integration by spring-projects.

the class AbstractSubscribableAmqpChannel method onInit.

@Override
public void onInit() throws Exception {
    super.onInit();
    this.dispatcher = this.createDispatcher();
    if (this.maxSubscribers == null) {
        this.maxSubscribers = this.getIntegrationProperty(this.isPubSub ? IntegrationProperties.CHANNELS_MAX_BROADCAST_SUBSCRIBERS : IntegrationProperties.CHANNELS_MAX_UNICAST_SUBSCRIBERS, Integer.class);
    }
    setMaxSubscribers(this.maxSubscribers);
    String queue = obtainQueueName(this.channelName);
    this.container.setQueueNames(queue);
    MessageConverter converter = (this.getAmqpTemplate() instanceof RabbitTemplate) ? ((RabbitTemplate) this.getAmqpTemplate()).getMessageConverter() : new SimpleMessageConverter();
    MessageListener listener = new DispatchingMessageListener(converter, this.dispatcher, this, this.isPubSub, getMessageBuilderFactory(), getInboundHeaderMapper());
    this.container.setMessageListener(listener);
    if (!this.container.isActive()) {
        this.container.afterPropertiesSet();
    }
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) MessageListener(org.springframework.amqp.core.MessageListener) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter)

Example 7 with MessageConverter

use of org.springframework.amqp.support.converter.MessageConverter in project spring-integration by spring-projects.

the class AmqpInboundGatewayParserTests method customMessageConverter.

@Test
public void customMessageConverter() {
    Object gateway = context.getBean("gateway");
    MessageConverter gatewayConverter = TestUtils.getPropertyValue(gateway, "amqpMessageConverter", MessageConverter.class);
    MessageConverter templateConverter = TestUtils.getPropertyValue(gateway, "amqpTemplate.messageConverter", MessageConverter.class);
    TestConverter testConverter = context.getBean("testConverter", TestConverter.class);
    assertSame(testConverter, gatewayConverter);
    assertSame(testConverter, templateConverter);
    assertEquals(Boolean.TRUE, TestUtils.getPropertyValue(gateway, "autoStartup"));
    assertEquals(0, TestUtils.getPropertyValue(gateway, "phase"));
    assertEquals(Long.valueOf(1234L), TestUtils.getPropertyValue(gateway, "replyTimeout", Long.class));
    assertEquals(Long.valueOf(1234L), TestUtils.getPropertyValue(gateway, "messagingTemplate.receiveTimeout", Long.class));
    assertTrue(TestUtils.getPropertyValue(gateway, "messageListenerContainer.missingQueuesFatal", Boolean.class));
}
Also used : SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) Test(org.junit.Test)

Example 8 with MessageConverter

use of org.springframework.amqp.support.converter.MessageConverter 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 MessageConverter

use of org.springframework.amqp.support.converter.MessageConverter in project spring-integration by spring-projects.

the class InboundEndpointTests method testAdapterConversionError.

@Test
public void testAdapterConversionError() 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);
    QueueChannel outputChannel = new QueueChannel();
    adapter.setOutputChannel(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 10 with MessageConverter

use of org.springframework.amqp.support.converter.MessageConverter in project spring-integration by spring-projects.

the class DefaultAmqpHeaderMapperTests method jsonTypeIdNotOverwritten.

// INT-2090
@Test
public void jsonTypeIdNotOverwritten() {
    DefaultAmqpHeaderMapper headerMapper = DefaultAmqpHeaderMapper.inboundMapper();
    MessageConverter converter = new Jackson2JsonMessageConverter();
    MessageProperties amqpProperties = new MessageProperties();
    converter.toMessage("123", amqpProperties);
    Map<String, Object> headerMap = new HashMap<String, Object>();
    headerMap.put("__TypeId__", "java.lang.Integer");
    MessageHeaders integrationHeaders = new MessageHeaders(headerMap);
    headerMapper.fromHeadersToRequest(integrationHeaders, amqpProperties);
    assertEquals("java.lang.String", amqpProperties.getHeaders().get("__TypeId__"));
    Object result = converter.fromMessage(new Message("123".getBytes(), amqpProperties));
    assertEquals(String.class, result.getClass());
}
Also used : Message(org.springframework.amqp.core.Message) MessageProperties(org.springframework.amqp.core.MessageProperties) HashMap(java.util.HashMap) MessageHeaders(org.springframework.messaging.MessageHeaders) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) Test(org.junit.Test)

Aggregations

MessageConverter (org.springframework.amqp.support.converter.MessageConverter)10 Test (org.junit.Test)6 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)5 SimpleMessageConverter (org.springframework.amqp.support.converter.SimpleMessageConverter)5 MessageProperties (org.springframework.amqp.core.MessageProperties)3 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)3 Jackson2JsonMessageConverter (org.springframework.amqp.support.converter.Jackson2JsonMessageConverter)3 MessageConversionException (org.springframework.amqp.support.converter.MessageConversionException)3 Message (org.springframework.messaging.Message)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 Channel (com.rabbitmq.client.Channel)2 MessageListener (org.springframework.amqp.core.MessageListener)2 Connection (org.springframework.amqp.rabbit.connection.Connection)2 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)2 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)2 DirectChannel (org.springframework.integration.channel.DirectChannel)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 PollableChannel (org.springframework.messaging.PollableChannel)2 HashMap (java.util.HashMap)1 Message (org.springframework.amqp.core.Message)1