org.springframework.messaging.core.MessagePostProcessor Java Examples

The following examples show how to use org.springframework.messaging.core.MessagePostProcessor. 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: JmsMessagingTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T convertSendAndReceive(String destinationName, Object request, @Nullable Map<String, Object> headers,
		Class<T> targetClass, @Nullable MessagePostProcessor postProcessor) {

	Message<?> requestMessage = doConvert(request, headers, postProcessor);
	Message<?> replyMessage = sendAndReceive(destinationName, requestMessage);
	return (replyMessage != null ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null);
}
 
Example #2
Source File: AbstractMessageChannelMessagingSendingTemplateTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void convertAndSend_WithDestinationNamePayloadHeadersAndPostProcessor_shouldResolveTheDestinationSendTheConvertedMessageAndCallPostProcessor()
		// @checkstyle:on
		throws Exception {
	// Arrange
	MessageSendingTemplateTest messageSendingTemplate = new MessageSendingTemplateTest(
			this.destinationResolver);
	when(this.destinationResolver.resolveDestination("destination"))
			.thenReturn("resolvedDestination");
	MessagePostProcessor messagePostProcessor = mock(MessagePostProcessor.class);
	when(messagePostProcessor.postProcessMessage(ArgumentMatchers.any()))
			.thenAnswer((Answer<Message<?>>) invocation -> (Message<?>) invocation
					.getArguments()[0]);

	Map<String, Object> headers = Collections.singletonMap("headerKey",
			"headerValue");
	String payload = "payload";

	// Act
	messageSendingTemplate.convertAndSend("destination", payload, headers,
			messagePostProcessor);

	// Assert
	verify(this.destinationResolver).resolveDestination("destination");
	assertThat(
			messageSendingTemplate.getMessageChannel().getSentMessage().getPayload())
					.isEqualTo(payload);
	assertThat(messageSendingTemplate.getMessageChannel().getSentMessage()
			.getHeaders().get("headerKey")).isEqualTo(headers.get("headerKey"));
	verify(messagePostProcessor).postProcessMessage(
			messageSendingTemplate.getMessageChannel().getSentMessage());
}
 
Example #3
Source File: AbstractMessageChannelMessagingSendingTemplateTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void convertAndSend_WithDestinationNamePayloadAndPostProcessor_shouldResolveTheDestinationSendTheConvertedMessageAndCallPostProcessor()
		throws Exception {
	// @checkstyle:on
	// Arrange
	MessageSendingTemplateTest messageSendingTemplate = new MessageSendingTemplateTest(
			this.destinationResolver);
	when(this.destinationResolver.resolveDestination("destination"))
			.thenReturn("resolvedDestination");
	MessagePostProcessor messagePostProcessor = mock(MessagePostProcessor.class);
	when(messagePostProcessor.postProcessMessage(ArgumentMatchers.any()))
			.thenAnswer((Answer<Message<?>>) invocation -> (Message<?>) invocation
					.getArguments()[0]);

	String payload = "payload";

	// Act
	messageSendingTemplate.convertAndSend("destination", payload,
			messagePostProcessor);

	// Assert
	verify(this.destinationResolver).resolveDestination("destination");
	assertThat(
			messageSendingTemplate.getMessageChannel().getSentMessage().getPayload())
					.isEqualTo(payload);
	verify(messagePostProcessor).postProcessMessage(
			messageSendingTemplate.getMessageChannel().getSentMessage());
}
 
Example #4
Source File: AbstractMessageChannelMessagingSendingTemplate.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Override
public <T> void convertAndSend(String destinationName, T payload,
		Map<String, Object> headers, MessagePostProcessor postProcessor)
		throws MessagingException {
	D channel = resolveMessageChannelByLogicalName(destinationName);
	convertAndSend(channel, payload, headers, postProcessor);
}
 
Example #5
Source File: JmsMessagingTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> T convertSendAndReceive(String destinationName, Object request, Map<String, Object> headers,
		Class<T> targetClass, MessagePostProcessor postProcessor) {

	Message<?> requestMessage = doConvert(request, headers, postProcessor);
	Message<?> replyMessage = sendAndReceive(destinationName, requestMessage);
	return (replyMessage != null ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null);
}
 
Example #6
Source File: JmsMessagingTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T convertSendAndReceive(Object request, Class<T> targetClass, MessagePostProcessor postProcessor) {
	Destination defaultDestination = getDefaultDestination();
	if (defaultDestination != null) {
		return convertSendAndReceive(defaultDestination, request, targetClass, postProcessor);
	}
	else {
		return convertSendAndReceive(getRequiredDefaultDestinationName(), request, targetClass, postProcessor);
	}
}
 
