0

I need to have a Java method run every 30 seconds within a WebSphere 7 clustered environment (two boxes with 1 server each) - what's the current best-practice to do this while avoiding concurrency issues?

Some more details: We've got records in an Oracle database that need to be twiddled exactly once. If they get double-twiddled, bad things will happen.

So I'm imagining something like this:

public synchronized void BatchTwiddle() {

    List myList = findRecordsToBeTwiddled();
    twiddleRecords(myList);
}


public void twiddleRecords(myRecords myList) {

    ListIterator<myRecord> myRecordsIterator = myList.listIterator();

    while (myRecordsIterator.hasNext()) {
         myRecordsIterator.next().twiddleRecord();
    }
}

How do I get BatchTwiddle() called every thirty seconds when there's multiple servers (A total of 2) involved? Is it best to just run it on ONE server

So far, I've been digging into the WebSphere Scheduler concept, using ScheduledExecutorService, or using EJB Timers, but nothing seems like a clear winner yet.

john3103
  • 101
  • 2
  • Well, what's wrong with a `ScheduledExecutorService`? – fge Apr 05 '14 at 18:10
  • @fge - I honestly don't know. I'm new enough to Java that, while ScheduledExecutorService LOOKS like it will solve my problem, I can't tell if it's the best way to handle it, or if there will be big architectural problems down the road. – john3103 Apr 05 '14 at 18:30
  • There is no reason why there should be architectural problems at all, `ScheduledExecutorService` Just Works(tm). What is more, unlike other solutions, it is insensitive to OS time change. – fge Apr 05 '14 at 18:35
  • 2
    The disadvantage to ScheduledExecutorService is that it uses unmanaged threads, so things like `java:comp` lookups won't work. – Brett Kail Apr 07 '14 at 00:11
  • You might want to have a look at http://stackoverflow.com/questions/6607101/commonj-timermanager-versus-ejb3-timerservice – ᄂ ᄀ Apr 09 '14 at 01:15

1 Answers1

0

Use node configuration to know which node have to run your task and which not. Then create construction like this:

if(shouldRun(THIS_NODE_NAME)) {
   //do job...
}
MGorgon
  • 2,547
  • 23
  • 41