출처: 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); } |
[출처] 파일 감시 데몬 만들기|작성자 zulu
'IT_Programming > Java' 카테고리의 다른 글
Java theory and practice: : 클로저(closure) 논의 (0) | 2013.07.27 |
---|---|
jsoup: Java HTML Parser (0) | 2013.07.21 |
PreparedStatement로 2000byte를 못 넣는다? (0) | 2011.11.27 |
Serializable 를 이용한 Clone (0) | 2011.11.26 |
[JSON-LIB 사용] JSON 형태의 Data 사용하기 (0) | 2011.07.04 |