Example #7
Source File: JmsMessagingTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void convertAndSend(String destinationName, Object payload, Map<String, Object> headers,
		MessagePostProcessor postProcessor) throws MessagingException {

	Message<?> message = doConvert(payload, headers, postProcessor);
	send(destinationName, message);
}
 
Example #8
Source File: JmsMessagingTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void convertAndSend(Object payload, MessagePostProcessor postProcessor) throws MessagingException {
	Destination defaultDestination = getDefaultDestination();
	if (defaultDestination != null) {
		convertAndSend(defaultDestination, payload, postProcessor);
	}
	else {
		convertAndSend(getRequiredDefaultDestinationName(), payload, postProcessor);
	}
}
 
Example #9
Source File: SimpMessagingTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void convertAndSendToUser(String user, String destination, Object payload, Map<String, Object> headers,
		MessagePostProcessor postProcessor) throws MessagingException {

	Assert.notNull(user, "User must not be null");
	user = StringUtils.replace(user, "/", "%2F");
	super.convertAndSend(this.destinationPrefix + user + destination, payload, headers, postProcessor);
}
 
Example #10
Source File: RocketMQTemplate.java    From spring-boot-starter-rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
protected Message<?> doConvert(Object payload, Map<String, Object> headers, MessagePostProcessor postProcessor) {
    String content;
    if (payload instanceof String) {
        content = (String) payload;
    } else {
        // if payload not as string, use objectMapper change it.
        try {
            content = objectMapper.writeValueAsString(payload);
        } catch (JsonProcessingException e) {
            log.info("convert payload to String failed. payload:{}", payload);
            throw new RuntimeException("convert to payload to String failed.", e);
        }
    }

    MessageBuilder<?> builder = MessageBuilder.withPayload(content);
    if (headers != null) {
        builder.copyHeaders(headers);
    }
    builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN);

    Message<?> message = builder.build();
    if (postProcessor != null) {
        message = postProcessor.postProcessMessage(message);
    }
    return message;
}
 
Example #11
Source File: RocketMQTemplate.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
@Override
protected Message<?> doConvert(Object payload, Map<String, Object> headers, MessagePostProcessor postProcessor) {
    Message<?> message = super.doConvert(payload, headers, postProcessor);
    MessageBuilder<?> builder = MessageBuilder.fromMessage(message);
    builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN);
    return builder.build();
}
 
Example #12
Source File: JmsMessagingTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T convertSendAndReceive(String destinationName, Object request, Class<T> targetClass,
		@Nullable MessagePostProcessor requestPostProcessor) throws MessagingException {

	return convertSendAndReceive(destinationName, request, null, targetClass, requestPostProcessor);
}
 
Example #13
Source File: JmsMessagingTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T convertSendAndReceive(Object request, Class<T> targetClass, @Nullable MessagePostProcessor postProcessor) {
	Destination defaultDestination = getDefaultDestination();
	if (defaultDestination != null) {
		return convertSendAndReceive(defaultDestination, request, targetClass, postProcessor);
	}
	else {
		return convertSendAndReceive(getRequiredDefaultDestinationName(), request, targetClass, postProcessor);
	}
}
 
Example #14
Source File: JmsMessagingTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void convertAndSend(String destinationName, Object payload, @Nullable Map<String, Object> headers,
		@Nullable MessagePostProcessor postProcessor) throws MessagingException {

	Message<?> message = doConvert(payload, headers, postProcessor);
	send(destinationName, message);
}
 
Example #15
Source File: JmsMessagingTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void convertAndSend(Object payload, @Nullable MessagePostProcessor postProcessor) throws MessagingException {
	Destination defaultDestination = getDefaultDestination();
	if (defaultDestination != null) {
		convertAndSend(defaultDestination, payload, postProcessor);
	}
	else {
		convertAndSend(getRequiredDefaultDestinationName(), payload, postProcessor);
	}
}
 
Example #16
Source File: SimpMessagingTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void convertAndSendToUser(String user, String destination, Object payload,
		@Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor)
		throws MessagingException {

	Assert.notNull(user, "User must not be null");
	user = StringUtils.replace(user, "/", "%2F");
	destination = destination.startsWith("/") ? destination : "/" + destination;
	super.convertAndSend(this.destinationPrefix + user + destination, payload, headers, postProcessor);
}
 
