Use JMS Clients to Utilize Free Computer Resources
Pages: 1, 2, 3, 4, 5

Servlet work creator

A servlet can be used to create these UnitOfWork instances. Although a WebLogic Server startup class can perform the same functions, for administrative purposes it is easier to send messages to a servlet from a secure Web browser. (An alternative implementation would be to utilize a Web service.) If security is placed on the servlet, an authenticated user can pass it commands in the query string to start and stop delivering units of work to the JMS request queue. I'll show you a skeletal example of the servlet for its methods of interest:

public class WorkServlet extends HttpServlet {

 ...

 private QueueSender qsender;

 private ObjectMessage msg;

 private int numMessages = 5;

 ...

 

 // This places the unit of work on the request queue

 public synchronized boolean sendMessages(int numberOfMessages, 

                                                 PrintWriter o) {

   for(int i=0; i<numberOfMessages; i++) {

     SimpleMatrix simple = new SimpleMatrix();

     // Send message to the request queue

     try {

       msg.setObject(simple);

       msg.setJMSReplyTo(QueueResponder);

       qsender.send(msg);

       if (o != null)

         o.println("Sent a Message to queue.");

     } catch (Exception e) {

       System.out.println("WorkServlet:sendMessages:Cannot" +

          "send message, exception raised");

       e.printStackTrace();

       return false;

     }      

   }

   return true;

 }



 // This may place a few units of work onto the queue on startup

 public void init(ServletConfig config) throws ServletException {

 ...

 }



 // Responds to a Query String action with keywords

 // Action = SEND : place unit of work object onto queue

 // Action = CONNECT : connect to JMS server

 // Action = DISCONNECT : disconnect from JMS server



 public void doGet(HttpServletRequest request, 

                   HttpServletResponse response)

                           throws ServletException, IOException {

   doPost( request, response );

 }

 

 public void doPost(HttpServletRequest request, 

                    HttpServletResponse response) 

                            throws ServletException, IOException {

  ...

  if (request.getParameter("Action").equals("SEND")           

    sendMessages(numberMessages, response.getWriter());

 }



}

The JMS client class's job is simply to accept a message on the request queue, call the doWork() method on the object to perform the work on this machine, and send the results back to the reply queue, where a message-driven bean picks up the results for further processing and storage. You can check to see if this is the text message to allow a control message to be sent to the client by telling it to exit processing. Of course, in a real situation, the message may include the name of the client so that not all clients stop processing.

The beauty of using the UnitOfWork interface is that the JMS client, once written, will work for any future classes that implement the interface. This makes the JMS client generic enough to be used in many different scenarios without modification. All that is needed is that the compiled UnitOfWork interface and all its implementing classes be in the client's classpath.

In this simplistic model, the client waits for a message to start processing. In a real situation, the client's onMessage() method may wait depending on the time of day such as after 5 p.m. or if the CPU load on the machine is less than a threshold. This logic would need to be added to the client to make it more in tune with a machine's schedule for usage.

Pages: 1, 2, 3, 4, 5

Next Page ยป