集群中的Camel readlock策略

时间:2016-10-18 22:22:15

标签: locking apache-camel

我们正在尝试使用Apache Camel迁移到群集。到目前为止,我们在一个节点上使用它并且运行良好。

一个节点: 我将readlock策略设置为'已更改'它使用camelLock文件跟踪文件更改,并且只有在文件下载完成后,它才会被选中进行处理。但是骆驼读锁策略改变了#39;在集群中不鼓励。根据骆驼文件'幂等'被推荐。当我使用5GB文件进行测试时会发生这种情况。

两个节点: 我有一个readlock策略来“幂等”'它将文件分发到其中一个节点,但是在文件下载完成之前,camel就开始处理文件。

当readlock策略是幂等的时候,是否有办法在文件下载之前停止处理骆驼?

2 个答案:

答案 0 :(得分:1)

Even though both "readLock=changed" and "readLock=idempotent" cause the file-consumer to wait, they really address quite different use-cases: while "readLock=changed" guards against the file being incomplete (i.e. still being written by some producer/sender), "readLock=idempotent" guards against a file being read by two consumer routes. It's a bit confusing that they're addressed by the same option.

First, to address the "changed" scenario: can the sender be changed so that it writes the file in one directory and then, when it is done writing, it copies it into the directory being monitored by your file-consumer? If this is under your control, this is a good way of letting the OS handle things instead of trying to deal with it yourself. (This does not address the issue of the multiple readers.) Otherwise, I suggest you revert back to readLock=changed

Next, on multiple readers, one work around is to only have this route run on only one node of your cluster. Sometimes this might defeat the purpose of clustering, but it is quite possible that you're starting up additional nodes to help with some other routes, and you're fine with this particular route running on just one node. It's a bit of a hack to do something like this, because all nodes are no longer equal, but it is still an option to consider. Simplest would be to start one node with some environment property that flags it as the node that will handle file-reading... or some similar approach.

If you do want the route on multiple nodes, you can start by using the option "idempotent=true" but this is not good enough on its own. The option uses a repository, where it records what files have been read before, and the default repository is in-memory (i.e. each node has its own). So, the default implementation is helpful if the same file is actually being received more than once, and you wish to skip it. However, if you want it to work across nodes, you have to use a different repository.

One central repository could be a database. In that case use can use Camel's JDBC or JPA based repositories. Or, you could use something like Hazelcast. See here for your options: http://camel.apache.org/idempotent-consumer.html

答案 1 :(得分:0)

我们还在Docker集群模式下使用了readLock=changed,并且由于我们在特定的时间间隔内使用了readLockMinAge,因此运行得很好。