【问题标题】:Saving WS payload into database in Camel将 WS 有效负载保存到 Camel 中的数据库中
【发布时间】:2013-05-03 07:46:37
【问题描述】:

我绝对是 Apache Camel 的新手。我想创建一个非常简单的应用程序,它将接受 WS 调用并使用 JPA 将有效负载保存到数据库中。 有效载荷的结构非常简单。根是一个婚姻对象。它包含一些 String 和 int 以及 Date 字段、一个妻子、一个丈夫和一个孩子列表(Person 对象)。

我的目标是将这些数据保存到数据库的两个表中:MARRIAGE、PERSON。

我已经成功创建了一个 jaxws:endpoint,我在其中侦听并响应一个虚拟响应。 我已经创建了表和 JPA 实体。

我不知道如何将 WS 实现与 spring 配置的 JpaTemplate “连接”。我应该以某种方式使用@Converter 类或@Injet 将其通过Camel 路由解决到Spring 的WS 实现类中来解决这个问题。我很困惑。

我应该使用 cxf 端点而不是 jaxws 端点吗?

【问题讨论】:

    标签: jpa jax-ws cxf apache-camel


    【解决方案1】:

    如果你想使用骆驼,你需要使用camle-cxf端点。我要做的是将端点公开为camle-cxf 端点。像这样的:

    <camel-cxf:cxfEndpoint id="listenerEndpoint"
                           address="http://0.0.0.0:8022/Dummy/services/Dummy"
                           wsdlURL="wsdl/DummyService.wsdl"
                           xmlns:tns="http://dummy.com/ws/Dummy"
                           serviceName="tns:Dummy"
                           endpointName="tns:DummyService">
        <camel-cxf:properties>
            <entry key="schema-validation-enabled" value="true"/>
            <entry key="dataFormat" value="PAYLOAD"/>
        </camel-cxf:properties>
    </camel-cxf:cxfEndpoint>
    

    然后我会有一个像这样的简单 Spring bean:

    <bean id="processor" class="com.dummy.DummyProcessor">
         <property name="..." value="..."/> //there goes your data source of jdbc template or whatever...
    </bean>
    

    如果您想使用JPA,只需配置所有配置并将您的实体管理器注入此 bean。

    实际的类应该是这样的:

    public class DummyProcessor {
    
        @Trancational //If you need transaction to be at this level...
        public void processRequest(Exchange exchange) {
            YourPayloadObject object = exchange.getIn().getBody(YourPayloadObject.class);
            //object - is your object from SOAP request, now you can get all the data and store it in the database.
        }
    }
    

    骆驼路线是这样的:

    <camel:camelContext trace="true" id="camelContext" >
    
        <camel:route id="listenerEndpointRoute">
            <camel:from uri="cxf:bean:listenerEndpoint?dataFormat=POJO&amp;synchronous=true" />
            <camel:log message="Got message. The expected operation is :: ${headers.operationName}"/>
            <camel:choice>
                <camel:when>
                    <camel:simple>${headers.operationName} == 'YourPayloadObject'</camel:simple>
                    <camel:bean ref="processor" method="processRequest"/>
                </camel:when>
            </camel:choice>
            <camel:log message="Got message before sending to target: ${headers.operationName}"/>
            <camel:to uri="cxf:bean:someTargetEndpointOrSomethingElse"/>
            <camel:log message="Got message received from target ${headers.operationName}"/>
        </camel:route>
    
    </camel:camelContext>
    

    希望这会有所帮助。

    【讨论】:

    • 您好保利乌斯!谢谢你的帮助!使用您的示例代码,我可以做我想做的事。现在我可以继续了。我的下一个目标是将我的试点应用程序与 jBPM 集成。哦,我接受了你用 CXF 替换 JAXWS 的建议。
    猜你喜欢
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2021-11-16
    相关资源
    最近更新 更多