このコンテンツは選択した言語では利用できません。

2.5. File locking


Overview

It is possible to have multiple instances of a poller endpoint attempting to read a file on the system. To ensure that there are no conflicts in accessing the file, poller endpoints obtain an exclusive lock on a file while it is being processed.
The locking behavior is controlled by an implementation of the org.apache.servicemix.common.locks.LockManager interface. By default, poller endpoints use a provided implementation of this interface. If the default behavior is not appropriate for your application, you can implement the LockManager interface and configure your endpoints to use your implementation.

Implementing a lock manager

To implement a custom lock manager, you need to provide your own implementation of the org.apache.servicemix.common.locks.LockManager interface. The LockManager has single method, getLock() that needs to be implemented. Example 2.8, “The lock manager's get lock method” shows the signature for getLock().

Example 2.8. The lock manager's get lock method

Lock getLock(String id);
The getLock() method takes a string that represents the URI of the file being processes and it returns a java.util.concurrent.locks.Lock object. The returned Lock object holds the lock for the specified file.
Example 2.9, “Simple lock manager implementation” shows a simple lock manager implementation.

Example 2.9. Simple lock manager implementation

package org.apache.servicemix.demo;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.servicemix.common.locks.LockManager;

public class myLockManager implements LockManager
{
  private ConcurrentMap<String, Lock> locks = new ConcurrentHashMap<String, Lock>();

  public Lock getLock(String id)
  {
    Lock lock = locks.get(id);
    if (lock == null)
    {
      lock = new ReentrantLock();
      Lock oldLock = locks.putIfAbsent(id, lock);
      if (oldLock != null)
      {
        lock = oldLock;
      }
    }
    return lock;
  }

}
Copy to Clipboard Toggle word wrap

Configuring the endpoint to use a lock manager

You configure a poller endpoint to use a custom lock manager using its lockManager attribute. The lockManager attribute's value is a reference to a bean element specifying the class of your custom lock manager implementation.
Example 2.10, “Poller endpoint using a custom lock manager” shows configuration for a poller endpoint that uses a custom lock manager.

Example 2.10. Poller endpoint using a custom lock manager

<beans xmlns:file="http://servicemix.apache.org/file/1.0"
	       xmlns:foo="http://servicemix.org/demo/">

  <file:poller service="foo:filePoller"
               endpoint="filePoller"
               targetService="foo:fileSender" 
               file="inbox"
               lockManager="#myLockManager" />

  <bean id="myLockManager" class="org.apache.servicemix.demo.myLockManager" />
  ...
</beans>
Copy to Clipboard Toggle word wrap
Note
You can also configure a poller endpoint to use a custom lock manager by adding a child lockManager element to the endpoint's configuration. The lockManager element simply wraps the bean element that configures the lock manager.
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat