如何在事务中的mule流中调用多个http web服务?

时间:2013-01-26 06:13:34

标签: spring hibernate mule mule-studio

我是骡子的新手。 我有一个春天冬眠的骡子设置。假设有3个webservice.Aim是在一个mule流中调用所有这些webservice,这样所有3个将在一个事务中。所以假设如果第三个web服务失败,那么之前的2个将自动回滚。

这是我尝试过的代码片段。 我的mule-flow.xml(currently I have only one webservice,kindly let me know how can I add multipe webservice call in flow?

<spring:beans>
 <spring:import resource="classpath:spring-mule.xml"/>
 <spring:import resource="classpath:applicationContext-persistence.xml"/>
</spring:beans>

<flow name="addCustomer" doc:name="addCustomer">
<http:inbound-endpoint exchange-pattern="request-response"
address="http://localhost:8081/pos/addCustomer" doc:name="HTTP" />

<cxf:simple-service serviceClass="com.proj.pos.webservice.interfac.CustomerService" doc:name="SOAP"/>
<component ><spring-object bean="customerService"/></component>
</flow>

</mule>

我的spring-mule.xml

<bean id="customerService"  class="com.proj.pos.webservice.implementation.CustomerServiceImpl">
    <property name="cusDao" >
    <ref local="customerDao"/>
    </property>
    </bean>

我:的applicationContext-的persistence.xml

     <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="xyz" />
<property name="password" value="xyz" />
</bean> 

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="datasource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                <prop key="hibernate.connection.release_mode">auto</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

     <bean id="customerDao" class="com.proj.pos.dao.implementation.CustomerDaoImpl">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>

我的CustomerServiceImpl

@WebService(endpointInterface = "com.proj.pos.webservice.interfac.CustomerService",
        serviceName = "CustomerService")
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao cusDao;

    @Transactional  //NOTE:USING THIS ANNOTATION I AM ABLE TO ACHIEVE THE BELOW METHOD //IN TRANSACTION,BUT I BELEIVE THIS FEATURE WILL NOT WORK IF WE HAVE MULTIPLE WEBSERVICE CALL
    @Override
    public Customer addCustomer(CustomerDto dto) {
        Customer customer=new Customer(dto.getCustomerId(), dto.getFname(), dto.getLname(), dto.getAge(), dto.getDateOfBirth(), dto.getAddress());
        customer.setCustomerId(cusDao.persist(customer));
         return customer;
    }

    public CustomerDao getCusDao() {
        return cusDao;
    }

    public void setCusDao(CustomerDao cusDao) {
        this.cusDao = cusDao;
    }

}

请让我知道任何解决方案。谢谢

2 个答案:

答案 0 :(得分:1)

通过查看您的问题我理解的是您尝试进行3次网络服务呼叫,并且您希望在一次交易中进行所有三次呼叫。

当您拨打另一个系统(在您的情况下是Web服务)时,您无法控制维护交易。

在您的情况下,您可以使用Scatter收集路由器来调用三个Web服务,如果任何路由(Web服务调用)失败,它将抛出CompositeRoutingException,您可以在捕获异常中捕获此异常策略。

在catch异常策略中,您需要执行回滚活动(进行另一个Web服务调用或db调用,以满足您的回滚要求)。

答案 1 :(得分:0)

根据我的理解,您有三个或更多webservices调用和一些db操作。如果任何服务调用失败,您想要回滚。

为SpringTransactionFactory编写如下的spring bean并设置transationManager属性(来自applicationContext-persistence.xml的ref)

<spring:beans>
 <spring:import resource="classpath:spring-mule.xml"/>
 <spring:import resource="classpath:applicationContext-persistence.xml"/>
 <spring:bean id = "transactionFactory" class = "org.mule.module.spring.transaction.SpringTransactionFactory">
    <spring:property ref="transactionManager" name=""></spring:property>
 </spring:bean>
</spring:beans>

并在inboundEndpoint中添加一个tansactionfactory ref,如下所示

<http:inbound-endpoint exchange-pattern="request-response"
                address="http://localhost:8081/pos/addCustomer" doc:name="HTTP" >
                <custom-transaction action="ALWAYS_BEGIN" factory-ref="transactionFactory"/>
                </http:inbound-endpoint>

完整的流程将在单个事务中并包含dao类