Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-17
6
Medium Priority
?
240 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

916 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