首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring与http-client一起发行

Spring与http-client一起发行
EN

Stack Overflow用户
提问于 2015-11-20 03:29:53
回答 2查看 147关注 0票数 1

我有spring模块,以rabbitmq作为传输工具。我的模块有http源http客户端处理器,它调用rest url http://x.y.z/test

代码语言:javascript
运行
复制
stream create --name cycletest4 --definition "http |  http-client --url='''https://x.y.z/test''' --httpMethod=GET | log" 

http post --data '{ "messageAttribute": { "channelType" : "EML", "contentKey" : "20020", "messageFormat" : "1", "contentSubscriber" : "dmttts", "languageCode" : "en-ca" }, "substitutionKeyValueData" : { "SvcgLOBCd": "CA", "User": "user", "phone": "yyyy, "accountLast": "tttt", "userName": "LP", "Company": "bbbb", "firstName": "Ryan" } }'

现在,当我的rest客户端抛出任何异常,如404或连接超时异常时,消息将返回http_

我的理解是,只有连接超时,异常才会被放回队列,或者任何其他异常或200将消息移动到下一个组件,当我尝试时,它是http-client\ log.But,所有的异常都被放回了http/http-client之间的队列。

现在,我的用法是,我想重试所有的套接字时间,/connection超时异常,.any,其他系统异常,50x错误,我想写到日志或文件接收器?根据我想路由重试和不重试异常的异常,如何实现this.Basically?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-20 04:30:51

只有2xx的结果将转到log

4xx5xx被认为是错误。

您将需要一个自定义的http-client模块来捕获您认为“确定”的异常,并将它们转发到output通道。就像..。

代码语言:javascript
运行
复制
<int:service-activator input-channel="input" ref="gw" />

<int:gateway request-channel="toHttp" error-channel="errors" />

<int:chain input-channel="errors" output-channel="output">
    <!-- examine payload.cause (http status code etc) and decide whether 
         to throw an exception or return the status code for sending to output -->
</int:chain>
票数 1
EN

Stack Overflow用户

发布于 2015-11-23 18:09:34

我们试着按照上面的建议来实现。它现在正在过滤4XX错误并将有效负载发送到下一个模块。但是,消息在内部兔子mq中没有被确认。在2XX的情况下,确认也不会在更改之后发生。在更改之前,2XX确认发生得很好。5XX重试继续正常进行。请让我们知道如何克服这一问题。

以下是上下文xml

代码语言:javascript
运行
复制
<service-activator input-channel="inputX" ref="gw" />

<gateway id="gw" default-request-channel="toHttp" error-channel="errors" />

<beans:bean id="responseInterceptor" class="com.batch.httpclient.ResponseInterceptor">
</beans:bean>

<chain input-channel="errors" output-channel="output">
<transformer ref="responseInterceptor"  />
</chain>


<int-http:outbound-gateway id='batch-http'  header-mapper="headerMapper"
                           request-channel='toHttp' url-expression="${url}" http-method="${httpMethod}"
                           expected-response-type='java.lang.String' charset='${charset}'
                           reply-timeout='${replyTimeout}' reply-channel='output'>
</int-http:outbound-gateway>

<beans:bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper"
            factory-method="outboundMapper">
    <beans:property name="outboundHeaderNames" value="*"/>
    <beans:property name="userDefinedHeaderPrefix" value=""/>
</beans:bean>

<channel id="output" />
<channel id="input" />
<channel id="inputX" />
<channel id="toHttp" />

ResponseInterceptor的Java代码是这样的-

代码语言:javascript
运行
复制
public Message<String> transform(ErrorMessage errorMessage) {


        if(null != errorMessage && null != errorMessage.getPayload() && null != errorMessage.getPayload().getCause()
                && null != errorMessage.getPayload().getCause().getMessage()){
            String rootCause = errorMessage.getPayload().getCause().getMessage();
            //check if the error message contains 400 or 404 http code.
            if(rootCause.contains("400") || rootCause.contains("404")){
                return MessageBuilder.withPayload(errorMessage.getPayload().getCause().getMessage())
                        .copyHeaders(errorMessage.getHeaders())
                        .removeHeader("errorChannel")
                        .removeHeader("replyChannel")
                        .setReplyChannelName("output").setErrorChannelName(null).build();
            }

        }
        return null;
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33818484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档