2016-12-15 173 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