Java Code Examples for org.springframework.messaging.support.MessageHeaderAccessor#setImmutable()

The following examples show how to use org.springframework.messaging.support.MessageHeaderAccessor#setImmutable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractMethodMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	String destination = getDestination(message);
	if (destination == null) {
		return;
	}
	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();
}
 
Example 2
Source File: GenericMessagingTemplate.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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();
	}

	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);
	}
}
 
Example 3
Source File: AbstractMethodMessageHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	String destination = getDestination(message);
	if (destination == null) {
		return;
	}
	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();
}
 
Example 4
Source File: GenericMessagingTemplate.java    From java-technology-stack with MIT License 6 votes vote down vote up
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();
	}

	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);
	}
}
 
Example 5
Source File: AbstractMethodMessageHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	String destination = getDestination(message);
	if (destination == null) {
		return;
	}
	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()));
	}

	handleMessageInternal(message, lookupDestination);
	headerAccessor.setImmutable();
}
 
Example 6
Source File: GenericMessagingTemplate.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected final void doSend(MessageChannel channel, Message<?> message) {
	Assert.notNull(channel, "'channel' is required");

	MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
	if (accessor != null && accessor.isMutable()) {
		accessor.setImmutable();
	}

	long timeout = this.sendTimeout;
	boolean sent = (timeout >= 0 ? channel.send(message, timeout) : channel.send(message));

	if (!sent) {
		throw new MessageDeliveryException(message,
				"failed to send message to channel '" + channel + "' within timeout: " + timeout);
	}
}
 
Example 7
Source File: TracingChannelInterceptor.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
/**
 * Use this to create a span for processing the given message. Note: the result has no
 * name and is not started. This creates a child from identifiers extracted from the
 * message headers, or a new span if one couldn't be extracted.
 * @param message message to use for span creation
 * @return span to be created
 */
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;
}
 
Example 8
Source File: TracingChannelInterceptor.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
 */
@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(toRemoteServiceName(headers));
		addTags(message, span, channel);
	}
	if (log.isDebugEnabled()) {
		log.debug("Created a new span in post receive " + span);
	}
	headers.setImmutable();
	if (message instanceof ErrorMessage) {
		ErrorMessage errorMessage = (ErrorMessage) message;
		return new ErrorMessage(errorMessage.getPayload(),
				headers.getMessageHeaders(), errorMessage.getOriginalMessage());
	}
	return new GenericMessage<>(message.getPayload(), headers.getMessageHeaders());
}
 
Example 9
Source File: TracingChannelInterceptor.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
 */
@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());
}