IT_Programming/Java

[펌] 파일 감시 데몬 만들기

JJun ™ 2013. 6. 27. 04:47

 


 출처: http://blog.naver.com/shin7688/120172067214


 

1. 중요 라이브러리:

           - java.util.concurrent.ScheduledExecutorService

 

2. 파일 변경 감시 데몬 만들기(WatcherDaemon)

 

<WatcherDaemon class> 

package test.watcher;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.net.URL;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;

import java.util.concurrent.TimeUnit;

 

public class WatcherDaemon

{

          

           String FILE_ROUTE_CONF_FILE = "watch.txt";

           protected static final int DEFAULT_RELOAD_CHECK_INTERVAL_MS = 10000;

           protected ScheduledExecutorService watchTimer;

           protected String[] absoluteResourcePaths;

          

           public static void main(String[] args)

           {

                     WatcherDaemon daemon = new WatcherDaemon();

                     daemon.init();

           }

          

           public void init()

           {

                     URL url = getClass().getClassLoader().getResource(FILE_ROUTE_CONF_FILE);

                     absoluteResourcePaths = new String[]{url.getPath()};

                     deploy();

                     createRedeployMonitor();

           }

          

           protected void createRedeployMonitor()

           {

                   AbstractFileWatcher watcher = new ConfigFileWatcher(absoluteResourcePaths [0]);

                   scheduleConfigMonitor(watcher);

           }

          

           protected void scheduleConfigMonitor(AbstractFileWatcher watcher)

           {

                  final int reloadIntervalMs = DEFAULT_RELOAD_CHECK_INTERVAL_MS;

                  watchTimer = Executors.newSingleThreadScheduledExecutor();

                  watchTimer.scheduleWithFixedDelay(watcher, reloadIntervalMs,

                  reloadIntervalMs, TimeUnit.MILLISECONDS);

           }

          

           protected class ConfigFileWatcher extends AbstractFileWatcher

           {

                     public ConfigFileWatcher(String watchedResource)

                     {

                                super(watchedResource);

                     }

 

                     @Override

                     protected synchronized void onChange(File file)

                     {

                                deploy();

                     }

           }

          

           public void deploy()

           {

                     InputStream inputStream = null;

                     try {

                                inputStream = new FileInputStream(new File(absoluteResourcePaths [0]));

                     } catch (FileNotFoundException e1)  {                    

                                e1.printStackTrace();

                     }

                     // TODO: implementation

                     if (inputStream != null)

                    {

                                try {

                                          inputStream.close();

                                } catch (IOException e) {

                                          e.printStackTrace();

                                }

                     }

           }

 

}

 

 

 

<AbstractFileWatcher class>

package test.watcher;

 

import java.io.File;

 

import org.apache.log4j.Logger;

 

public abstract class AbstractFileWatcher implements Runnable

{

 

           private transient Logger logger = Logger.getLogger(getClass());

 

           private long timeStamp;

           private File file;

 

           public AbstractFileWatcher(String file_path)

           {

                     this.file = new File(file_path);

                     this.timeStamp = file.lastModified();

           }

 

           public final void run()

           {              

                     long timeStamp = file.lastModified();                   

                     if   (this.timeStamp != timeStamp) {

                                this.timeStamp = timeStamp;

                                try {

                                          onChange(file);

                                } catch (Throwable t) {

                                          logger.error(String.format("Monitor for %s threw an exception", file), t);

                                }

                     }                   

           }

 

           protected abstract void onChange(File file);

}