Example #17
Source File: JmsMessagingTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void convertAndSend(String destinationName, Object payload, @Nullable Map<String, Object> headers,
		@Nullable MessagePostProcessor postProcessor) throws MessagingException {

	Message<?> message = doConvert(payload, headers, postProcessor);
	send(destinationName, message);
}
 
Example #18
Source File: JmsMessagingTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T convertSendAndReceive(Object request, Class<T> targetClass, @Nullable MessagePostProcessor postProcessor) {
	Destination defaultDestination = getDefaultDestination();
	if (defaultDestination != null) {
		return convertSendAndReceive(defaultDestination, request, targetClass, postProcessor);
	}
	else {
		return convertSendAndReceive(getRequiredDefaultDestinationName(), request, targetClass, postProcessor);
	}
}
 
Example #19
Source File: JmsMessagingTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void convertAndSend(Object payload, @Nullable MessagePostProcessor postProcessor) throws MessagingException {
	Destination defaultDestination = getDefaultDestination();
	if (defaultDestination != null) {
		convertAndSend(defaultDestination, payload, postProcessor);
	}
	else {
		convertAndSend(getRequiredDefaultDestinationName(), payload, postProcessor);
	}
}
 
Example #20
Source File: JmsMessagingTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T convertSendAndReceive(String destinationName, Object request, Class<T> targetClass,
		@Nullable MessagePostProcessor requestPostProcessor) throws MessagingException {

	return convertSendAndReceive(destinationName, request, null, targetClass, requestPostProcessor);
}
 
Example #21
Source File: JmsMessagingTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T convertSendAndReceive(String destinationName, Object request, @Nullable Map<String, Object> headers,
		Class<T> targetClass, @Nullable MessagePostProcessor postProcessor) {

	Message<?> requestMessage = doConvert(request, headers, postProcessor);
	Message<?> replyMessage = sendAndReceive(destinationName, requestMessage);
	return (replyMessage != null ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null);
}
 
Example #22
Source File: SimpMessagingTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void convertAndSendToUser(String user, String destination, Object payload,
		@Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor)
		throws MessagingException {

	Assert.notNull(user, "User must not be null");
	user = StringUtils.replace(user, "/", "%2F");
	destination = destination.startsWith("/") ? destination : "/" + destination;
	super.convertAndSend(this.destinationPrefix + user + destination, payload, headers, postProcessor);
}
 
Example #23
Source File: SimpMessagingTemplate.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException {
	convertAndSendToUser(user, destination, payload, (MessagePostProcessor) null);
}
 
Example #24
Source File: JmsMessagingTemplate.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void convertAndSend(String destinationName, Object payload, MessagePostProcessor postProcessor)
		throws MessagingException {

	convertAndSend(destinationName, payload, null, postProcessor);
}
 
Example #25
Source File: SimpMessagingTemplate.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void convertAndSendToUser(String user, String destination, Object payload,
		MessagePostProcessor postProcessor) throws MessagingException {

	convertAndSendToUser(user, destination, payload, null, postProcessor);
}
 
Example #26
Source File: SimpMessagingTemplate.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException {
	convertAndSendToUser(user, destination, payload, (MessagePostProcessor) null);
}
 
Example #27
Source File: JmsMessagingTemplate.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void convertAndSend(String destinationName, Object payload, @Nullable MessagePostProcessor postProcessor)
		throws MessagingException {

	convertAndSend(destinationName, payload, null, postProcessor);
}
 
Example #28
Source File: JmsMessagingTemplate.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T convertSendAndReceive(String destinationName, Object request, Class<T> targetClass,
		MessagePostProcessor requestPostProcessor) throws MessagingException {

	return convertSendAndReceive(destinationName, request, null, targetClass, requestPostProcessor);
}
 
Example #29
Source File: AbstractMessageChannelMessagingSendingTemplate.java    From spring-cloud-aws with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void convertAndSend(String destinationName, T payload,
		MessagePostProcessor postProcessor) throws MessagingException {
	D channel = resolveMessageChannelByLogicalName(destinationName);
	convertAndSend(channel, payload, postProcessor);
}
 
Example #30
Source File: SimpMessagingTemplate.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void convertAndSendToUser(String user, String destination, Object payload,
		@Nullable MessagePostProcessor postProcessor) throws MessagingException {

	convertAndSendToUser(user, destination, payload, null, postProcessor);
}