Tuesday, February 4, 2014

Using default WorkManager in your weblogic server


In one of the projects that I work on, we extensively use Weblogic Server. It is great. I thought I would share with all a tip that I learnt today.

If you want to execute a Thread in Weblogic Server, the best idea will be to use a Work object instead of a Thread . Work objects are managed by WorkManagers.  WorkManagers are configured to a great extent. Read about it here.

There are many different types of WorkManagers - Default, Global, Application-level WorkManagers. The default WorkManager, not surprisingly, is used for all internal Weblogic tasks. For most of our needs, the default WorkManager is good enough. Here, I will quickly discuss how to schedule a Work using the default WorkManager.

There are two places you need to change:

First is in your web.xml:
Add a resource-reference to default WorkManager.

  <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>
  
This lets your application refer to default WorkManager. To use it in your code:

InitialContext ic = new InitialContext();
WorkManager wm = (WorkManager) ic.lookup("java:comp/env/wm/default");
wm.schedule(new TestWork());

This should use the default WorkManager to start a Work object.

I should thank this article for helping me understand how I should configure. There are a few modifications I made to suit my needs.