Example usage for org.springframework.messaging.support MessageHeaderAccessor setImmutable

List of usage examples for org.springframework.messaging.support MessageHeaderAccessor setImmutable

Introduction

In this page you can find the example usage for org.springframework.messaging.support MessageHeaderAccessor setImmutable.

Prototype

public void setImmutable() 

Source Link

Document

By default when #getMessageHeaders() is called, "this" MessageHeaderAccessor instance can no longer be used to modify the underlying message headers.

Usage

From source file:org.springframework.cloud.sleuth.instrument.messaging.TracingChannelInterceptor.java

/**
 * Use this to create a span for processing the given message. Note: the result has no
 * name and is not started./* ww  w  .j ava  2s .  co  m*/
 * <p>
 * <p>
 * This creates a child from identifiers extracted from the message headers, or a new
 * span if one couldn't be extracted.
 */
public Span nextSpan(Message<?> message) {
    MessageHeaderAccessor headers = mutableHeaderAccessor(message);
    TraceContextOrSamplingFlags extracted = this.extractor.extract(headers);
    headers.setImmutable();
    Span result = this.tracer.nextSpan(extracted);
    if (extracted.context() == null && !result.isNoop()) {
        addTags(message, result, null);
    }
    if (log.isDebugEnabled()) {
        log.debug("Created a new span " + result);
    }
    return result;
}

From source file:org.springframework.cloud.sleuth.instrument.messaging.TracingChannelInterceptor.java

/**
 * This starts a consumer span as a child of the incoming message or the current trace
 * context, placing it in scope until the receive completes.
 *//* w w  w .j ava2  s .  co  m*/
@Override
public Message<?> postReceive(Message<?> message, MessageChannel channel) {
    if (emptyMessage(message)) {
        return message;
    }
    MessageHeaderAccessor headers = mutableHeaderAccessor(message);
    TraceContextOrSamplingFlags extracted = this.extractor.extract(headers);
    Span span = this.threadLocalSpan.next(extracted);
    MessageHeaderPropagation.removeAnyTraceHeaders(headers, this.tracing.propagation().keys());
    this.injector.inject(span.context(), headers);
    if (!span.isNoop()) {
        span.kind(Span.Kind.CONSUMER).name("receive").start();
        span.remoteServiceName(REMOTE_SERVICE_NAME);
        addTags(message, span, channel);
    }
    if (log.isDebugEnabled()) {
        log.debug("Created a new span in post receive " + span);
    }
    headers.setImmutable();
    return new GenericMessage<>(message.getPayload(), headers.getMessageHeaders());
}

From source file:org.springframework.cloud.sleuth.instrument.messaging.TracingChannelInterceptor.java

/**
 * This starts a consumer span as a child of the incoming message or the current trace
 * context. It then creates a span for the handler, placing it in scope.
 *//* w  ww. j a v a 2  s.  c  o  m*/
@Override
public Message<?> beforeHandle(Message<?> message, MessageChannel channel, MessageHandler handler) {
    if (emptyMessage(message)) {
        return message;
    }
    MessageHeaderAccessor headers = mutableHeaderAccessor(message);
    TraceContextOrSamplingFlags extracted = this.extractor.extract(headers);
    // Start and finish a consumer span as we will immediately process it.
    Span consumerSpan = this.tracer.nextSpan(extracted);
    if (!consumerSpan.isNoop()) {
        consumerSpan.kind(Span.Kind.CONSUMER).start();
        consumerSpan.remoteServiceName(REMOTE_SERVICE_NAME);
        addTags(message, consumerSpan, channel);
        consumerSpan.finish();
    }
    // create and scope a span for the message processor
    this.threadLocalSpan.next(TraceContextOrSamplingFlags.create(consumerSpan.context())).name("handle")
            .start();
    // remove any trace headers, but don't re-inject as we are synchronously
    // processing the
    // message and can rely on scoping to access this span later.
    MessageHeaderPropagation.removeAnyTraceHeaders(headers, this.tracing.propagation().keys());
    if (log.isDebugEnabled()) {
        log.debug("Created a new span in before handle" + consumerSpan);
    }
    if (message instanceof ErrorMessage) {
        return new ErrorMessage((Throwable) message.getPayload(), headers.getMessageHeaders());
    }
    headers.setImmutable();
    return new GenericMessage<>(message.getPayload(), headers.getMessageHeaders());
}

From source file:org.springframework.messaging.core.GenericMessagingTemplate.java

protected final void doSend(MessageChannel channel, Message<?> message, long timeout) {
    Assert.notNull(channel, "MessageChannel is required");

    Message<?> messageToSend = message;
    MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
    if (accessor != null && accessor.isMutable()) {
        accessor.removeHeader(this.sendTimeoutHeader);
        accessor.removeHeader(this.receiveTimeoutHeader);
        accessor.setImmutable();
    } else if (message.getHeaders().containsKey(this.sendTimeoutHeader)
            || message.getHeaders().containsKey(this.receiveTimeoutHeader)) {
        messageToSend = MessageBuilder.fromMessage(message).setHeader(this.sendTimeoutHeader, null)
                .setHeader(this.receiveTimeoutHeader, null).build();
    }/*from  ww w . j av  a  2  s .com*/

    boolean sent = (timeout >= 0 ? channel.send(messageToSend, timeout) : channel.send(messageToSend));

    if (!sent) {
        throw new MessageDeliveryException(message,
                "Failed to send message to channel '" + channel + "' within timeout: " + timeout);
    }
}

From source file:org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.java

@Override
public void handleMessage(Message<?> message) throws MessagingException {
    String destination = getDestination(message);
    if (destination == null) {
        return;//from  w  w  w.j  a va2 s  .  c  o  m
    }
    String lookupDestination = getLookupDestination(destination);
    if (lookupDestination == null) {
        return;
    }
    MessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getMutableAccessor(message);
    headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, lookupDestination);
    headerAccessor.setLeaveMutable(true);
    message = MessageBuilder.createMessage(message.getPayload(), headerAccessor.getMessageHeaders());

    if (logger.isDebugEnabled()) {
        logger.debug("Searching methods to handle " + headerAccessor.getShortLogMessage(message.getPayload())
                + ", lookupDestination='" + lookupDestination + "'");
    }

    handleMessageInternal(message, lookupDestination);
    headerAccessor.setImmutable();
}