Spring 如何为<;添加断路器;内部:网关>;春季一体化

Spring 如何为<;添加断路器;内部:网关>;春季一体化,spring,spring-integration,Spring,Spring Integration,这是我的代码,我在spring-config.xml中定义了一个网关,如下所示 <int:gateway id="myGateway" service-interface="com.sample.si.MyService" error-channel="myError-channel" default-request-channel="sendServiceChannel"

这是我的代码,我在spring-config.xml中定义了一个网关,如下所示

<int:gateway id="myGateway"
                 service-interface="com.sample.si.MyService"
                 error-channel="myError-channel"
                 default-request-channel="sendServiceChannel" 
                 >
        <int:method name="sendService" request-channel="sendServiceChannel" /       
}

错误处理程序是一个将异常消息发送到队列并将其读回并再次发送到网关的过程。这是我的错误处理程序代码

public class ErrorHandler{@Inject private JmsTemplate jmsTemplate;
     public void resolveError( Message<MessagingException> message) {
         try{ 
             MyRequest myRequest=(MessageRequest)message.getPayload().getFailedMessage().getPayload()
             jmsTemplate.convertAndSend(DATA_QUEUE, myRequest);
        } catch(Exception e){
    //log error
        } 
    }
}
例外情况:

Caused by: org.springframework.ws.client.WebServiceIOException: I/O error: Connect to localhost:10002 timed out; nested exception is org.apache.http.conn.ConnectTimeoutException: Connect to localhost:10002 timed out
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:561)
at org.springframework.integration.ws.MarshallingWebServiceOutboundGateway.doHandle(MarshallingWebServiceOutboundGateway.java:81)
at org.springframework.integration.ws.AbstractWebServiceOutboundGateway.handleRequestMessage(AbstractWebServiceOutboundGateway.java:188)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler$AdvisedRequestHandler.handleRequestMessage(AbstractReplyProducingMessageHandler.java:144)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice$1.execute(AbstractRequestHandlerAdvice.java:74)
at org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice.doInvoke(RequestHandlerCircuitBreakerAdvice.java:61)
at org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice.invoke(AbstractRequestHandlerAdvice.java:69)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy102.handleRequestMessage(Unknown Source)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.doInvokeAdvisedRequestHandler(AbstractReplyProducingMessageHandler.java:117)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:102)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
... 90 more
网关(以及错误通道)位于断路器的下游-您需要显示(在)服务激活器的上游-这是您需要配置错误通道以处理断路器结果的地方

这就是说,这里有一个循环依赖关系-网关正在发送到服务激活器,服务激活器正在再次调用网关

您需要进一步解释您试图做什么-显示更多配置-上游和下游

编辑

请记住,这将导致无限循环,因为失败的消息将无限期重试

与此测试用例相比,发送到JMS不会有任何不同,它的工作方式与我预期的完全相同(包括无限循环)


以上是我为实施断路器而编辑的配置,经过所有更改后,我仍然存在此问题,即半开后时间,我将其增加到60000,并且它起作用了问题中的上述解决方案应适用于断路器,请关注我和Gary Russell的评论。

有时间的话,可以用outbound gateway编辑一下吗,谢谢!!正如我所说,您的配置完全错误
defaultrequest channel=“sendServiceChannel”
发送一条消息,该消息再次调用网关。您还将ws-outbound网关订阅到同一频道。service activator需要订阅从某个上游组件获取消息的其他通道。我将断路器移动到了出站网关,如上所示,现在它在一定程度上工作,当超时异常出现时,它将重定向到错误通道,并将断路器的状态更改为打开,但是,如果我立即尝试将消息从errorhandler发送到outbound gateway,并且在timelimit之后不到半个open,那么它不应该命中URI,对吗?但我仍然得到超时异常(在WebServiceIOException类上有一个调试点),然后再次返回ErrorChannel异常是断路器显式抛出的还是断路器不工作?我正在使用测试用例进行测试。您需要显示您的测试用例-编辑问题。
public class EmailGatewayProducerConsumerTest {

@Inject
@Qualifier("myGateway")
private Myservice myService;

@Test
public void senderTest(){
MyRequest myRequest = new MyRequest(12,"rajendra","rajednra.ch@hotmail.com");
           try {
        myService.sendService(myRequest);
    }catch (Exception e){
        //System.out.println("Caught Exception " +e);
    }
}
public class ErrorHandler{@Inject private JmsTemplate jmsTemplate;
     public void resolveError( Message<MessagingException> message) {
         try{ 
             MyRequest myRequest=(MessageRequest)message.getPayload().getFailedMessage().getPayload()
             jmsTemplate.convertAndSend(DATA_QUEUE, myRequest);
        } catch(Exception e){
    //log error
        } 
    }
}
 @JmsListener(destination = DATA_QUEUE)public void consume(MyRequest myRequest) throws InterruptedException {
log.info("Receiving  event: {}", myRequest);
try {
   myService.sendService(myRequest)
}catch (Exception e){
    log.error(e.toString());
} }
Caused by: org.springframework.ws.client.WebServiceIOException: I/O error: Connect to localhost:10002 timed out; nested exception is org.apache.http.conn.ConnectTimeoutException: Connect to localhost:10002 timed out
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:561)
at org.springframework.integration.ws.MarshallingWebServiceOutboundGateway.doHandle(MarshallingWebServiceOutboundGateway.java:81)
at org.springframework.integration.ws.AbstractWebServiceOutboundGateway.handleRequestMessage(AbstractWebServiceOutboundGateway.java:188)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler$AdvisedRequestHandler.handleRequestMessage(AbstractReplyProducingMessageHandler.java:144)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice$1.execute(AbstractRequestHandlerAdvice.java:74)
at org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice.doInvoke(RequestHandlerCircuitBreakerAdvice.java:61)
at org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice.invoke(AbstractRequestHandlerAdvice.java:69)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy102.handleRequestMessage(Unknown Source)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.doInvokeAdvisedRequestHandler(AbstractReplyProducingMessageHandler.java:117)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:102)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
... 90 more
public interface GW {

    void send(String foo);

}


<int:gateway id="gw" service-interface="com.example.GW" default-request-channel="foo"
    error-channel="errorChannel"/>

<int:service-activator input-channel="foo" expression = "1 / 0"> <!-- always fail -->
    <int:request-handler-advice-chain>
        <bean class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
            <property name="threshold" value="1" />
            <property name="halfOpenAfter" value="10000" />
        </bean>
    </int:request-handler-advice-chain>
</int:service-activator>

<int:bridge input-channel="errorChannel" output-channel="retryChannel" />

<int:channel id="retryChannel">
    <int:queue />
</int:channel>

<int:chain input-channel="retryChannel">
    <int:transformer expression="payload.failedMessage.payload" />
    <int:service-activator ref="gw" />
    <int:poller fixed-delay="1000" />
</int:chain>
org.springframework.messaging.MessageHandlingException: Expression evaluation failed: 1 / 0;
...
Caused by: java.lang.ArithmeticException: / by zero

CircuitBreakerOpenException: Circuit Breaker is Open for ServiceActivator for [ExpressionEvaluatingMessageProcessor for: [1 / 0]] 

CircuitBreakerOpenException: Circuit Breaker is Open for ServiceActivator for [ExpressionEvaluatingMessageProcessor for: [1 / 0]] 

CircuitBreakerOpenException: Circuit Breaker is Open for ServiceActivator for [ExpressionEvaluatingMessageProcessor for: [1 / 0]]