简体   繁体   中英

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

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.

@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. Rather than a camel component, for example the 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 . 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. However, I don't know if it would interfere with the call to the service which has the @Transactional annotation.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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