apache camel http4错误处理

时间:2016-10-21 21:05:26

标签: spring-boot apache-camel

我从Camel 2.17升级到2.18后发生了一个奇怪的问题。在我的路由器类中,我有以下代码片段来处理传入的请求。良好的请求传递到链中,而特定错误的目标是错误处理,如下所示:

application.yml

camel.endpoint.my.servletEndpoint: servlet:///activate?httpMethodRestrict=POST,OPTIONS

CamelRouter.java

from("{{camel.endpoint.my.servletEndpoint}}")
            .doTry()
                .process(myRequestProcessor)
                .to("{{camel.endpoint.my.trigger}}")
            .doCatch(IllegalArgumentException.class)
                .to("{{camel.endpoint.httpError.badRequest}}")
            .doCatch(IllegalStateException.class)
                .to("{{camel.endpoint.httpError.badState}}")
            .end();

// HTTP Error endpoints
from("{{camel.endpoint.httpError.badRequest}}")
        .process(new HttpErrorHandler(HttpStatus.BAD_REQUEST));
from("{{camel.endpoint.httpError.badState}}")
        .process(new HttpErrorHandler(HttpStatus.INTERNAL_SERVER_ERROR));

MyRequestProcessor.java

public class RedcapRequestProcessor implements Processor {
...
@Override
public void process(Exchange exchange) throws Exception {
    MyTriggerRequest triggerRequest = new MyTriggerRequest();

    HttpServletRequest request = exchange.getIn().getHeader(Exchange.HTTP_SERVLET_REQUEST, HttpServletRequest.class);
    // If request doesn't look right, throw IllegalArgumentException

    if (!getAndVerifyParameter(request, "url_under_test_parameter"))
    throw new IllegalArgumentException("Not a good parameter!");

    // Add to outbound message
    exchange.getOut().setBody(triggerRequest);
}

}

这在Camel 2.17中按预期工作 - 当MyRequestProcessor在处理期间抛出IllegalArgumentException时,路由器类将捕获它并相应地处理。但升级后,我现在收到IOException: Stream closed错误,显然是因为HTTP响应Exchange由于某种原因被关闭了?堆栈跟踪:

org.apache.camel.RuntimeCamelException: java.io.IOException: Stream closed
at org.apache.camel.http.common.HttpMessage.createBody(HttpMessage.java:74)
at org.apache.camel.impl.MessageSupport.getBody(MessageSupport.java:47)
at org.apache.camel.http.common.DefaultHttpBinding.doWriteResponse(DefaultHttpBinding.java:386)
at org.apache.camel.http.common.DefaultHttpBinding.writeResponse(DefaultHttpBinding.java:322)
at org.apache.camel.http.common.CamelServlet.doService(CamelServlet.java:210)
at org.apache.camel.http.common.CamelServlet.service(CamelServlet.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:105)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:115)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:121)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:89)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:107)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.IOException: Stream closed
    at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:335)
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:155)
    at org.apache.camel.util.IOHelper.copy(IOHelper.java:196)
    at org.apache.camel.util.IOHelper.copy(IOHelper.java:169)
    at org.apache.camel.util.IOHelper.copyAndCloseInput(IOHelper.java:218)
    at org.apache.camel.util.IOHelper.copyAndCloseInput(IOHelper.java:214)
    at org.apache.camel.http.common.HttpHelper.readResponseBodyFromInputStream(HttpHelper.java:244)
    at org.apache.camel.http.common.HttpHelper.readRequestBodyFromServletRequest(HttpHelper.java:195)
    at org.apache.camel.http.common.DefaultHttpBinding.parseBody(DefaultHttpBinding.java:572)
    at org.apache.camel.http.common.HttpMessage.createBody(HttpMessage.java:72)
    ... 85 common frames omitted

那里有专家可以告诉我为什么这可能会停止工作吗?我已经检查过以确保依赖项是兼容的(Apache Camel 2.18,Spring Boot 1.4.1)。我也让Spring Boot(和camel-spring-boot)处理CamelServlet和CamelContext的所有自动配置。感谢

1 个答案:

答案 0 :(得分:0)

实际上,升级Camel似乎暴露了我的HttpErrorHandler的实现问题。一旦我按照以下方式重新设计了CamelRouter代码块,问题就消失了:

// HTTP Error endpoints
// *** reimplemented HttpErrorHandler as a bean, now passing status codes as messages to be handled
from("{{camel.endpoint.my.servletEndpoint}}")
            .doTry()
                .process(myRequestProcessor)
                .to("{{camel.endpoint.my.trigger}}")
            .doCatch(IllegalArgumentException.class)
                .transform(constant(HttpStatus.BAD_REQUEST))
                .process(httpErrorHandler);
            .doCatch(IllegalStateException.class)
                .transform(constant(HttpStatus.INTERNAL_SERVER_ERROR))
                .process(httpErrorHandler);
            .end();

我不能说我完全理解为什么原始代码停止工作(即使它工作得很糟糕:)) - 也许它试图以某种方式重用传入的HTTP Exchange - 但更干净的代码似乎就足够了。