HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Distributed Tasking in Java"
Volume: 5 Issue: 4, Page 87

	

Listing 1:

public abstract class Task implements java.io.Serializable
{
...
}


Listing 2:

struct Task
{
// Add information specific to the task
};


Listing 3:

public abstract class Task implements javax.ejb.EntityBean
{
...
}


Listing 4:

import java.rmi.*;
import java.rmi.server.*;
public abstract class Handler extends Thread
{

protected abstract void handle(Task task) throws Exception;
   public void run()
   {
      try
      {
         Scheduler scheduler = (Scheduler)  Naming.lookup("rmi://localhost/scheduler");                 
         while (true)
         {
            Task[] tasks = scheduler.getTasks(10);

            for (int i=0 ; i<tasks.length ; i++)
            {
               try
               {
                  handle(tasks[i]);
               }
               catch (Exception handleException)
               {
                  handleException.printStackTrace();
               }
            }
            // Sleep for five seconds
            try { sleep(5000); } catch (Exception sleepException) { }
         } 
      } 
      catch (Exception e)
      {
            e.printStackTrace();
      } 
   }  
}


Listing 5:

import java.rmi.*;
public interface Scheduler extends Remote
{
   // Add a Task to the queue
   public void addTasks(Task[] task) throws RemoteException;

   // Get tasks
   public Task[] getTasks(int max) throws RemoteException;
   
   // Indicate completion
   public void complete(Task[] task) throws RemoteException;
}


Listing 6:

import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;
public class UpdateHandler extends Handler
{
   private Connection connection_ = null;       
   public UpdateHandler() throws Exception
   {
      Class.forName(driver);
      connection_ = DriverManager.getConnection(url, user, password);
   }
   protected void handle(Task task) throws Exception
   {
      if (task instanceof UpdateTask)
      { 
         Exception exception = null;
         UpdateTask updateTask = (UpdateTask) task;
         Statement stmt = -znull;
         try
         {      
            stmt = connection_.createStatement();
                                
            String sql = updateTask.getSQL();
                                                                
            stmt.executeUpdate(sql);
         }
         catch (Exception e)
         {
            exception = e;
         }
         finally
         {
            try
            {
               if (stmt != null)
                  stmt.close();
            }
            catch (Exception fe)
            {
            }
         }
      }
   }
}



 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.