jms - serial execution in Java-ee -
i have incoming concurrent requests should processed in serial. have attempted achieve converting requests messages , post jms queue. use mdb process queue.
using vendor specific config, understand can limit mdb 1 instance, recommended , portable way solve problem?
edit: forgot mention don't need features of jms (reliability etc).
assume have job this.
class logjob implements runnable{ private final string name; public logjob(string name){ this.name = name; } @override public void run() { system.out.println(" starting ."+name); try { thread.sleep(1000); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println(" end ."+name); } }
it display starting , ending job. placed sleep demo
create list of jobs
arraylist<logjob> jobs = new arraylist<logjob>(); ( int i=0;i<10;i++){ logjob job = new logjob("job"+i); jobs.add(job); }
let see how process in serial
executorservice singlethread = executors.newsinglethreadexecutor(); (iterator<logjob> iterator = jobs.iterator(); iterator.hasnext();) { singlethread.execute(iterator.next()); } singlethread.shutdown();
this provide output.
starting .job0 end .job0 starting .job1 end .job1 starting .job2 end .job2 starting .job3 end .job3 starting .job4 end .job4 starting .job5 end .job5 starting .job6 end .job6 starting .job7 end .job7 starting .job8 end .job8 starting .job9 end .job9
update
based on conversation in comment, came know have use in java-ee environment. said have use managedexecutorservice
. how ever dont need use singleton ejb , concurrentlinkedqueue.
you can implement jobs callable , can block further processing using future.get()
string name = managedservice.submit(iterator.next()).get();
from api
if block waiting task, can use constructions of form result = exec.submit(acallable).get();
Comments
Post a Comment