Spring integration spring集成中的尝试与建议
我创建了一个简单的示例来了解重试和建议。以下是示例配置:Spring integration spring集成中的尝试与建议,spring-integration,Spring Integration,我创建了一个简单的示例来了解重试和建议。以下是示例配置: @Configuration @EnableIntegration @IntegrationComponentScan @ComponentScan public class SimpleConfiguration2 { @Bean public DirectChannel requestChannel() { return new DirectChannel(); } @Bean public DirectC
@Configuration
@EnableIntegration
@IntegrationComponentScan
@ComponentScan
public class SimpleConfiguration2 {
@Bean
public DirectChannel requestChannel() {
return new DirectChannel();
}
@Bean
public DirectChannel aChannel() {
return new DirectChannel();
}
@Bean
public DirectChannel retryLaterChannel() {
return new DirectChannel();
}
@Bean
public DirectChannel tapChannel() {
return new DirectChannel();
}
@Bean
public DirectChannel endChannel() {
return new DirectChannel();
}
@Bean
public ExceptionThrowingTransformer exceptionThrowingTransformer() {
return new ExceptionThrowingTransformer();
}
@Bean
public IntegrationFlow initFlow() {
return IntegrationFlows.from(requestChannel())
.gateway(aChannel(), e -> e.advice(retryAdvice())
.errorChannel(retryLaterChannel()))
.get();
}
@Bean
public Advice retryAdvice() {
final RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
advice.setRetryTemplate(retryTemplate());
return advice;
}
@Bean
public RetryTemplate retryTemplate() {
final RetryTemplate ret = new RetryTemplate();
ret.setRetryPolicy(retryPolicy());
ret.setThrowLastExceptionOnExhausted(false);
return ret;
}
@Bean
public RetryPolicy retryPolicy() {
final Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>() {{
put(RuntimeException.class, true);
}
private static final long serialVersionUID = -1L;
};
final RetryPolicy ret = new SimpleRetryPolicy(3, map, true);
return ret;
}
@Bean
public IntegrationFlow aChannelFlow() {
return IntegrationFlows.from(aChannel())
.transform("C: "::concat)
.transform(exceptionThrowingTransformer())
.channel(endChannel())
.get();
}
@Bean
public IntegrationFlow endFlow() {
return IntegrationFlows.from(endChannel())
.handle(String.class, (p, h) -> p)
.get();
}
@Bean
public IntegrationFlow retryLaterFlow() {
return IntegrationFlows.from(retryLaterChannel())
.wireTap(tapChannel())
// .bridge(null)
// .handle(String.class, (p, h) -> p)
.get();
}
@Bean
public IntegrationFlow tapFlow() {
return IntegrationFlows.from(tapChannel())
.handle(m -> System.err.println("tap [" + Thread.currentThread().getName() + "] " + m.getPayload() ))
.get();
}
}
和回音网关:
@MessagingGateway(defaultReplyTimeout=600)
public interface EchoGateway {
@Gateway(requestChannel = "requestChannel")
String echo(String message);
}
问题1:
如果按原样运行代码,对于放置在requestChannel上的输入“MSG1”,我将获得以下输出:
main ExceptionThrowingTransformer will fail 1 more times
tap [main] org.springframework.integration.transformer.MessageTransformationException: ; nested exception is org.springframework.messaging.MessageHandlingException: ; nested exception is java.lang.RuntimeException
main ExceptionThrowingTransformer last failure
tap [main] org.springframework.integration.transformer.MessageTransformationException: ; nested exception is org.springframework.messaging.MessageHandlingException: ; nested exception is java.lang.RuntimeException
main C: MSG1
而我希望看到的是:
main ExceptionThrowingTransformer will fail 1 more times
main ExceptionThrowingTransformer last failure
main C: MSG1
重试会发生,但原始异常仍会传播到initFlow网关中指定的errorChannel。有没有办法防止这种情况发生
问题2:
当numFail更改为3时,除了问题1中的输出外,还会向调用者返回一个异常:
org.springframework.messaging.MessagingException: failure occurred in error-handling flow; nested exception is org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'application:-1.retryLaterChannel'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers
如果只有retryLaterFlow中的第一条注释未注释,则exceptionThrowingTransformer引发的异常将返回给调用方。
既然retryTemplate已经设置了setThrowLastExceptionOnExhausted(false),为什么会这样
问题3:
如果retryLaterFlow中只有第二条注释未注释,则输出如下所示:
main ExceptionThrowingTransformer will fail 2 more times
tap [main] org.springframework.integration.transformer.MessageTransformationException: ; nested exception is org.springframework.messaging.MessageHandlingException: ; nested exception is java.lang.RuntimeException
org.springframework.integration.transformer.MessageTransformationException: ; nested exception is org.springframework.messaging.MessageHandlingException: ; nested exception is java.lang.RuntimeException
为什么重试没有生效?(对问题1的回答也可以回答此问题)您有两个网关(初始和中流),您正在建议中流网关,以便它按预期工作
您应该建议变压器获得您期望的结果
org.springframework.messaging.MessagingException: failure occurred in error-handling flow; nested exception is org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'application:-1.retryLaterChannel'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers
main ExceptionThrowingTransformer will fail 2 more times
tap [main] org.springframework.integration.transformer.MessageTransformationException: ; nested exception is org.springframework.messaging.MessageHandlingException: ; nested exception is java.lang.RuntimeException
org.springframework.integration.transformer.MessageTransformationException: ; nested exception is org.springframework.messaging.MessageHandlingException: ; nested exception is java.lang.RuntimeException