我有一个 JMS 监听器。阅读消息后,我将转换为自定义对象
public IntegrationFlow queueProcessorFlow() {
return IntegrationFlows.from(Jms.inboundAdapter(jmsTemplate)
.destination("test_queue"),
c -> c.poller(Pollers.fixedDelay(5000L)
.maxMessagesPerPoll(1)))
//convert json to our custom object
.transform(new JsonToQueueEventConverterTransformer(springBeanFactory))
.transform(new CustomTransformer(springBeanFactory))
.handle(o -> {
}).get();
}
变压器
public class CustomerTransformer implements GenericTransformer<CustomPojo, CustomPojo> {
private final QueueDataProcessorSpringBeanFactory factory;
@Override
public CustomPojo transform(CustomPojo CustomPojo) {
try {
//do something e.g. service call
throw new Exception("This failed mate !! SOS");
} catch (Exception e) {
//ISSUE here
//e contains the original payload in the stack trace
throw new RuntimeException(e);
}
return CustomPojo;
}
现在,当我抛出自定义异常时,堆栈跟踪包含所有内容。它甚至包含有效负载。我对异常情况下的有效负载不感兴趣。
如何更新以不包含负载?
** 更新 **
根据答案进行更改后,我仍然看到问题
org.springframework.integration.transformer.MessageTransformationException: Failed to transform Message; nested exception is org.springframework.messaging.MessageHandlingException: nested exception is org.springframework.integration.transformer.MessageTransformationException: Error initiliazing the :; nested exception is CustomException Error lab lab lab , failedMessage=GenericMessage [payload=
我的错误处理程序
@Bean
public IntegrationFlow errorHandlingFlow() {
return IntegrationFlows.from("errorChannel")
.handle(message -> {
try {
ErrorMessage e = (ErrorMessage) message;
if (e.getPayload() instanceof MessageTransformationException) {
String stackTrace = ExceptionUtils.getStackTrace(e.getPayload());
LOG.info("Exception trace {} ", stackTrace);
最佳答案
不确定失去payload
的商业目的是什么在堆栈跟踪中,但您可以通过抛出 MessageTransformationException
来实现而不是RuntimeException
.
为了避免在堆栈跟踪中出现具有上述有效负载的消息,您需要使用以下构造函数之一:
public MessageTransformationException(String description, Throwable cause) {
super(description, cause);
}
public MessageTransformationException(String description) {
super(description);
}
而不是那些基于Message<?>
的.
这样包装MessageTransformingHandler
将执行适当的逻辑:
protected Object handleRequestMessage(Message<?> message) {
try {
return this.transformer.transform(message);
}
catch (Exception e) {
if (e instanceof MessageTransformationException) {
throw (MessageTransformationException) e;
}
throw new MessageTransformationException(message, "Failed to transform Message", e);
}
}
更新
结果是MessageTransformationException
还不够,因为 AbstractMessageHandler
检查 MessageHandlingException
用于包裹IntegrationUtils.wrapInHandlingExceptionIfNecessary()
。因此我建议抛出 MessageHandlingException
从你的代码代替。并将此构造函数与 null
一起使用对于消息参数:
MessageHandlingException(Message<?> failedMessage, Throwable cause)
关于java - Spring Integration 错误附加已完成的有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53596124/