List of usage examples for org.springframework.messaging.support MessageHeaderAccessor getMessageHeaders
public MessageHeaders getMessageHeaders()
From source file:org.springframework.cloud.sleuth.instrument.messaging.TracingChannelInterceptor.java
private Message<?> outputMessage(Message<?> originalMessage, Message<?> retrievedMessage, MessageHeaderAccessor additionalHeaders) { MessageHeaderAccessor headers = MessageHeaderAccessor.getMutableAccessor(originalMessage); if (originalMessage.getPayload() instanceof MessagingException) { headers.copyHeaders(MessageHeaderPropagation.propagationHeaders(additionalHeaders.getMessageHeaders(), this.tracing.propagation().keys())); return new ErrorMessage((MessagingException) originalMessage.getPayload(), isWebSockets(headers) ? headers.getMessageHeaders() : new MessageHeaders(headers.getMessageHeaders())); }//from w ww .ja v a 2s. c om headers.copyHeaders(additionalHeaders.getMessageHeaders()); return new GenericMessage<>(retrievedMessage.getPayload(), isWebSockets(headers) ? headers.getMessageHeaders() : new MessageHeaders(headers.getMessageHeaders())); }
From source file:org.springframework.cloud.sleuth.instrument.messaging.TracingChannelInterceptor.java
private boolean isWebSockets(MessageHeaderAccessor headerAccessor) { return headerAccessor.getMessageHeaders().containsKey("stompCommand") || headerAccessor.getMessageHeaders().containsKey("simpMessageType"); }
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. *///from ww w . ja va 2 s .c o 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 w w . ja v a 2s .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.converter.AbstractMessageConverter.java
@Override @Nullable/*from w w w . j av a2 s. c om*/ public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) { if (!canConvertTo(payload, headers)) { return null; } Object payloadToUse = convertToInternal(payload, headers, conversionHint); if (payloadToUse == null) { return null; } MimeType mimeType = getDefaultContentType(payloadToUse); if (headers != null) { MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class); if (accessor != null && accessor.isMutable()) { if (mimeType != null) { accessor.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType); } return MessageBuilder.createMessage(payloadToUse, accessor.getMessageHeaders()); } } MessageBuilder<?> builder = MessageBuilder.withPayload(payloadToUse); if (headers != null) { builder.copyHeaders(headers); } if (mimeType != null) { builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType); } return builder.build(); }
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(); }