Search in sources :

Example 16 with Message

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

the class TracingRabbitListenerAdviceTest method continues_parent_trace.

@Test
public void continues_parent_trace() throws Throwable {
    MessageProperties props = new MessageProperties();
    props.setHeader("X-B3-TraceId", TRACE_ID);
    props.setHeader("X-B3-SpanId", SPAN_ID);
    props.setHeader("X-B3-ParentSpanId", PARENT_ID);
    props.setHeader("X-B3-Sampled", SAMPLED);
    Message message = MessageBuilder.withBody(new byte[0]).andProperties(props).build();
    onMessageConsumed(message);
    // cleared the headers to later work doesn't try to use the old parent
    assertThat(message.getMessageProperties().getHeaders()).isEmpty();
    assertThat(spans).filteredOn(span -> span.kind() == CONSUMER).extracting(MutableSpan::parentId).contains(SPAN_ID);
}
Also used : Message(org.springframework.amqp.core.Message) MessageProperties(org.springframework.amqp.core.MessageProperties) Test(org.junit.Test)

Example 17 with Message

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

the class TracingRabbitListenerAdviceTest method tags_consumer_span_but_not_listener.

@Test
public void tags_consumer_span_but_not_listener() throws Throwable {
    MessageProperties properties = new MessageProperties();
    properties.setConsumerQueue("foo");
    Message message = MessageBuilder.withBody(new byte[0]).andProperties(properties).build();
    onMessageConsumed(message);
    assertThat(spans.get(0).tags()).containsExactly(entry("rabbit.queue", "foo"));
    assertThat(spans.get(1).tags()).isEmpty();
}
Also used : Message(org.springframework.amqp.core.Message) MessageProperties(org.springframework.amqp.core.MessageProperties) Test(org.junit.Test)

Example 18 with Message

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

the class RabbitAutoConfigurationTests method testRabbitListenerContainerFactoryWithCustomSettings.

@Test
public void testRabbitListenerContainerFactoryWithCustomSettings() {
    load(new Class<?>[] { MessageConvertersConfiguration.class, MessageRecoverersConfiguration.class }, "spring.rabbitmq.listener.retry.enabled:true", "spring.rabbitmq.listener.retry.maxAttempts:4", "spring.rabbitmq.listener.retry.initialInterval:2000", "spring.rabbitmq.listener.retry.multiplier:1.5", "spring.rabbitmq.listener.retry.maxInterval:5000", "spring.rabbitmq.listener.autoStartup:false", "spring.rabbitmq.listener.acknowledgeMode:manual", "spring.rabbitmq.listener.concurrency:5", "spring.rabbitmq.listener.maxConcurrency:10", "spring.rabbitmq.listener.prefetch:40", "spring.rabbitmq.listener.defaultRequeueRejected:false", "spring.rabbitmq.listener.idleEventInterval:5", "spring.rabbitmq.listener.transactionSize:20");
    SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory = this.context.getBean("rabbitListenerContainerFactory", SimpleRabbitListenerContainerFactory.class);
    DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitListenerContainerFactory);
    assertThat(dfa.getPropertyValue("autoStartup")).isEqualTo(Boolean.FALSE);
    assertThat(dfa.getPropertyValue("acknowledgeMode")).isEqualTo(AcknowledgeMode.MANUAL);
    assertThat(dfa.getPropertyValue("concurrentConsumers")).isEqualTo(5);
    assertThat(dfa.getPropertyValue("maxConcurrentConsumers")).isEqualTo(10);
    assertThat(dfa.getPropertyValue("prefetchCount")).isEqualTo(40);
    assertThat(dfa.getPropertyValue("txSize")).isEqualTo(20);
    assertThat(dfa.getPropertyValue("messageConverter")).isSameAs(this.context.getBean("myMessageConverter"));
    assertThat(dfa.getPropertyValue("defaultRequeueRejected")).isEqualTo(Boolean.FALSE);
    assertThat(dfa.getPropertyValue("idleEventInterval")).isEqualTo(5L);
    Advice[] adviceChain = (Advice[]) dfa.getPropertyValue("adviceChain");
    assertThat(adviceChain).isNotNull();
    assertThat(adviceChain.length).isEqualTo(1);
    dfa = new DirectFieldAccessor(adviceChain[0]);
    MessageRecoverer messageRecoverer = this.context.getBean("myMessageRecoverer", MessageRecoverer.class);
    MethodInvocationRecoverer<?> mir = (MethodInvocationRecoverer<?>) dfa.getPropertyValue("recoverer");
    Message message = mock(Message.class);
    Exception ex = new Exception("test");
    mir.recover(new Object[] { "foo", message }, ex);
    verify(messageRecoverer).recover(message, ex);
    RetryTemplate retryTemplate = (RetryTemplate) dfa.getPropertyValue("retryOperations");
    assertThat(retryTemplate).isNotNull();
    dfa = new DirectFieldAccessor(retryTemplate);
    SimpleRetryPolicy retryPolicy = (SimpleRetryPolicy) dfa.getPropertyValue("retryPolicy");
    ExponentialBackOffPolicy backOffPolicy = (ExponentialBackOffPolicy) dfa.getPropertyValue("backOffPolicy");
    assertThat(retryPolicy.getMaxAttempts()).isEqualTo(4);
    assertThat(backOffPolicy.getInitialInterval()).isEqualTo(2000);
    assertThat(backOffPolicy.getMultiplier()).isEqualTo(1.5);
    assertThat(backOffPolicy.getMaxInterval()).isEqualTo(5000);
}
Also used : RetryTemplate(org.springframework.retry.support.RetryTemplate) MessageRecoverer(org.springframework.amqp.rabbit.retry.MessageRecoverer) Message(org.springframework.amqp.core.Message) ExponentialBackOffPolicy(org.springframework.retry.backoff.ExponentialBackOffPolicy) SimpleRabbitListenerContainerFactory(org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MethodInvocationRecoverer(org.springframework.retry.interceptor.MethodInvocationRecoverer) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Advice(org.aopalliance.aop.Advice) ExpectedException(org.junit.rules.ExpectedException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Example 19 with Message

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

the class TracingRabbitListenerAdviceTest method consumer_has_service_name.

@Test
public void consumer_has_service_name() throws Throwable {
    Message message = MessageBuilder.withBody(new byte[] {}).build();
    onMessageConsumed(message);
    assertThat(reportedSpans).extracting(Span::remoteServiceName).containsExactly("my-service", null);
}
Also used : Message(org.springframework.amqp.core.Message) Test(org.junit.Test)

Example 20 with Message

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

the class DispatcherHasNoSubscribersTests method testPubSub.

@Test
public void testPubSub() {
    final Channel channel = mock(Channel.class);
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpTemplate amqpTemplate = mock(AmqpTemplate.class);
    PublishSubscribeAmqpChannel amqpChannel = new PublishSubscribeAmqpChannel("noSubscribersChannel", container, amqpTemplate);
    amqpChannel.setBeanName("noSubscribersChannel");
    amqpChannel.setBeanFactory(mock(BeanFactory.class));
    amqpChannel.afterPropertiesSet();
    List<String> logList = insertMockLoggerInListener(amqpChannel);
    MessageListener listener = (MessageListener) container.getMessageListener();
    listener.onMessage(new Message("Hello world!".getBytes(), null));
    verifyLogReceived(logList);
}
Also used : ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) Channel(com.rabbitmq.client.Channel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageListener(org.springframework.amqp.core.MessageListener) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate) 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