下面列出了怎么用org.springframework.messaging.core.MessagePostProcessor的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
}
@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);
}
}
@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);
}
@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);
}
}
@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);
}
@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);
}
@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);
}
@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);
}
}
@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);
}
@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);
}
}
@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);
}
@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);
}
@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();
}
@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;
}
@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);
}
@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);
}
}
@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);
}
@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);
}
}
@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);
}
@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);
}
@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());
}
@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());
}
@Override
public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException {
convertAndSendToUser(user, destination, payload, (MessagePostProcessor) null);
}
@Override
public void convertAndSendToUser(String user, String destination, Object payload,
@Nullable MessagePostProcessor postProcessor) throws MessagingException {
convertAndSendToUser(user, destination, payload, null, postProcessor);
}
@Override
public void convertAndSend(String destinationName, Object payload, @Nullable MessagePostProcessor postProcessor)
throws MessagingException {
convertAndSend(destinationName, payload, null, postProcessor);
}
@Override
public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException {
convertAndSendToUser(user, destination, payload, (MessagePostProcessor) null);
}
@Override
public void convertAndSendToUser(String user, String destination, Object payload,
@Nullable MessagePostProcessor postProcessor) throws MessagingException {
convertAndSendToUser(user, destination, payload, null, postProcessor);
}
@Override
public void convertAndSend(String destinationName, Object payload, @Nullable MessagePostProcessor postProcessor)
throws MessagingException {
convertAndSend(destinationName, payload, null, postProcessor);
}
@Override
public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException {
convertAndSendToUser(user, destination, payload, (MessagePostProcessor) null);
}
@Override
public void convertAndSendToUser(String user, String destination, Object payload,
MessagePostProcessor postProcessor) throws MessagingException {
convertAndSendToUser(user, destination, payload, null, postProcessor);
}