English 中文(简体)
利用java.nio在案卷变化中创造/移动/转手活动
原标题:Use java.nio to raise create/move/delete events on file changes

For a media library where I d like to update entries when they change in the file system I wanted to give the new java.nio file watching features a try by using this example.
I was expecting to get useful events when a file is being created, moved (renamed) or deleted, but here is what s happening when watching folders on windows7 (haven t tried the other operating systems yet):

Format:
[ThreadName] DEBUG 2012-04-09 18:20:35.934 GroupNumber-COMMAND: Path
ThreadName: Each watch folder runs in its own thread having a distinct id
GroupNumber: Messages sent with the same GroupNumber are being sent at the same time (usually..)
COMMAND: The received command
Path: The received path

Rename:
[Watch0] DEBUG 2012-04-09 18:20:35.934 2-ENTRY_DELETE: C:	mp	mp	est.avi
[Watch0] DEBUG 2012-04-09 18:20:35.935 2-ENTRY_CREATE: C:	mp	mp	est1.avi
[Watch0] DEBUG 2012-04-09 18:20:35.936 3-ENTRY_MODIFY: C:	mp	mp
[Watch0] DEBUG 2012-04-09 18:20:35.937 4-ENTRY_MODIFY: C:	mp	mp	est1.avi

[Watch4] DEBUG 2012-04-09 18:43:47.965 18-ENTRY_DELETE: F:	mp	est.avi
[Watch4] DEBUG 2012-04-09 18:43:47.966 18-ENTRY_CREATE: F:	mp	est1.avi
[Watch4] DEBUG 2012-04-09 18:43:47.967 19-ENTRY_MODIFY: F:	mp	est1.avi

Create:
[Watch0] DEBUG 2012-04-09 18:22:02.055 5-ENTRY_CREATE: C:	mp	est.avi
[Watch0] DEBUG 2012-04-09 18:22:02.066 6-ENTRY_MODIFY: C:	mp	est.avi
[Watch0] DEBUG 2012-04-09 18:22:03.460 7-ENTRY_MODIFY: C:	mp	est.avi
//Note the 1.4   delay between the last two messages. 
//This is the time required to actually copy the file

Move in same watch folder:
[Watch0] DEBUG 2012-04-09 18:18:42.395 0-ENTRY_DELETE: C:	mp	est.avi
[Watch0] DEBUG 2012-04-09 18:18:42.396 0-ENTRY_MODIFY: C:	mp	mp
[Watch0] DEBUG 2012-04-09 18:18:42.396 1-ENTRY_CREATE: C:	mp	mp	est.avi
[Watch0] DEBUG 2012-04-09 18:18:42.396 1-ENTRY_MODIFY: C:	mp	mp	est.avi

Move to other watch folder on same drive:
[Watch1] DEBUG 2012-04-09 18:23:24.341 8-ENTRY_CREATE: C:	mp2	est.avi
[Watch0] DEBUG 2012-04-09 18:23:24.341 8-ENTRY_DELETE: C:	mp	est.avi
[Watch1] DEBUG 2012-04-09 18:23:24.342 10-ENTRY_MODIFY: C:	mp2	est.avi
//The two 8 are lying. Both messages are being sent from different threads
//and the shared counter hasn t been incremented by any yet. The next entry has been 
//incremented by two!

Move to other watch folder on different drive:
[Watch4] DEBUG 2012-04-09 18:25:42.324 11-ENTRY_CREATE: F:	mp	est.avi
[Watch4] DEBUG 2012-04-09 18:25:42.338 12-ENTRY_MODIFY: F:	mp	est.avi
[Watch4] DEBUG 2012-04-09 18:25:42.703 13-ENTRY_MODIFY: F:	mp	est.avi
[Watch3] DEBUG 2012-04-09 18:25:49.433 14-ENTRY_DELETE: C:	mp2	est.avi
//Note that the last delete message is being sent from another thread then the first ones.
//This is because the source and destination WatchDirs aren t the same

Delete:
[Watch9] DEBUG 2012-04-05 21:22:02.921 ENTRY_DELETE: C:	mp	est (2011).mkv

