How do I set up a batch job session based on priority?

I looked through the documentation and also made numerous attempts by Google to learn, but so far I have come empty-handed:

1) Is there a custom parameter that would allow me to achieve the following? If so, how to configure it?

I would like to set up batch jobs so that in addition to the โ€œnormalโ€ priority, I could be able to run โ€œhighโ€ priority tasks that could bring others into line. Among the โ€œnormalโ€ FIFO priority tasks are fine. I would like to have permanent records of the work performed and their status, preferably with automatic failure attempts.

I work with Spring -Batch 3.0.3 in particular and Spring 4.0.6 in general. I submit jobs from a web service on a JBoss AS 7.1.1 server.

2) If there is no implementation outside the box, can I write something (taskExecutor?) To achieve this? And how can I do this?

I have a suggested ThreadPoolExecutor to work with, but the Job class is still unsolvable because I cannot find where to specify the job class. (For a number of reasons, I make settings in jXML, not programmatically with annotations.) No matter what I do, the deployment is always done with org.springframework.batch.core.job.flow.FlowJob, and then can't convert to another class of work.

+5
source share
1 answer

You can use ExecutorService with PriorityBlockingQueue to handle this, for example:

int maxThreads = 4; ExecutorService pool = new ThreadPoolExecutor(1, maxThreads, 1, TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>()); 

Then for as many tasks as you have:

 Job job = new Job(desiredPriority); pool.execute(job); 

They will be ordered by the PriorityBlockingQueue application based on the priority you passed to them.

Runnable and Comparable will be implemented in your Job class:

 public class Job implements Runnable, Comparable<Job> { private final int priority; public Job(int priority) { this.priority = priority; } @Override public int compareTo(Job o) { // enforces descending order (but this is up to you) if (this.priority > o.priority) { return -1; } else if (this.priority < o.priority) { return 1; } else { return 0; } } @Override public void run() { // do whatever needs to happen on a thread } } 
0
source

All Articles