Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
237 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

721 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