Solved

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

Posted on 2004-08-17
6
224 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How Complex Is This Java Course ? 9 63
strDist challenge 35 84
mapBully challenge 6 89
eclipse java  build path 6 21
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now