Accessing Work Managers in your applications

As we have seen, Weblogic Work Managers provide a server-level configuration that allows administrators a way to set dispatch-policies to their Servlets and EJBs.

WebLogic Server also provides a programmatic way of handling work from within an application. This can be carried out via the CommonJ API. Weblogic Server implements the commonj.work and commonj.timers packages to handle concurrent programming of EJBs and Servlets.

The following code example shows how to lookup a CommonJ Work Manager (bound in the JNDI name “java:comp/env/wm/myWM”) and use it in a Servlet:

public class HelloWorldServlet extends HttpServlet {

   public void doGet(HttpServletRequest req, HttpServletResponse res)

      throws IOException {

      PrintWriter out = res.getWriter();

      try {

         InitialContext ic = new InitialContext();

         WorkManager wm = (WorkManager)ic.lookup

            ("java:comp/env/wm/myWM");     

         wm.schedule(new Work(){

         public void run() {

         ExecuteThread th = (ExecuteThread) Thread.currentThread();

         System.out.println("Self-tuning workmanager: " +

           th.getWorkManager().getName());

         }

         public void release() {}

         public boolean isDaemon() {return false;}

         });

      }

      catch (Exception e) { e.printStackTrace();  }

      out.println("<h4>Servlet responded!</h4>");

      System.out.println("finished execution");

    }

}

 

Francesco Google+