0

Within my Java batch job (JSR-352, JBeret) I have reached a point where I (optionally) would like to wait for a user decision. According to my research, the JSR-352 specification does not provide a "waiting" concept. So the question is, what other options are there?

My current considerations:

  1. I'd prefer not to split the job as this would require to maintain the connection between the parts for monitoring needs.
  2. To introduce JMS into the project only for this purpose seems somewhat overkill.
  3. Polling the database via JPA or JDBC doesn't seem like a nice solution either.
  4. The specific information is not yet available when the job is started, it, therefore, cannot be passed as a job parameter.
import javax.batch.api.Decider;
import javax.batch.runtime.StepExecution;
import javax.inject.Named;

@Named
public class AwaitingDecider implements Decider {

    @Override
    public String decide(final StepExecution[] executions) {
        String decision = // how to wait here?
        return decision;
    }
}
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106

2 Answers2

1

You can use some java concurrency construct like CountDownLatch to coordinate different parts of your program. The important part for the batch application is to have a shared object that holds the lock (or similar) and all parts are accessing the same instance. See a related thread: Java batch: jobContext transientUserData not passed through steps , especially the comment related to using @JobScoped CDI scope provided by JBeret.

If you are running in EE, e.g., WildFly, you can also consider keeping the waiting/locking in a @Singleton ejb with bean-managed concurrency.

cheng
  • 1,076
  • 6
  • 6
0

In this scenario you need to halt your job to wait for an external event. Here it is users, it could be other enterprise information systems, even run from other companies. How soon can you expect a response? Is it seconds? Minutes? Hours? Days?

Be aware that for whatever amount of time, your batch job would have to wait. Using singletons or locks or polling will use resources on your batch machine. But even more, what would happen to your job once the JVM is killed and restarted?

I think such scenarios are those where you hit the limits of JSR-352 and want to switch to a workflow engine, possible one that performs BPM.

https://stackoverflow.com/search?q=java+workflow+engine

Queeg
  • 7,748
  • 1
  • 16
  • 42