我有一个根据我的UI的请求来观看HotFolder的要求我的UI上有启动和停止按钮,当我点击start时,我的代码应该观看文件夹,点击stop它应该停止观看它。我正在使用监视服务来监视HotFolder,我正在从我的控制器向监视服务传递标志,以启动和停止监视文件夹。请建议我怎样才能不再看文件夹?
以下是代码片段:
@RequestMapping(value = "/start", method = RequestMethod.GET)
public ModelAndView hotFolder()
{
ModelAndView model = new ModelAndView();
model.setViewName("welcomePage");
HotFolder h = new HotFolder();
h.hotfolderTesting(true);
return model;
}
@RequestMapping(value = "/stop", method = RequestMethod.POST)
public ModelAndView hotFolderStop()
{
ModelAndView model = new ModelAndView();
model.setViewName("welcomePage");
HotFolder h = new HotFolder();
h.hotfolderTesting(false);
return model;
}HotFolder.java:
public void hotfolderTesting(boolean flag)
{
try (WatchService service = FileSystems.getDefault().newWatchService()) {
Map<WatchKey, Path> keyMap = new HashMap<>();
Path path = Paths.get("E:\\TestingWatch");
keyMap.put(path.register(service, StandardWatchEventKinds.ENTRY_CREATE), path);
WatchKey watchKey;
if (flag) {
while (true) {
watchKey = service.take();
for (WatchEvent<?> event : watchKey.pollEvents()) {
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
System.out.println("Created: " + event.context());
} else if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
System.out.println("Deleted: " + event.context());
} else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
System.out.println("Modified :" + event.context());
}
}
if (!watchKey.reset()) {
break;
}
}
}
} catch (Exception ignored) {
}
}发布于 2016-12-16 06:27:31
肯定有一些你没有提到的东西,因为我看到的注释让我认为你在使用某种基于REST的库。
无论如何,你的代码永远不会工作。这是因为你基本上是把回答你问题的线程变成了监视线程,而这永远不会完成你的工作。我建议使用Singleto模式,像这样编写您的HotFolder类:
public class HotFolder implements Runnable{
private static HotFolder instance = null;
public static HotFolder getInstance(){
if(instance == null)
instance = new HotFolder();
return instance;
}
private boolean running = false;
private Thread t = null
private HotFolder(){
}
public setRunning(boolean running){
this.running = running;
if(running && t == null){
t = new Thread(this);
t.start()
}else if(!running && t!= null){
t = null;
}
}
public boolean getRunning(){
return running;
}
public void run(){
try (WatchService service = FileSystems.getDefault().newWatchService()) {
Map<WatchKey, Path> keyMap = new HashMap<>();
Path path = Paths.get("E:\\TestingWatch");
keyMap.put(path.register(service, StandardWatchEventKinds.ENTRY_CREATE), path);
WatchKey watchKey;
watchKey = service.take();
do{
for (WatchEvent<?> event : watchKey.pollEvents()) {
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
System.out.println("Created: " + event.context());
} else if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
System.out.println("Deleted: " + event.context());
} else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
System.out.println("Modified :" + event.context());
}
}
}while(running && watchKey.reset());
} catch (Exception ignored) {
}
}
}然后你就会这样叫它
//to activate
HotFolder.getInstance().setRunning(true)
//to stop it
HotFolder.getInstance().setRunning(false)https://stackoverflow.com/questions/41174018
复制相似问题