Solved

How do I code a file/folder/DB "change detection" monitor in a J2EE application?

Posted on 2004-08-17
6
228 Views
Last Modified: 2013-11-24
I must program an application system containing workflows that will require "change detector" triggers in order to set workflow events in motion, so I must create monitors that will keep watch for changes in files, folder contents, and database tables.

This system is part of a J2EE commercial application.

I've never done this kind of "change detection" monitor. Can someone describe how to approach it, or point me toward a tutorial or resource that would shed some light?
0
Comment
Question by:ERP_CodeWarrior
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 250 total points
ID: 11820230
What you have to do is to write a separate thread for each task (database, files etc) that does this. This thread will be checking the targets every few seconds. For exampel to check if a file is changed load up the file and check its last modified date:

static long lastModified = System.currentTimeMillis();
File file = new File("mpla.txt");
if (file.lastModified() > lastModified)
{
    lastModified = file.lastModified();
    // do stuff
}

You will need to do the same for the database and the folder. You will need to hold the previous values and compare them with the current values you receive upon request. For example if you want to check if a folder has more/less files than the last time you checked, you will need to store last time's checked files in a collection and then compare them one by one.

Same for database, although not sure how you could see if data is changed apart from loading up everything and comparing them. There should be something that indicates when the db or a table was last updated, otherwise you might need to add one more field in the db indicating the last access date.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11820255
You could always use the TimerTask class to do it.
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11820403
What you need to do is 1) define what the possible changes you are interested in, define how to recognise such changes from a known state (e.g. monitoring for new file creations in a given directory, first the known state of the directory should be modeled and stored, so perhaps an ArrayList of files, and then periodically compare the known state with the existing state, so get a new directory list, and compare this to the stored state - if there are changes, then trigger whatever tasks are required, and then store this "new" state as the current state for the next periodic check.

You say J2EE - do you mean full EJB compliance, or other areas of J2EE (JMS, etc).

Storing known state should probably be done in a persistent manner (database, etc), so differences across invocations of the software can also be monitored.

To handle the periodic testing, some form of scheduler will be required (you could possibly write one using java.util.Timer and java.util.TimerTask, or probably more "simply", deploy an opensource J2EE scheduler (Quartz is one such, https://quartz.dev.java.net/).

For extendability, I'd use a JMS topic for handling triggering events on "state changes" - a JMS message describing exactly the state change would be placed onto the JMS topic, and every task that is necessary would be implemented as a JMS receiver on this topic (could use MDB, or hand write the receiver yourself). Using a topic allows for distribution, and also allows for adding more functionality later, without having to re-work existing tasks...
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question