0

I am using Jnotify to write an app. (JNotify is a library for for detecting file modification.)

The app has the following components:

  1. A file writer that writes to file X

  2. A file watcher (created using JNotify) that watches file X

  3. An external app (such as notepad) that writes to file X as demanded by user.

I want JNotify to trigger notifications only when X is modified using 3, and to ignore when modified via 1. (or at least differentiate between modifications via 1 and 3).

Is there a simple way in which I can do it? One way is to have a synchronized variable that is toggled when the file writer writes to it, but I feel this is not very elegant.

4

2 に答える 2

1

いずれにせよ、一時的に 2 を無効にするために 1 + 2 間の通信が必要です (3 に関しては、そうする方法はありません)。もちろん、1 + 2 が同じ JVM で実行されている場合、より適切な方法は、共通変数で状態を共有することです。

他のスマートなコミュニケーション方法を考えるかもしれません。1 つが頭に浮かびました。1 が書き込みを開始する前に、2 も lsten されているロック ファイルが生成されます。書き込みが完了した後にロック ファイルが 1 から削除されると、ロック ファイルの削除が通知された後、2 はファイルをリッスンし続ける可能性があります。

于 2011-03-21T16:33:52.853 に答える
0

私は2つの異なる方法を試しました(選択したオプションとともに以下で説明します)

  • オプション 1: 1 によって true に切り替えられる共有ブール変数 (つまり、ロック) を使用します。この変数が true の場合、3 は書き込みを行わず、代わりに false に設定します)。

  • オプション 2: ファイルの共有 SHA1 ハッシュを使用します。ライター (1) は、ファイルを書き込むたびにハッシュを更新します。ウォッチャー (2) は、変更されたファイルのハッシュをチェックし、ハッシュが共有ハッシュと一致するかどうかを無視します。

オプション 2 は完全に機能したため、使用することにしました。オプション 1 を使用するのは注意が必要です。変更されたすべてのファイルに対して、JNotify は2 つの更新をトリガーします (奇妙です)。

于 2011-03-22T07:51:32.797 に答える