Camel CXF(代码优先)Tomcat示例-如何部署客户端

时间:2018-11-06 05:51:15

标签: java apache-camel cxf

我已经基于Camel CXF(代码优先)Tomcat示例构建了一个项目,其中的CFX Web服务由Java接口定义。在示例中,WS接口是IncidentService.java。在我的项目中,服务接口称为Ingester,并定义了upload(String body,String id)方法。该服务部署到tomcat,我使用以下代码从另一个项目中的客户端调用该服务:

ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
                factory.setServiceClass(Ingester.class);
                factory.setAddress(toURL);
                Ingester client = (Ingester) factory.create();
                String out = client.upload(exchange.getIn().getBody(String.class),"2");

客户端项目通过将服务器项目导入为参考项目(在Eclipse中)来参考Ingester接口。

一切正常。

现在,我只想在用户计算机上部署客户端(并使它仍然可以工作)。

第一种方法是我简单地(使用maven)震撼了客户端。当我运行可执行jar时,出现错误,找不到ws.Ingester类。

    org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[ID-VSWINLT019-1541482662292-0-1]
        at org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1846)
        at org.apache.camel.impl.DefaultExchange.setException(DefaultExchange.java:385)
        at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:66)
        at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
        at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:197)
        at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:79)
        at java.util.TimerThread.mainLoop(Unknown Source)
        at java.util.TimerThread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: ws.Ingester 
        at qp.PullRoute$1.process(PullRoute.java:54)
        at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)
        ... 9 more
Caused by: java.lang.ClassNotFoundException: ws.Ingester

我不确定要采用哪种方法,因此我尝试了几种方法都没有成功。

我想做如下操作,删除对IncidentService接口的引用:

.to("cxf://http://localhost:8080/data-ingest-service/webservices/ws"
            + "?serviceClass=ws.Ingester"
            + "?serviceName=upload"
            + "?id=1")

这是正确的方向吗?我收到以下异常,如何使其正常工作?

Exception in thread "main" org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> To[cxf://http://localhost:8080/data-ingest-service/webservices/ws?serviceClass=ws.Ingester?serviceName=upload?id=1] <<< in route: Route(route1)[[From[timer://Timer?period=60000]] -> [OnExcep... because of Failed to resolve endpoint: cxf://http://localhost:8080/data-ingest-service/webservices/ws?serviceClass=ws.Ingester%3FserviceName%3Dupload%3Fid%3D1 due to: ws.Ingester?serviceName=upload?id=1

有人可以给我建议吗? 'id'参数似乎是问题的一部分,我假设将像上面的代码第一个示例中一样将body传递给body参数。

我不认为打包服务器并将其作为从属存储库包含是正确的方法,主要是因为这似乎有些过分。

谢谢

2 个答案:

答案 0 :(得分:0)

我认为,如果用packaging包装战争(将pom.xml中的WEB-INF/web.xml换成<packaging>war</packaging>

第一步,在您的pom.xml中:

web.xml

现在,为您的<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Web Application</display-name> <!-- Context loader --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:camel-context.xml</param-value> </context-param> <!-- CXF servlet --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>

jetty

然后您可以在网络容器中进行战争,例如雄猫。

或者,如果您只想运行jar,则应查看嵌入式Error We encountered a server error while trying to inspect the URL. 服务器或类似的服务器。

答案 1 :(得分:0)

我已经完成了这项工作,但我不认为这是最好的解决方案。我将服务器作为依赖项包含在客户端中,这真的是一个好主意吗?我将Shade插件的内容添加到了我的POM中(来自此答案Apache CXF client loads fine in Eclipse but standalone jar throws NullpointerException in WSDLServiceFactory