You are on page 1of 9

Weblogic thread pooling

1. Introduction Thread pooling refers to a technique where a pool of worker threads are created and managed by the application. When a new job arrives, instead of creating a new thread to service it, it's queued by the threadpool manager and dispatched later to one of the available worker threads. The thread-pool manager manages the number of active worker threads based on available resources as well as load considerations, adding new threads to the pool or freeing some worker threads in response to the number of outstanding requests. The primary goals of thread pooling are managing the number of active threads in the system and reducing the overhead of creating new threads by reusing threads from a pool. Please click on the below link to know more about thread pooling. http://java.sys-con.com/node/37241 2. Thread pool types There are two ways of implementing thread pool for the web components like JSP, Servlet etc. and its associated classes develop your own thread pool library or leverage out of the box support from application servers like weblogic. Creating your own work managers, work queues, work listeners, work and its synchronizations are so difficult and time consuming. This document outlines how a thread pool can be configured in weblogic and uses it in the programs. 3. Weblogic Configurations As the first step, follow the below steps to configure the thread pool in weblogic which includes specifying the minimum thread count, maximum thread count etc. Screenshots are provided for each step for easy understanding 1) Log into weblogic admin console 2) Expand Environment from the left menu as in the below screenshot

3) Click on Work Managers link

4) Click on Lock & Edit button from the left menu

5) Click on New button

6) Click on Next button

7) Enter the name for your work manager (eg: TestWorkManager) and click on Next button

8) Select the weblogic instance/cluster where your application runs and click Finish

9) Click on Activate Changes from the left top menu.

10) Click on the work manager which is newly created

11) You may configure minimum thread count, maximum thread count; total number of requests can be queued etc. here.

4. Web.xml changes Next step is to specify the thread pool as a resource in the web.xml file as below. Its important to note that the work manager name and <res-ref-name> should match. <web-app> <resource-ref> <res-ref-name>wm/TestWorkManager</res-ref-name> <res-type>commonj.work.WorkManager</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref> 5. Use thread pool in the program Now that you have a thread pool configured in weblogic and can be used in the programs. Next step is to write a class which implements commonj.work.Work interface. The business logic should go inside run() method as shown below. An instance of this class represents a Work/Job/Task. In the below example, one instance of MyWork class can print the numbers in the provided color (eg-green)

class MyWork implements Work{ PrintWriter pw;

String fontcolor; int count; public MyWork(PrintWriter pw, String fontcolor, int count){ this.pw=pw; this.fontcolor = fontcolor; this.count= count; } public void run() { for(int i=0;i<count;i++){ try{pw.println("<font color='"+fontcolor+"'>"+i); pw.flush();}catch(Exception e){} } } public void release() {} public boolean isDaemon() {return false;} } Now you may start schedule all of your work for execution. You can define any number of work as per your requirement and set for execution in parallel. In the below example, you can see two work, where the first one prints numbers in GREEN color and the second one in RED color. The same class (MyWork) is used for creating two work instances here. However this is not necessary, you can have instances of different classes which implements commonj.work.Work interface. The below example program is written in JSP for the ease of testing. You may do the similar coding in other APIs like servlet, EJB etc. First you need to get an instance of Work Manager which is created through the weblogic admin console by using JNDI lookup. Then you can create instances of Work and schedule it for execution. As you see, its important to instruct the Work Manager to wait for the completion of all work. Otherwise its possible that the main program will complete its execution before all or any of the scheduled work complete, and you will not see the output of them. The screenshot of the resultant page can be seen with mixed numbers in GREEN and RED color which proves that the execution has done in parallel and weblogic thread pool worked successfully. <%@page import="commonj.work.*" %> <%@page import="javax.naming.*" %> <%@page import="java.io.*" %> <%@page import="java.util.*" %> <%
//Instantiate Work manager

InitialContext ctx = new InitialContext(); WorkManager workManager = (WorkManager)ctx.lookup("java:comp/env/wm/TestWorkManager"); PrintWriter pw = response.getWriter(); int count=9999;
//Schedule all work for execution, which would run in parallel //Create an instance of work, which prints the numbers in green

WorkItem workItem1 = workManager.schedule(new MyWork(pw,"green",count));


//Create an instance of work, which prints the numbers in green

WorkItem workItem2 = workManager.schedule(new MyWork(pw,"red",count));

List workItemList=new ArrayList();

workItemList.add(workItem1); workItemList.add(workItem2);
//Wait the main thread to finish all the threads

boolean finshed = workManager.waitForAll(workItemList, WorkManager.INDEFINITE); out.println("<font color='blue'>" +finshed + "</font>"); %> 6. Resultant screen

Note- The above configurations and program have been tested in weblogic 10.0.1 version. It should work similarly in other versions from 9.2 without any changes as per Oracle Inc.

You might also like