Search in sources :

Example 1 with SimpleMessageConverter

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

the class MappingUtilsTests method testMapping.

@Test
public void testMapping() {
    Message<?> requestMessage = MessageBuilder.withPayload("foo").setHeader(AmqpHeaders.CONTENT_TYPE, "my/ct").build();
    MessageConverter converter = new SimpleMessageConverter();
    AmqpHeaderMapper headerMapper = DefaultAmqpHeaderMapper.outboundMapper();
    MessageDeliveryMode defaultDeliveryMode = MessageDeliveryMode.NON_PERSISTENT;
    boolean headersMappedLast = false;
    org.springframework.amqp.core.Message mapped = MappingUtils.mapMessage(requestMessage, converter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("text/plain"));
    headersMappedLast = true;
    mapped = MappingUtils.mapMessage(requestMessage, converter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("my/ct"));
    ContentTypeDelegatingMessageConverter ctdConverter = new ContentTypeDelegatingMessageConverter();
    ctdConverter.addDelegate("my/ct", converter);
    mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("my/ct"));
    headersMappedLast = false;
    mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("text/plain"));
    headersMappedLast = true;
    requestMessage = MessageBuilder.withPayload("foo").setHeader(AmqpHeaders.CONTENT_TYPE, 42).build();
    try {
        mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("contentType header must be a MimeType or String, found: java.lang.Integer"));
    }
}
Also used : SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) ContentTypeDelegatingMessageConverter(org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) ContentTypeDelegatingMessageConverter(org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter) MessageDeliveryMode(org.springframework.amqp.core.MessageDeliveryMode) Test(org.junit.Test)

Example 2 with SimpleMessageConverter

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

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

Aggregations

SimpleMessageConverter (org.springframework.amqp.support.converter.SimpleMessageConverter)3 Test (org.junit.Test)2 MessageConverter (org.springframework.amqp.support.converter.MessageConverter)2 Channel (com.rabbitmq.client.Channel)1 MessageDeliveryMode (org.springframework.amqp.core.MessageDeliveryMode)1 MessageListener (org.springframework.amqp.core.MessageListener)1 MessageProperties (org.springframework.amqp.core.MessageProperties)1 Connection (org.springframework.amqp.rabbit.connection.Connection)1 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)1 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)1 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)1 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)1 ContentTypeDelegatingMessageConverter (org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter)1 Jackson2JsonMessageConverter (org.springframework.amqp.support.converter.Jackson2JsonMessageConverter)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 JsonToObjectTransformer (org.springframework.integration.json.JsonToObjectTransformer)1 ObjectToJsonTransformer (org.springframework.integration.json.ObjectToJsonTransformer)1 Transformer (org.springframework.integration.transformer.Transformer)1