Notifications of client job queue events
From JPPF 4.2 Documentation
Main Page > Development guide > Job queue events |
The JPPF client alllows receiving notifications of when jobs are added to or removed from its queue. To this effect, the AbstractGenericClient class (the super class of JPPFClient) provides methods to register or unregister listeners for these notifications:
public abstract class AbstractGenericClient extends AbstractJPPFClient { // Register the specified listener to receive client queue event notifications public void addClientQueueListener(ClientQueueListener listener) // Unregister the specified listener public void removeClientQueueListener(ClientQueueListener listener) }
As we can see, these methods accept listners of type ClientQueueListener, defined as follows:
public interface ClientQueueListener extends EventListener { // Called to notify that a job was added to the queue void jobAdded(ClientQueueEvent event); // Called to notify that a job was removed from the queue void jobRemoved(ClientQueueEvent event); }
The jobAdded() and jobRemoved() methods are notifications of events of type ClientQueueEvent:
public class ClientQueueEvent extends EventObject { // Get the JPPF client source of this event public JPPFClient getClient() // Get the job that was added or removed public JPPFJob getJob() // Get all the jobs currently in the queue public List<JPPFJob> getQueuedJobs() // Get the size of this job queue public int getQueueSize() }
Here is an example usage, which adapts the size of a client connection pool based on the number of jobs in the queue:
JPPFClient client = new JPPFClient(); JPPFConnectionPool pool; // wait until "myPool" is initialized while ((pool = client.findConnectionPool("myPool")) == null) Thread.sleep(20L); final JPPFConnectionPool thePool = pool; // register a queue listener that will adapt the pool size client.addClientQueueListener(new ClientQueueListener() { @Override public void jobAdded(ClientQueueEvent event) { int n = event.getQueueSize(); // grow the connection pool JPPFConnectionPool pool = event.getClient().findConnectionPool("myPool"); if (n > pool.getMaxSize()) pool.setMaxSize(n); } @Override public void jobRemoved(ClientQueueEvent event) { int n = event.getQueueSize(); // shrink the connection pool JPPFConnectionPool pool = event.getClient().findConnectionPool("myPool"); if (n < pool.getMaxSize()) pool.setMaxSize(n); } }); // ... submit jobs ...
Main Page > Development guide > Job queue events |