SpringRabbitTracing { public RabbitTemplate decorateRabbitTemplate(RabbitTemplate rabbitTemplate) { if (beforePublishPostProcessorsField == null) return rabbitTemplate; Collection<MessagePostProcessor> processors; try { processors = (Collection) beforePublishPostProcessorsField.get(rabbitTemplate); } catch (IllegalAccessException e) { return rabbitTemplate; } TracingMessagePostProcessor tracingMessagePostProcessor = new TracingMessagePostProcessor(this); if (processors == null) { rabbitTemplate.setBeforePublishPostProcessors(tracingMessagePostProcessor); return rabbitTemplate; } for (MessagePostProcessor processor : processors) { if (processor instanceof TracingMessagePostProcessor) { return rabbitTemplate; } } List<MessagePostProcessor> newProcessors = new ArrayList<>(processors.size() + 1); newProcessors.addAll(processors); newProcessors.add(tracingMessagePostProcessor); rabbitTemplate.setBeforePublishPostProcessors( newProcessors.toArray(new MessagePostProcessor[0]) ); return rabbitTemplate; } SpringRabbitTracing(Builder builder); static SpringRabbitTracing create(Tracing tracing); static SpringRabbitTracing create(MessagingTracing messagingTracing); static Builder newBuilder(Tracing tracing); static Builder newBuilder(MessagingTracing messagingTracing); RabbitTemplate newRabbitTemplate(ConnectionFactory connectionFactory); RabbitTemplate decorateRabbitTemplate(RabbitTemplate rabbitTemplate); SimpleRabbitListenerContainerFactory newSimpleRabbitListenerContainerFactory(
    ConnectionFactory connectionFactory
  ); SimpleRabbitListenerContainerFactory decorateSimpleRabbitListenerContainerFactory(
    SimpleRabbitListenerContainerFactory factory
  );  }
@Test public void decorateRabbitTemplate_adds_by_default() { RabbitTemplate template = new RabbitTemplate(); assertThat(rabbitTracing.decorateRabbitTemplate(template)) .extracting("beforePublishPostProcessors") .satisfies(postProcessors -> assertThat(((Collection) postProcessors)).anyMatch( postProcessor -> postProcessor instanceof TracingMessagePostProcessor )); }
@Test public void decorateRabbitTemplate_skips_when_present() { RabbitTemplate template = new RabbitTemplate(); template.setBeforePublishPostProcessors(new TracingMessagePostProcessor(rabbitTracing)); assertThat(rabbitTracing.decorateRabbitTemplate(template)) .extracting("beforePublishPostProcessors") .satisfies(l -> assertThat((List<?>) l).hasSize(1)); }
@Test public void decorateRabbitTemplate_appends_when_absent() { RabbitTemplate template = new RabbitTemplate(); template.setBeforePublishPostProcessors(new UnzipPostProcessor()); assertThat(rabbitTracing.decorateRabbitTemplate(template)) .extracting("beforePublishPostProcessors") .satisfies(postProcessors -> assertThat(((Collection) postProcessors)).anyMatch( postProcessor -> postProcessor instanceof TracingMessagePostProcessor )); }