Class RabbitTraceHeaderCarrier

java.lang.Object
com.fayupable.logged.spring.rabbitmq.RabbitTraceHeaderCarrier

public final class RabbitTraceHeaderCarrier extends Object
Carries a @Logged call chain's FlowContext across a RabbitMQ message, by writing it into the message's properties when publishing and reading it back when consuming.

The Spring AMQP counterpart to com.fayupable.logged.spring.kafka.KafkaTraceHeaderCarrier: the traceId and depth are written as plain entries in MessageProperties.getHeaders(), never into the message body. RabbitMQ message headers exist specifically for metadata like this: they travel with the message but are entirely separate from whatever serialization format the message body uses, so this never touches or constrains that body's schema.

Like every propagation class in this library, nothing here is wired in automatically. A consuming application calls writeToHeaders(org.springframework.amqp.core.MessageProperties) when publishing a message and readAndAdopt(org.springframework.amqp.core.MessageProperties, java.lang.Runnable) when consuming one, exactly where it already publishes or consumes RabbitMQ messages.

  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    readAndAdopt(org.springframework.amqp.core.MessageProperties properties, Runnable work)
    Reads a FlowContext from properties, if present, adopts it as the active context on this thread for the duration of work, and restores this thread's previous context again afterward, regardless of whether work completes normally or throws.
    static void
    writeToHeaders(org.springframework.amqp.core.MessageProperties properties)
    Writes the current thread's FlowContext, if any is active, into properties as TRACE_ID_HEADER/DEPTH_HEADER header entries.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • writeToHeaders

      public static void writeToHeaders(org.springframework.amqp.core.MessageProperties properties)
      Writes the current thread's FlowContext, if any is active, into properties as TRACE_ID_HEADER/DEPTH_HEADER header entries.

      Does nothing if no @Logged call is currently active on this thread: a message published from outside any call chain simply carries no trace headers, exactly as if this method had never been called.

      Parameters:
      properties - the properties of the message about to be published
    • readAndAdopt

      public static void readAndAdopt(org.springframework.amqp.core.MessageProperties properties, Runnable work)
      Reads a FlowContext from properties, if present, adopts it as the active context on this thread for the duration of work, and restores this thread's previous context again afterward, regardless of whether work completes normally or throws.

      If properties carries no TRACE_ID_HEADER — for example, a message published by a service that does not use this library, or one published from outside any call chain — work is simply run as-is, with no context adopted. Any @Logged call made from within it then starts a new chain of its own, exactly as it would without this class involved at all.

      Parameters:
      properties - the properties of the message being consumed
      work - the message-handling code to run with the message's trace context active