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

The following examples show how to use org.springframework.messaging.support.MessageHeaderAccessor#getMessageHeaders() . 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: TracingChannelInterceptor.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
private Message<?> outputMessage(Message<?> originalMessage,
		Message<?> retrievedMessage, MessageHeaderAccessor additionalHeaders) {
	MessageHeaderAccessor headers = MessageHeaderAccessor
			.getMutableAccessor(originalMessage);
	if (originalMessage instanceof ErrorMessage) {
		ErrorMessage errorMessage = (ErrorMessage) originalMessage;
		headers.copyHeaders(MessageHeaderPropagation.propagationHeaders(
				additionalHeaders.getMessageHeaders(),
				this.tracing.propagation().keys()));
		return new ErrorMessage(errorMessage.getPayload(),
				isWebSockets(headers) ? headers.getMessageHeaders()
						: new MessageHeaders(headers.getMessageHeaders()),
				errorMessage.getOriginalMessage());
	}
	headers.copyHeaders(additionalHeaders.getMessageHeaders());
	return new GenericMessage<>(retrievedMessage.getPayload(),
			isWebSockets(headers) ? headers.getMessageHeaders()
					: new MessageHeaders(headers.getMessageHeaders()));
}
 
Example 2
Source File: MessagingRSocket.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private MessageHeaders createHeaders(String destination, @Nullable MonoProcessor<?> replyMono) {
	MessageHeaderAccessor headers = new MessageHeaderAccessor();
	headers.setLeaveMutable(true);
	headers.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, destination);
	if (this.dataMimeType != null) {
		headers.setContentType(this.dataMimeType);
	}
	headers.setHeader(RSocketRequesterMethodArgumentResolver.RSOCKET_REQUESTER_HEADER, this.requester);
	if (replyMono != null) {
		headers.setHeader(RSocketPayloadReturnValueHandler.RESPONSE_HEADER, replyMono);
	}
	headers.setHeader(HandlerMethodReturnValueHandler.DATA_BUFFER_FACTORY_HEADER, this.bufferFactory);
	return headers.getMessageHeaders();
}
 
Example 3
Source File: SimpleMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void toMessageWithPayloadAndMutableHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("foo", "bar");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	Message<?> message = this.converter.toMessage("payload", headers);

	assertEquals("payload", message.getPayload());
	assertSame(headers, message.getHeaders());
	assertEquals("bar", message.getHeaders().get("foo"));
}
 
Example 4
Source File: MessageSendingTemplateTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void convertAndSendPayloadAndMutableHeadersToDestination() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("foo", "bar");
	accessor.setLeaveMutable(true);
	MessageHeaders messageHeaders = accessor.getMessageHeaders();

	this.template.setMessageConverter(new StringMessageConverter());
	this.template.convertAndSend("somewhere", "payload", messageHeaders);

	MessageHeaders actual = this.template.message.getHeaders();
	assertSame(messageHeaders, actual);
	assertEquals(new MimeType("text", "plain", StandardCharsets.UTF_8), actual.get(MessageHeaders.CONTENT_TYPE));
	assertEquals("bar", actual.get("foo"));
}
 
Example 5
Source File: GenericMessagingTemplateTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void convertAndSendWithSimpMessageHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("key", "value");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	this.template.convertAndSend("channel", "data", headers);
	List<Message<byte[]>> messages = this.messageChannel.getMessages();
	Message<byte[]> message = messages.get(0);

	assertSame(headers, message.getHeaders());
	assertFalse(accessor.isMutable());
}
 
Example 6
Source File: SimpleMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void toMessageWithPayloadAndMutableHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("foo", "bar");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	Message<?> message = this.converter.toMessage("payload", headers);

	assertEquals("payload", message.getPayload());
	assertSame(headers, message.getHeaders());
	assertEquals("bar", message.getHeaders().get("foo"));
}
 
Example 7
Source File: MessageSendingTemplateTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void convertAndSendPayloadAndMutableHeadersToDestination() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("foo", "bar");
	accessor.setLeaveMutable(true);
	MessageHeaders messageHeaders = accessor.getMessageHeaders();

	this.template.setMessageConverter(new StringMessageConverter());
	this.template.convertAndSend("somewhere", "payload", messageHeaders);

	MessageHeaders actual = this.template.message.getHeaders();
	assertSame(messageHeaders, actual);
	assertEquals(new MimeType("text", "plain", StandardCharsets.UTF_8), actual.get(MessageHeaders.CONTENT_TYPE));
	assertEquals("bar", actual.get("foo"));
}
 
Example 8
Source File: GenericMessagingTemplateTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void convertAndSendWithSimpMessageHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("key", "value");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	this.template.convertAndSend("channel", "data", headers);
	List<Message<byte[]>> messages = this.messageChannel.getMessages();
	Message<byte[]> message = messages.get(0);

	assertSame(headers, message.getHeaders());
	assertFalse(accessor.isMutable());
}
 
Example 9
Source File: SimpleMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void toMessageWithPayloadAndMutableHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("foo", "bar");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	Message<?> message = this.converter.toMessage("payload", headers);

	assertEquals("payload", message.getPayload());
	assertSame(headers, message.getHeaders());
	assertEquals("bar", message.getHeaders().get("foo"));
}
 
Example 10
Source File: MessageSendingTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void convertAndSendPayloadAndMutableHeadersToDestination() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("foo", "bar");
	accessor.setLeaveMutable(true);
	MessageHeaders messageHeaders = accessor.getMessageHeaders();

	this.template.setMessageConverter(new StringMessageConverter());
	this.template.convertAndSend("somewhere", "payload", messageHeaders);

	MessageHeaders actual = this.template.message.getHeaders();
	assertSame(messageHeaders, actual);
	assertEquals(new MimeType("text", "plain", Charset.forName("UTF-8")), actual.get(MessageHeaders.CONTENT_TYPE));
	assertEquals("bar", actual.get("foo"));
}
 
Example 11
Source File: GenericMessagingTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void convertAndSendWithSimpMessageHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("key", "value");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	this.template.convertAndSend("channel", "data", headers);
	List<Message<byte[]>> messages = this.messageChannel.getMessages();
	Message<byte[]> message = messages.get(0);

	assertSame(headers, message.getHeaders());
	assertFalse(accessor.isMutable());
}
 
Example 12
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 13
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());
}