Search in sources :

Example 1 with Message

use of org.springframework.amqp.core.Message in project brave by openzipkin.

the class TracingMessagePostProcessorTest method should_add_b3_headers_to_message.

@Test
public void should_add_b3_headers_to_message() {
    Message message = MessageBuilder.withBody(new byte[] {}).build();
    Message postProcessMessage = tracingMessagePostProcessor.postProcessMessage(message);
    List<String> expectedHeaders = Arrays.asList("X-B3-TraceId", "X-B3-SpanId", "X-B3-Sampled");
    Set<String> headerKeys = postProcessMessage.getMessageProperties().getHeaders().keySet();
    assertThat(headerKeys).containsAll(expectedHeaders);
}
Also used : Message(org.springframework.amqp.core.Message) Test(org.junit.Test)

Example 2 with Message

use of org.springframework.amqp.core.Message in project microservices by pwillhan.

the class RabbitTemplateExample method main.

public static void main(String[] args) throws UnsupportedEncodingException {
    CachingConnectionFactory factory = null;
    try {
        factory = new CachingConnectionFactory("localhost");
        RabbitTemplate template = new RabbitTemplate(factory);
        MessageProperties properties = new MessageProperties();
        properties.setContentType("text/xml");
        properties.setContentEncoding("utf-8");
        String soapMessage = "<soapenv:Envelope" + "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope\">\n" + "<soapenv:Header/>\n" + "<soapenv:Body>\n" + "  <p:greet xmlns:p=\"http://greet.service.kishanthan.org\">\n" + "     <in>" + "sample" + "</in>\n" + "  </p:greet>\n" + "</soapenv:Body>\n" + "</soapenv:Envelope>";
        Message message = new Message(soapMessage.getBytes("UTF-8"), properties);
        template.convertAndSend("", "test-queue", message);
    } finally {
        if (factory != null) {
            factory.destroy();
        }
    }
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Message(org.springframework.amqp.core.Message) MessageProperties(org.springframework.amqp.core.MessageProperties) CachingConnectionFactory(org.springframework.amqp.rabbit.connection.CachingConnectionFactory)

Example 3 with Message

use of org.springframework.amqp.core.Message in project spring-integration by spring-projects.

the class OutboundEndpointTests method testDelayExpression.

@Test
public void testDelayExpression() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    RabbitTemplate amqpTemplate = spy(new RabbitTemplate(connectionFactory));
    AmqpOutboundEndpoint endpoint = new AmqpOutboundEndpoint(amqpTemplate);
    willDoNothing().given(amqpTemplate).send(anyString(), anyString(), any(Message.class), isNull());
    willAnswer(invocation -> invocation.getArgument(2)).given(amqpTemplate).sendAndReceive(anyString(), anyString(), any(Message.class), isNull());
    endpoint.setExchangeName("foo");
    endpoint.setRoutingKey("bar");
    endpoint.setDelayExpressionString("42");
    endpoint.setBeanFactory(mock(BeanFactory.class));
    endpoint.afterPropertiesSet();
    endpoint.handleMessage(new GenericMessage<>("foo"));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    verify(amqpTemplate).send(eq("foo"), eq("bar"), captor.capture(), isNull());
    assertThat(captor.getValue().getMessageProperties().getDelay(), equalTo(42));
    endpoint.setExpectReply(true);
    endpoint.setOutputChannel(new NullChannel());
    endpoint.handleMessage(new GenericMessage<>("foo"));
    verify(amqpTemplate).sendAndReceive(eq("foo"), eq("bar"), captor.capture(), isNull());
    assertThat(captor.getValue().getMessageProperties().getDelay(), equalTo(42));
    endpoint.setDelay(23);
    endpoint.setRoutingKey("baz");
    endpoint.afterPropertiesSet();
    endpoint.handleMessage(new GenericMessage<>("foo"));
    verify(amqpTemplate).sendAndReceive(eq("foo"), eq("baz"), captor.capture(), isNull());
    assertThat(captor.getValue().getMessageProperties().getDelay(), equalTo(23));
}
Also used : AsyncRabbitTemplate(org.springframework.amqp.rabbit.AsyncRabbitTemplate) RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) NullChannel(org.springframework.integration.channel.NullChannel) Test(org.junit.Test)

Example 4 with Message

use of org.springframework.amqp.core.Message in project spring-integration by spring-projects.

the class OutboundEndpointTests method testHeaderMapperWinsGateway.

