2016-12-15 117 views
0

我有一个要求,根据来自我的UI的请求观看HotFolder 我有我的用户界面上的启动和停止按钮,当我点击开始我的代码应该观看文件夹并点击停止它应该停止观看它。我正在使用手表服务来观看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) { 
    } 
} 

回答

0

有是肯定的东西,你不提,因为我看到的注解,让我觉得你使用某种基于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) 
+0

我在公共无效setRunning(布尔运行)获得java.lang.IllegalThreadStateException { \t \t this.running = running; \t \t if(running){ \t \t \t this.start(); \t \t} \t} – user7076183

+0

好吧,似乎一个线程不能启动超过一个。我将更改代码以使用runnables。 – bracco23

+0

仍然没有运气:(一些时间线程成为空..有些时候没有..即使当我点击停止按钮(这是调用HotFolder.getInstance()。setRunning(false)方法)仍然看文件夹..(仍然记录在文件夹中创建的新文件) – user7076183