不是单一事件,而是必须解释一套指挥+道路。 例如,删除由单一指挥构成,而改称同一倍的指挥从删除指挥开始,但将由今后的指挥加以界定。 此外,可以同时处理多个档案,这将最终列入属于不同行动指挥的随机清单,必须加以分类。

我可以讨论的最佳途径是:,该类别,在接收时,活动在另一场(如果发生其他事件,也属于同一活动组)之后,就会被搁置。

如果重新命名、移动、创建或删除单一档案,那么这项工作就会发生,但如果开始同时复制多个档案或复制一批档案,则不会再有任何作用。

是否落实了我已经需要的东西(如共同使用案例)? 或者有人对如何处理这一问题以涵盖所有案件有好的想法?

最终,它将不得不为窗户、lin子和os工作。

应当支持的更复杂的例子

[Watch0] DEBUG 2012-04-09 19:10:17.774 0-ENTRY_CREATE: C:	mp	mp	estlarge.avi
[Watch0] DEBUG 2012-04-09 19:10:17.825 0-ENTRY_MODIFY: C:	mp	mp	estlarge.avi
[Watch0] DEBUG 2012-04-09 19:10:17.826 1-ENTRY_MODIFY: C:	mp	mp
[Watch0] DEBUG 2012-04-09 19:12:09.516 2-ENTRY_DELETE: C:	mp	mp	estsmall.avi
[Watch0] DEBUG 2012-04-09 19:12:09.516 3-ENTRY_CREATE: C:	mp	estsmall.avi
[Watch0] DEBUG 2012-04-09 19:12:09.517 3-ENTRY_MODIFY: C:	mp	estsmall.avi
[Watch0] DEBUG 2012-04-09 19:12:09.521 4-ENTRY_MODIFY: C:	mp	mp
[Watch0] DEBUG 2012-04-09 19:14:13.025 5-ENTRY_MODIFY: C:	mp	mp	estlarge.avi

在这里,在创建大型档案时,正在搬迁一个小档案。

问题回答

你们正在发生的事件就是你们将要开展的活动。 视窗。 在Linot,当你看着一个夹子时,你只想为这个夹子,而不是从这个夹子中找到文件。 我认为,这甚至是第十届特别协调员会议的缩影,尽管我从未在那里处理过这个问题。 这意味着,如果你重新观察<代码>/tmp。 页: 1 /tmp/tmp/,但不ENTRY_CREATE /tmp/tmp/test.avi

我也处理过类似的问题,看着文件夹,在《一套规则》中累积变更通知,然后在《一套规则》中提及的所有档案上定期运作。 (正在更新的一套,另一套是扫描和清除的,而I d swap是一套。)

In short, I gave up on trying to figure out when files were moved or copied or appended to. I simply treated every create or modify as creating a new file unrelated to previous files. Anything more was just unworkable in practice, at least in my case, where I was tracking millions of files on a server.

我并不十分熟悉java nio文档探测器,但我使用了

如果你想用某种方式来研究:

我看到的一件事是,你为每次活动创造新的通道,你用日夜标使座右铭同步。

Better would be if you create an ExecutorService i.e. rewrite your code like:

private ExecutorService executorService;


private class WatchEventHandler implements Callable {

    WatchEvent<?> event;

    public WatchEventHandler(final WatchEvent<?> event) {
        this.event = event;
    }

    @Override
    public void call() throws Exception{
        // do something with the event

        // fireFileWatchAction();


    }    
}


public ManagedFolderWatcher(DOManagedFile managedFolder) throws IOException {
   executorService = Executors.newFixedThreadPool(10);
   ...
}

and in your run/process events method

public void run() {
    try {
       while(true) {

          WatchKey key = watcher.take();
          ....

          for (WatchEvent<?> event : key.pollEvents()) {
              ...
              // in my oppinon there is no need to delay the event 
              executorService.schedule(new WatchEventHandler(event));
          }
          ...

       }
    } catch (InterruptedException ie) {
        // todo: propper error handling
        log.error("Thread interrupted", ie);
    } catch (ClosedWatchServiceException cwse) {
        // todo: propper error handling
        log.error("WatchService allready closed.", cwse);
    }

} 

当你以这种方式执行时,你甚至不需要你锁定的物体<代码>。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签