如何使用Camel将Rest请求从本地服务路由到远程服务

时间:2011-05-15 14:59:26

标签: java cxf apache-camel cxfrs

我正在尝试将来自cxf rest服务的rest请求路由到另一个。我看了http://camel.apache.org/cxfrs.html,这有助于理解部分过程。我在remoteService Invocation级别有一个classCastException。

我做错了什么?

<cxf:rsServer id="exposedService" address="/exposed/"
serviceClass="com.example.project.ExposedService" />

<!-- using different classes since we are not just proxying -->
<cxf:rsClient id="remoteService" address="http://remote.com/service"
serviceClass="com.example.project.RemoteService" />

<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
   <package>com.example.project</package>
</camel:camelContext>

服务类:

@Path("/myservice")
public class ExposedService {

    @POST
        @Consumes("application/xml")
    public void postResource(javax.xml.transform.Source resource) {
            // source : not using jaxb just plain xml
        // only serves to configure the endpoint (?)    
    }               
}


public class RemoteService {

    @POST
        @Consumes("application/xml")
    public void postResource(javax.xml.transform.Source resource) {
            // source : not using jaxb just plain xml
        // only serves to configure the endpoint (?)    
    }               
}

RouteBuilder:

public class MyRouteBuilder extends RouteBuilder {

public void configure() { 
    from("cxfrs://bean://exposedService")
    .process(...) // a few System.out.println's
    .to("cxfrs://bean://remoteService")
    ;   
}
}

堆栈跟踪:

| ERROR | qtp-2045010446-1 | DefaultErrorHandler              | 68 - org.apache.camel.camel-core - 2.6.0 | Failed delivery for exchangeId: ID-Lab-local-52879-1305561896560-7-2. Exhausted after delivery attempt: 1 caught: java.lang.ClassCastException
java.lang.ClassCastException
        at java.lang.Class.cast(Class.java:2990)[:1.6.0_24]
        at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:329)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.camel.component.cxf.jaxrs.CxfRsProducer.invokeHttpClient(CxfRsProducer.java:147)[118:org.apache.camel.camel-cxf:2.6.0]
        at org.apache.camel.component.cxf.jaxrs.CxfRsProducer.process(CxfRsProducer.java:77)[118:org.apache.camel.camel-cxf:2.6.0]
        at org.apache.camel.impl.converter.AsyncProcessorTypeConverter$ProcessorToAsyncProcessorBridge.process(AsyncProcessorTypeConverter.java:50)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.SendProcessor$2.doInAsyncProducer(SendProcessor.java:104)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.impl.ProducerCache.doInAsyncProducer(ProducerCache.java:272)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:98)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:68)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:174)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.RedeliveryErrorHandler.processErrorHandler(RedeliveryErrorHandler.java:299)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:208)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.DefaultChannel.process(DefaultChannel.java:269)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:125)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:102)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:68)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:91)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:85)[68:org.apache.camel.camel-core:2.6.0]
        at org.apache.camel.component.cxf.jaxrs.CxfRsInvoker.syncInvoke(CxfRsInvoker.java:134)[118:org.apache.camel.camel-cxf:2.6.0]
        at org.apache.camel.component.cxf.jaxrs.CxfRsInvoker.performInvocation(CxfRsInvoker.java:68)[118:org.apache.camel.camel-cxf:2.6.0]
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:89)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:153)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:87)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)[116:org.apache.cxf.bundle:2.3.2]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)[:1.6.0_24]
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)[:1.6.0_24]
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)[:1.6.0_24]
        at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:106)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.transport.http_osgi.OsgiDestination.doMessage(OsgiDestination.java:79)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.transport.http_osgi.OsgiServletController.invokeDestination(OsgiServletController.java:336)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.transport.http_osgi.OsgiServletController.invoke(OsgiServletController.java:108)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.transport.http_osgi.OsgiServlet.invoke(OsgiServlet.java:53)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.transport.http_osgi.SpringOsgiServlet.invoke(SpringOsgiServlet.java:48)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)[116:org.apache.cxf.bundle:2.3.2]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:103)[116:org.apache.cxf.bundle:2.3.2]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:595)[93:org.apache.geronimo.specs.geronimo-servlet_3.0_spec:1.0]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)[116:org.apache.cxf.bundle:2.3.2]
        at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.handle(HttpServiceServletHandler.java:64)[133:org.ops4j.pax.web.pax-web-jetty:0.7.4]
        at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.handle(HttpServiceContext.java:111)[133:org.ops4j.pax.web.pax-web-jetty:0.7.4]
        at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:68)[133:org.ops4j.pax.web.pax-web-jetty:0.7.4]
        at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.mortbay.jetty.Server.handle(Server.java:326)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]
        at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)[94:org.apache.servicemix.bundles.jetty:6.1.25.1]

1 个答案:

答案 0 :(得分:6)

这里有一个做同样事情的工作例子......

http://camel.apache.org/cxf-proxy-example.html

从此开始,然后逐个删除您的更改并查看它的中断位置......