@Test
public void testHeaderMapperWinsGateway() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    TestRabbitTemplate amqpTemplate = spy(new TestRabbitTemplate(connectionFactory));
    AmqpOutboundEndpoint endpoint = new AmqpOutboundEndpoint(amqpTemplate);
    endpoint.setHeadersMappedLast(true);
    endpoint.setExpectReply(true);
    DefaultAmqpHeaderMapper mapper = DefaultAmqpHeaderMapper.inboundMapper();
    mapper.setRequestHeaderNames("*");
    endpoint.setHeaderMapper(mapper);
    final AtomicReference<Message> amqpMessage = new AtomicReference<Message>();
    willAnswer(invocation -> {
        amqpMessage.set(invocation.getArgument(2));
        return null;
    }).given(amqpTemplate).doSendAndReceiveWithTemporary(isNull(), isNull(), any(Message.class), isNull());
    org.springframework.messaging.Message<?> message = MessageBuilder.withPayload("foo").setHeader(MessageHeaders.CONTENT_TYPE, "bar").setReplyChannel(new QueueChannel()).build();
    endpoint.handleMessage(message);
    assertNotNull(amqpMessage.get());
    assertEquals("bar", amqpMessage.get().getMessageProperties().getContentType());
    assertNull(amqpMessage.get().getMessageProperties().getHeaders().get(MessageHeaders.REPLY_CHANNEL));
}
Also used : ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) DefaultAmqpHeaderMapper(org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 5 with Message

use of org.springframework.amqp.core.Message in project spring-integration by spring-projects.

the class AmqpInboundChannelAdapterParserTests method withHeaderMapperOnlyCustomHeaders.

@Test
public void withHeaderMapperOnlyCustomHeaders() throws Exception {
    AmqpInboundChannelAdapter adapter = context.getBean("withHeaderMapperOnlyCustomHeaders", AmqpInboundChannelAdapter.class);
    AbstractMessageListenerContainer mlc = TestUtils.getPropertyValue(adapter, "messageListenerContainer", AbstractMessageListenerContainer.class);
    ChannelAwareMessageListener listener = TestUtils.getPropertyValue(mlc, "messageListener", ChannelAwareMessageListener.class);
    MessageProperties amqpProperties = new MessageProperties();
    amqpProperties.setAppId("test.appId");
    amqpProperties.setClusterId("test.clusterId");
    amqpProperties.setContentEncoding("test.contentEncoding");
    amqpProperties.setContentLength(99L);
    amqpProperties.setContentType("test.contentType");
    amqpProperties.setHeader("foo", "foo");
    amqpProperties.setHeader("bar", "bar");
    Message amqpMessage = new Message("hello".getBytes(), amqpProperties);
    listener.onMessage(amqpMessage, null);
    QueueChannel requestChannel = context.getBean("requestChannel", QueueChannel.class);
    org.springframework.messaging.Message<?> siMessage = requestChannel.receive(0);
    assertEquals("foo", siMessage.getHeaders().get("foo"));
    assertNull(siMessage.getHeaders().get("bar"));
    assertNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_ENCODING));
    assertNull(siMessage.getHeaders().get(AmqpHeaders.CLUSTER_ID));
    assertNull(siMessage.getHeaders().get(AmqpHeaders.APP_ID));
    assertNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_TYPE));
}
Also used : AmqpInboundChannelAdapter(org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter) Message(org.springframework.amqp.core.Message) QueueChannel(org.springframework.integration.channel.QueueChannel) MessageProperties(org.springframework.amqp.core.MessageProperties) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Aggregations

Message (org.springframework.amqp.core.Message)43 Test (org.junit.Test)38 MessageProperties (org.springframework.amqp.core.MessageProperties)15 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)6 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)6 AbstractMessageListenerContainer (org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer)5 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)5 QueueChannel (org.springframework.integration.channel.QueueChannel)5 BeanFactory (org.springframework.beans.factory.BeanFactory)4 AmqpInboundChannelAdapter (org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter)4 GenericMessage (org.springframework.messaging.support.GenericMessage)4 AsyncRabbitTemplate (org.springframework.amqp.rabbit.AsyncRabbitTemplate)3 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)3 Channel (com.rabbitmq.client.Channel)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Advice (org.aopalliance.aop.Advice)2 AmqpTemplate (org.springframework.amqp.core.AmqpTemplate)2 MessageListener (org.springframework.amqp.core.MessageListener)2 Connection (org.springframework.amqp.rabbit.connection.Connection)2 MessageRecoverer (org.springframework.amqp.rabbit.retry.MessageRecoverer)2