Search in sources :

Example 26 with Message

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

the class OutboundEndpointTests method testAsyncDelayExpression.

@Test
public void testAsyncDelayExpression() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    AsyncRabbitTemplate amqpTemplate = spy(new AsyncRabbitTemplate(new RabbitTemplate(connectionFactory), new SimpleMessageListenerContainer(connectionFactory), "replyTo"));
    amqpTemplate.setTaskScheduler(mock(TaskScheduler.class));
    AsyncAmqpOutboundGateway gateway = new AsyncAmqpOutboundGateway(amqpTemplate);
    willAnswer(invocation -> amqpTemplate.new RabbitMessageFuture("foo", invocation.getArgument(2))).given(amqpTemplate).sendAndReceive(anyString(), anyString(), any(Message.class));
    gateway.setExchangeName("foo");
    gateway.setRoutingKey("bar");
    gateway.setDelayExpressionString("42");
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setOutputChannel(new NullChannel());
    gateway.afterPropertiesSet();
    gateway.start();
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    gateway.handleMessage(new GenericMessage<>("foo"));
    verify(amqpTemplate).sendAndReceive(eq("foo"), eq("bar"), captor.capture());
    assertThat(captor.getValue().getMessageProperties().getDelay(), equalTo(42));
}
Also used : AsyncRabbitTemplate(org.springframework.amqp.rabbit.AsyncRabbitTemplate) RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) AsyncRabbitTemplate(org.springframework.amqp.rabbit.AsyncRabbitTemplate) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) BeanFactory(org.springframework.beans.factory.BeanFactory) TaskScheduler(org.springframework.scheduling.TaskScheduler) NullChannel(org.springframework.integration.channel.NullChannel) Test(org.junit.Test)

Example 27 with Message

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

the class OutboundEndpointTests method testHeaderMapperWinsAdapter.

@Test
public void testHeaderMapperWinsAdapter() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    RabbitTemplate amqpTemplate = spy(new RabbitTemplate(connectionFactory));
    AmqpOutboundEndpoint endpoint = new AmqpOutboundEndpoint(amqpTemplate);
    endpoint.setHeadersMappedLast(true);
    final AtomicReference<Message> amqpMessage = new AtomicReference<Message>();
    willAnswer(invocation -> {
        amqpMessage.set(invocation.getArgument(2));
        return null;
    }).given(amqpTemplate).send(isNull(), isNull(), any(Message.class), isNull());
    org.springframework.messaging.Message<?> message = MessageBuilder.withPayload("foo").setHeader(MessageHeaders.CONTENT_TYPE, "bar").build();
    endpoint.handleMessage(message);
    assertNotNull(amqpMessage.get());
    assertEquals("bar", amqpMessage.get().getMessageProperties().getContentType());
}
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) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 28 with Message

use of org.springframework.amqp.core.Message 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)

Example 29 with Message

use of org.springframework.amqp.core.Message in project av-service by dvoraka.

the class AvMessageMapper method transform.

/**
 * Transforms AV message to AMQP message.
 *
 * @param msg the AV message
 * @return the AMQP message
 * @throws MapperException if mapping failed
 */
public Message transform(AvMessage msg) throws MapperException {
    log.debug("AVTransform: " + msg);
    requireNonNull(msg, "Message must not be null!");
    // mandatory fields
    if (msg.getId() == null) {
        throw new MapperException("Message ID must not be null");
    } else if (msg.getType() == null) {
        throw new MapperException("Message type must not be null");
    }
    MessageProperties props = new MessageProperties();
    props.setMessageId(msg.getId());
    props.setType(msg.getType().toString());
    // correlation ID
    if (msg.getCorrelationId() != null) {
        props.setCorrelationId(msg.getCorrelationId());
    }
    // virus info
    props.setHeader(VIRUS_INFO_KEY, msg.getVirusInfo());
    // owner
    props.setHeader(OWNER_KEY, msg.getOwner());
    // filename
    props.setHeader(FILENAME_KEY, msg.getFilename());
    return new Message(msg.getData(), props);
}
Also used : Message(org.springframework.amqp.core.Message) AvMessage(dvoraka.avservice.common.data.AvMessage) DefaultAvMessage(dvoraka.avservice.common.data.DefaultAvMessage) MessageProperties(org.springframework.amqp.core.MessageProperties) MapperException(dvoraka.avservice.common.exception.MapperException)

Example 30 with Message

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

the class MessageHeadersTest method setHeader_noProperties.

@Test
public void setHeader_noProperties() {
    message = new Message(new byte[0], null);
    // doesn't crash
    MessageHeaders.setHeader(message, "b3", "1");
}
Also used : Message(org.springframework.amqp.core.Message) 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