简体   繁体   English

骆驼尊重bean方法调用是否带有@Transaction注释?

[英]Will camel respect bean method call annotated with @Transaction?

I created a route with Apache camel, reading from a queue, do some processing and eventually store the processed items in the database. 我使用Apache骆驼创建了一条路由,从队列中读取,进行了一些处理,最后将处理过的项目存储在数据库中。 Here is the (slimmed down version) route: 这是(精简版)路由:

fromF("activemq:queue:%s?username=%s&password=%s&transacted=true&transactionTimeout=5000", queueProperties
            .getStatusQueueName(), queueUsername, queuePassword)
            .unmarshal().json(JsonLibrary.Jackson, StatusUpdate.class)
            .bean(shippingLabelPersister, "persist").id("status-update-persister")
        .routeId(ROUTE_ID);

The shippingLabelPersister persist method takes the message body and calls the shippingLabelService: shippingLabelPersister持久方法采用消息正文并调用shippingLabelService:

public void persist(@Body List<ShippingLabel> labels) {
  if(CollectionUtils.isEmpty(labels)) {
    return;
  }

  List<ShippingLabel> savedLabels = shippingLabelService.saveAll(labels);

  for(ShippingLabel label : savedLabels) {
    logger.info("Updated shipment {} with status {} (timestamp: {})", label.getBarcode(), label.getStatus(), DATE_TIME_FORMATTER.format(label.getStatusChanged()));
  }
}

The saveAll method of the shippingLabel service has the @Transactional method. shippingLabel服务的saveAll方法具有@Transactional方法。

@Override
@Transactional
public List<ShippingLabel> saveAll(List<ShippingLabel> labels) {
  if(CollectionUtils.isEmpty(labels)) {
    return Collections.emptyList();
  }

 return repository.saveAll(labels);
}

I want to know if the persisting of the data will indeed be wrapped inside a transaction if it is called by the camel route? 我想知道,如果骆驼路线调用了数据,持久化数据是否确实会包裹在事务中?

Some background information : As you've seen I'm using beans for data persistence in this case. 一些背景信息 :如您所见,在这种情况下,我正在使用bean进行数据持久化。 Rather than a camel component, for example the camel-jpa-component . 而不是骆驼组件,例如camel-jpa-component The reason for this is that For now, regarding the scope and origin of the project, we want to reuse existing code as-is. 原因是,就项目的范围和来源而言,目前,我们希望按原样重用现有代码。 We're creating microservices from a legacy monolithic application where we can't yet rewrite the whole route (yet), making full use of the camel-jpa-component . 我们正在从一个遗留的整体应用程序中创建微服务,在该应用程序中,我们还无法完全重写整个路由,而充分利用the camel-jpa-component So I'm trying to see if my suggested solution is acceptable. 因此,我试图查看我建议的解决方案是否可以接受。

I'm also aware that camel provides a transacted method call from within the route, which uses springs transaction manager. 我也知道骆驼在路由中提供了一个transacted方法调用,它使用了springs事务管理器。 However, I don't know if it would interfere with the call to the service which has the @Transactional annotation. 但是,我不知道它是否会干扰对具有@Transactional批注的服务的调用。

我会考虑为此创建测试用例:让标签列表包含最后一个对于数据库不正确的ShippingLabel(太长或某物),断言没有插入ShippingLabel,重构saveAll()来调用nonTransactionalSaveAll(),这应该保存除了不正确的ShippingLabel之外的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM