Solved

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

Posted on 2004-08-17
6
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

621 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