We have a web app in Jboss 6.4 that has to check a Folder( new Files ), our idea was use Commons IO 2.4 with FileAlterationMonitor, like a backup we wanted to control the monitor.. if is working properly or not,
For this.. we created a TimerTask to control if the thread it's running if not, create another observer to continue with the work.
Our problem:
Now in our Test's we provoked a Exception to kill the Observer and detect that if he is not working and restart again, but We don't know how we have to do this with in FileAlterationMonitor, FileAlterationObserver or in FileAlterationListener, and how?
public class App {
private static final String FOLDER ="/Folder";
private static final FileAlterationMonitor monitor = new FileAlterationMonitor(1000);
public static void main(String[] args) throws Exception {
final File directory = new File(FOLDER);
FileAlterationObserver fao = new FileAlterationObserver(directory);
fao.addListener(new FileAlterationListenerImpl());
monitor.addObserver(fao);
monitor.start();
TimerTask task = new TimerTask() {
@Override
public void run() {
System.out.println("Monitor Controler");
ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
int noThreads = currentGroup.activeCount();
Thread[] lstThreads = new Thread[noThreads];
currentGroup.enumerate(lstThreads);
for (int i = 0; i < noThreads; i++) {
System.out.println("Thread No:" + i + " = "+ lstThreads[i].getName());
System.out.println(lstThreads[i].getState().toString());
System.out.println(lstThreads[i].isInterrupted());
System.out.println(lstThreads[i].isAlive());
}
for (FileAlterationObserver o :monitor.getObservers()) {
String obName = o.toString();
String obDir = o.getDirectory().toString();
for(FileAlterationListener l :o.getListeners()){
String listener = l.toString();
}
}
};
Timer timer = new Timer();
long delay = 0;
long intevalPeriod = 1 * 1000;
// schedules the task to be run in an interval
timer.scheduleAtFixedRate(task, delay, intevalPeriod);
}
}
My Solution:
public class App {
private static final String FOLDER = "/Folder/";
private static final FileAlterationMonitor monitor = new FileAlterationMonitor(1000);
public static void main(String[] args) throws Exception {
final File directory = new File(FOLDER);
FileAlterationObserver fao = new FileAlterationObserver(directory);
fao.addListener(new FileAlterationListenerImpl());
monitor.addObserver(fao);
monitor.start();
TimerTask task = new TimerTask() {
@Override
public void run() {
ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
int noThreads = currentGroup.activeCount();
Thread[] lstThreads = new Thread[noThreads];
currentGroup.enumerate(lstThreads);
boolean isDead = true;
for (int i = 0; i < noThreads; i++) {
// System.out.println("Thread No:" + i + " = "+ lstThreads[i].getName());
// System.out.println("getState: "+lstThreads[i].getState().toString());
// System.out.println("isInterrupted: "+ lstThreads[i].isInterrupted());
// System.out.println("isAlive: "+lstThreads[i].isAlive());
if(lstThreads[i].getName().equals("monitorThread"))
{
isDead= false;
}
}
if(isDead){
try {
monitor.stop();
monitor.start();
isDead = false;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Timer timer = new Timer();
long delay = 0;
long intevalPeriod = 1 * 1000;
timer.scheduleAtFixedRate(task, delay, intevalPeriod);
}
}
In my FileAlterationListenerImpl :
@Override
public void onStart(final FileAlterationObserver observer) {
Thread.currentThread().setName("monitorThread");
Is the only place I could set the name of the Thread..