Skip to content

kenshoo/jesque

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jesque

Build Status Coverage Status License Apache 2.0 Maven Central

Jesque is an implementation of Resque in Java. It is fully-interoperable with the Ruby and Node.js (Coffee-Resque) implementations.

Jesque is a Maven project and depends on Jedis to connect to Redis, Jackson to map to/from JSON and SLF4J for logging.

The project contains a client implementation as well as a worker implementation that supports listeners.

NOTE: Jesque's delayed jobs implementation is not compatible with resque-scheduler


How do I use it?

Jesque requires Java 7+. Download the latest source at:

https://github.com/gresrun/jesque Or, to use it in your Maven project, add it as a dependency:

<dependency>
  <groupId>net.greghaines</groupId>
  <artifactId>jesque</artifactId>
  <version>2.1.2</version>
</dependency>

Quickstart:

// Configuration
final Config config = new ConfigBuilder().build();

// Add a job to the queue
final Job job = new Job("TestAction",
  new Object[]{ 1, 2.3, true, "test", Arrays.asList("inner", 4.5)});
final Client client = new ClientImpl(config);
client.enqueue("foo", job);
client.end();

// Start a worker to run jobs from the queue
final Worker worker = new WorkerImpl(config,
  Arrays.asList("foo"), new MapBasedJobFactory(map(entry("TestAction", TestAction.class))));
  
final Thread workerThread = new Thread(worker);
workerThread.start();

worker.end(true);
try { workerThread.join(); } catch (Exception e){ e.printStackTrace(); }

Delayed jobs

Delayed jobs can be executed at sometime in the future.

final long delay = 10; // in seconds
final long future = System.currentTimeMillis() + (delay * 1000); // timestamp

client.delayedEnqueue("fooDelay", job, future);

Recurring Jobs

Recurring jobs can start at a specific time and exeucte at specified intervals.

final long delay = 10; // in seconds
final long future = System.currentTimeMillis() + (delay * 1000); // timestamp
final long frequency = 60; // in seconds

client.recurringEnqueue("fooRecur", job, future, (frequency * 1000));

Cancelling jobs

Delayed and recurring jobs can be cancelled.

client.removeDelayedEnqueue("fooDelay", job);
client.removeRecurringEnqueue("fooRecur", job);

Using a ClientPool

ClientPool is useful in multi threaded apps,

final Client jesqueClientPool = new ClientPoolImpl(config, PoolUtils.createJedisPool(config));
jesqueClientPool.enqueue("foo", job);

Listeners

You can execute custom callbacks during specific Worker events.

int myVar = 0;
worker.getWorkerEventEmitter().addListener(new WorkerListener(){
   public void onEvent(WorkerEvent event, Worker worker, String queue, Job job, 
					   Object runner, Object result, Throwable t) {
    if (runner instanceof TestAction) {
        ((TestAction) runner).setSomeVariable(myVar);
    }
  }
}, WorkerEvent.JOB_EXECUTE);

Available Worker Events

  • WORKER_START Finished starting up and is about to start running.
  • WORKER_POLL Polling the queue.
  • JOB_PROCESS Processing a Job.
  • JOB_EXECUTE About to execute a materialized Job.
  • JOB_SUCCESS Successfully executed a materialized Job.
  • JOB_FAILURE Caught an Exception during the execution of a materialized Job.
  • WORKER_ERROR Caught an Exception during normal operation.
  • WORKER_STOP Finished running and is about to shutdown.

For more usage examples check the tests. The tests require that Redis is running on localhost:6379.

Use the resque-web application to see the status of your jobs and workers or, if you prefer Java, try Jesque-Web.

Redis Configuration