Thursday, September 29, 2011

WebLogic WorkManager for Multi-threading

WebLogic's WorkManager provides a self-tuned ThreadPool to enable Multi-Threading.

Define a WorkManager either in WLS console or in web.xml as below.

<resource-ref>
<res-ref-name>wm/MyWorkManager</res-ref-name>
<res-type>commonj.work.WorkManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>


In weblogic-application.xml, you can configure the ThreadPool Size

  <work-manager>
<name>wm/MyWorkManager</name>
<max-threads-constraint>
<name>a</name>
<count>5</count>
</max-threads-constraint>
</work-manager>
<work-manager>


Java Code

        try {
InitialContext ic = new InitialContext();
WorkManager workManager = (WorkManager)ic.lookup("java:comp/env/wm/MyWorkManager");
List workItemList=new ArrayList();

for(int i=0;i<10;i++){
WorkItem workItem1 = startWorkItem(my,i,"OSMM");
workItemList.add(workItem1);
}



//run the work items in parallel; don't wait
//workManager.waitForAll(workItemList, WorkManager.IMMEDIATE);


    public WorkItem startWorkItem(WorkManager workManager, final int count, final String wmName) throws Exception{
WorkItem workItem10 = workManager.schedule(new Work() {
public void run() {
try {
System.out.println("Current Count ["+count+"] : wmName ["+wmName+"]ThreadName Is: "+Thread.currentThread().getName()+"##Priroty="+Thread.currentThread().getPriority());
} catch (Exception e) {
e.printStackTrace();
}
}

public void release() {
}

public boolean isDaemon() {
return false;
}
}); //End of Schedule
return workItem10;

}

3 comments:

Anonymous said...

hi,
is it possible to assign a workmanager to a pojo or any other service layer class ?
mine is web application and its mvc
so single servlet entry, how could i assign a work manager to models or pojos that do some business logic but i do not want that all threads gets blocked to a specific model at run time.

king pong siu said...

Hi,
I m wondering if after waitForAll (with a timeout value), for those computation not completed..we could stop the computation and release immediately the related thread.

Alwin Co Daan said...

Very much useful article. Kindly keep blogging

Java Training in Chennai

Java Online Training India