Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于java触发器运行任务_Java_Multithreading_Jakarta Ee - Fatal编程技术网

基于java触发器运行任务

基于java触发器运行任务,java,multithreading,jakarta-ee,Java,Multithreading,Jakarta Ee,如果Java中存在触发器(即某些事件,如添加到目录的新文件),我希望运行任务。Java是否具有对此的内置支持? 如果没有,我可以使用哪个第三方库来实现这一点?在Java 7中,当检测到文件或目录上的更改或事件时,有一个监视服务允许任务发生 教程: API文档: 下面是我制作的一个快速示例: package watcher; import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.File

如果Java中存在触发器(即某些事件,如添加到目录的新文件),我希望运行任务。Java是否具有对此的内置支持?
如果没有,我可以使用哪个第三方库来实现这一点?

在Java 7中,当检测到文件或目录上的更改或事件时,有一个监视服务允许任务发生

教程:

API文档:

下面是我制作的一个快速示例:

package watcher;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class Watcher {

    private final FileCreatedAction action;
    private final String pathToWatchString;

    public Watcher(FileCreatedAction action, String pathToWatchString) {
        this.action = action;
        this.pathToWatchString = pathToWatchString;
    }

    public void start() throws IOException {
        FileSystem defaultFileSystem = FileSystems.getDefault();
        WatchService watchService = defaultFileSystem.newWatchService();
        Path pathToWatch = defaultFileSystem.getPath(pathToWatchString);
        pathToWatch.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
        while(true) {
            try {
                WatchKey key = watchService.take();
                if (key != null) {
                    for (WatchEvent<?> event: key.pollEvents()) {
                        if (event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE))
                        {
                            WatchEvent<Path> ev = (WatchEvent<Path>)event;
                            Path filename = ev.context();
                            Path fullFilename = pathToWatch.resolve(filename);
                            action.performAction(fullFilename);
                        }
                    }
                }
            } catch (InterruptedException error) {
                return;
            }
        }
    }

    public static void main(String[] args) throws IOException {
        FileCreatedAction action = new FileCreatedAction() {

            @Override
            public void performAction(Path fullPath) {
                System.out.printf("Found file %s", fullPath);
            }
        };

        Watcher watcher = new Watcher(action, "/foo");
        watcher.start();
    }

}

interface FileCreatedAction {
    void performAction(Path fullPath);
}
packagewatcher;
导入java.io.IOException;
导入java.nio.file.FileSystem;
导入java.nio.file.FileSystems;
导入java.nio.file.Path;
导入java.nio.file.StandardWatchEventTypes;
导入java.nio.file.WatchEvent;
导入java.nio.file.WatchKey;
导入java.nio.file.WatchService;
公共类观察者{
私有最终文件创建操作;
私有最终字符串路径为WatchString;
公共观察程序(FileCreatedAction操作,字符串路径到观察字符串){
这个动作=动作;
this.pathToWatchString=pathToWatchString;
}
public void start()引发IOException{
FileSystem defaultFileSystem=FileSystems.getDefault();
WatchService WatchService=defaultFileSystem.newWatchService();
Path pathtwatch=defaultFileSystem.getPath(pathtwatchstring);
pathToWatch.register(watchService、StandardWatchEventTypes.ENTRY\u CREATE);
while(true){
试一试{
WatchKey=watchService.take();
if(key!=null){
for(WatchEvent事件:key.pollEvents()){
if(event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE))
{
WatchEvent ev=(WatchEvent)事件;
路径文件名=ev.context();
Path fullFilename=pathtwatch.resolve(文件名);
action.performation(完整文件名);
}
}
}
}捕获(中断异常错误){
返回;
}
}
}
公共静态void main(字符串[]args)引发IOException{
FileCreatedAction动作=新建FileCreatedAction(){
@凌驾
公共无效性能(路径完整路径){
System.out.printf(“找到文件%s”,完整路径);
}
};
观察者观察者=新观察者(操作,“/foo”);
watcher.start();
}
}
接口FileCreatedAction{
无效执行(路径完整路径);
}

您可以轻松实现自己的文件系统跟踪器。 这里有一个很好的工作示例:


通常,您需要的是一个名为“”的设计模式。您可以实现自己的,而不需要任何内置支持或外部框架

对于内置支持,请检查Java的“util”包中的“Observer”和EventListener(自Java 7)接口

此外,请查看以下链接:

(一)


2)

有没有办法避免连续循环?我的意思是,我希望基本上有一个Action类和一个Trigger类(带有方法isTriggered()),它可以是一个新添加的文件,并在触发器为true时执行操作。WatchService方法take()避免了繁忙的等待循环。