Notifications of client job queue events
From JPPF 6.3 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();
// wait until a pool is initialized
final JPPFConnectionPool thePool = client.awaitWorkingConnectionPool();
// 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
if (n > thePool.getSize()) thePool.setSize(n);
}
@Override
public void jobRemoved(ClientQueueEvent event) {
int n = event.getQueueSize();
// shrink the connection pool
if (n < thePool.getSize()) thePool.setSize(n);
}
});
// ... submit jobs ...
| Main Page > Development guide > Job queue events |

