【问题标题】:Camel Transactional client with Spring, how to rollback route changes?带有Spring的Camel Transactional客户端,如何回滚路由更改?
【发布时间】:2014-07-30 05:39:31
【问题描述】:

考虑以下两个测试。 findOne() 没有副作用,delete() 对底层 h2 数据库有副作用。我的问题是 @Transactional 不会回滚 delete() 方法的更改。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:app-context.xml")
public class AccountProcessorTest extends BaseRouteTest {

      private static final String ACCOUNTS_ENDPOINT = "seda:bar";

      @Test
      @Transactional
      public void findOne() {

          final Map<String, Object> headers = new HashMap<String, Object>();
          headers.put("id", 1);
          headers.put(Exchange.HTTP_METHOD, "GET");

          final String response = template.requestBodyAndHeaders(ACCOUNTS_ENDPOINT, null, headers, String.class);
          assertEquals("Checking account",JsonPath.read(response, "name"));

      }

     @Test
     @Transactional
     public void delete() {

         final Map<String, Object> headers = new HashMap<String, Object>();
         headers.put("id", 1);
         headers.put(Exchange.HTTP_METHOD, "DELETE");

         final String response = template.requestBodyAndHeaders(ACCOUNTS_ENDPOINT, null, headers, String.class);
         assertEquals(200, JsonPath.read(response, "code"));

      }

 }

BaseRouteTest 只是一个实用程序,我可以在其中获得对 Camel ProducerTemplate 的引用

public class BaseRouteTest implements InitializingBean {

   @Autowired
   private ApplicationContext applicationContext;

   protected ProducerTemplate template;

   @Override
   public void afterPropertiesSet() throws Exception {
      template = getCamelContext().createProducerTemplate();
   }

   private CamelContext getCamelContext() {
      return applicationContext.getBean("foo", CamelContext.class);
   }

}

我已使用已交易标签将路线标记为已交易。

<!-- camel-context -->
<camel:camelContext id="foo">
    <camel:route>
        <camel:from uri="seda:bar"/>
        <camel:transacted />
        <camel:process ref="accountProcessor"/>
    </camel:route>
</camel:camelContext>

我的spring配置文件:

<context:component-scan base-package="com.backbase.progfun"/>

<!-- embedded datasource -->
<jdbc:embedded-database id="dataSource" type="H2">
    <jdbc:script location="classpath:data/schema.ddl"/>
    <jdbc:script location="classpath:data/insert.sql"/>
</jdbc:embedded-database>

<!-- spring jdbc template -->
<bean class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="dataSource" />
</bean>

<!-- transaction management start -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
<!-- transaction management end -->

<!-- camel-context -->
<camel:camelContext id="foo">
    <camel:route>
        <camel:from uri="seda:bar"/>
        <camel:transacted />
        <camel:process ref="accountProcessor"/>
    </camel:route>
</camel:camelContext>

如果您克隆我在此处找到的 github 存储库,您可以快速尝试一下: https://github.com/altfatterz/camel-transaction

如果您运行 AccountProcessorTest,findOne() 测试用例会失败,因为 delete() 测试用例的副作用没有回滚。

任何建议将不胜感激。

谢谢。

【问题讨论】:

    标签: spring transactions apache-camel


    【解决方案1】:

    事务不跨 SEDA 队列进行。

    因此,您的测试开始的事务与您路由中的事务是不同的事务。因此,当您的测试启动的事务回滚时,您的路由所做的更改不会回滚。

    【讨论】:

    • 是的,确实如此。当我从“seda”更改为“direct”时,它起作用了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2016-12-11
    • 2017-04-14
    • 2018-07-05
    • 1970-01-01
    • 2018-01-11
    相关资源
    最近更新 更多