【问题标题】:how to invoke multiple http webservice in mule flow in transaction?如何在事务中的 mule 流中调用多个 http web 服务?
【发布时间】:2015-08-27 16:04:35
【问题描述】:

我是新来的骡子。 我有一个弹簧休眠骡子设置。假设有 3 个 web 服务。目标是在 mule 流中调用所有这些 web 服务,以便所有 3 个都在一个事务中。因此假设如果第 3 个 web 服务失败,那么前 2 个将自动回滚。

这是我尝试过的代码 sn-p。 我的 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;
    }

}

请告诉我任何解决方案。谢谢

【问题讨论】:

  • 您是否尝试公开或使用多个 Web 服务?
  • 我正在尝试公开 webservice..上面的代码不是 webservice 客户端的代码,所以我相信,我正在尝试公开 webservice..
  • 调用一个或另一个 Web 服务的条件是什么?
  • 无条件..我想使用 mule flow 排队通话
  • 您的服务使用什么协议?你考虑过 WS-AtomicTransaction (WS-AT) (SOAP) 吗?

标签: spring hibernate mule mule-studio


【解决方案1】:

通过看到您的问题,我了解到您尝试进行 3 个 Web 服务调用,并且您希望在一个事务中进行所有三个调用。

当您调用另一个系统(在您的情况下为 Web 服务)时,您无法控制维护事务。

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

在您的捕获异常策略中,您需要执行回滚活动(进行另一个 Web 服务调用或数据库调用,这将满足您的回滚要求)。

【讨论】:

    【解决方案2】:

    据我了解,您有三个或更多 Web 服务调用和一些数据库操作。如果任何服务调用失败,您想回滚。

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

    <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>
    

    完整的流程将在单个事务和 inclouding dao 类中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      相关资源
      最近更新 更多