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

The following examples show how to use org.springframework.messaging.support.MessageHeaderAccessor#isMutable() . 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: 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 2
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 3
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 4
Source File: SimpleMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Message<?> toMessage(Object payload, @Nullable MessageHeaders headers) {
	if (headers != null) {
		MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class);
		if (accessor != null && accessor.isMutable()) {
			return MessageBuilder.createMessage(payload, accessor.getMessageHeaders());
		}
	}
	return MessageBuilder.withPayload(payload).copyHeaders(headers).build();
}
 
Example 5
Source File: AbstractMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
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();
}
 
Example 6
Source File: SimpleMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Message<?> toMessage(Object payload, @Nullable MessageHeaders headers) {
	if (headers != null) {
		MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class);
		if (accessor != null && accessor.isMutable()) {
			return MessageBuilder.createMessage(payload, accessor.getMessageHeaders());
		}
	}
	return MessageBuilder.withPayload(payload).copyHeaders(headers).build();
}
 
Example 7
Source File: AbstractMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
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();
}
 
Example 8
Source File: SimpleMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Message<?> toMessage(Object payload, MessageHeaders headers) {
	if (payload == null) {
		return null;
	}
	if (headers != null) {
		MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class);
		if (accessor != null && accessor.isMutable()) {
			return MessageBuilder.createMessage(payload, accessor.getMessageHeaders());
		}
	}
	return MessageBuilder.withPayload(payload).copyHeaders(headers).build();
}
 
Example 9
Source File: AbstractMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public final Message<?> toMessage(Object payload, MessageHeaders headers, Object conversionHint) {
	if (!canConvertTo(payload, headers)) {
		return null;
	}

	payload = convertToInternal(payload, headers, conversionHint);
	if (payload == null) {
		return null;
	}

	MimeType mimeType = getDefaultContentType(payload);
	if (headers != null) {
		MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class);
		if (accessor != null && accessor.isMutable()) {
			accessor.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
			return MessageBuilder.createMessage(payload, accessor.getMessageHeaders());
		}
	}

	MessageBuilder<?> builder = MessageBuilder.withPayload(payload);
	if (headers != null) {
		builder.copyHeaders(headers);
	}
	builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
	return builder.build();
}