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)
{
}
}
}
}
}
No comments:
Post a Comment