Giter Site home page Giter Site logo

redjob's Introduction

RedJob Build Status

Spring based implementation of a Redis backed job queue.

RedJob is based on the ideas of Resque and Jesque, but is not compatible with them.

Build the project

To build (clean/compile/test/install) the project, invoke

$ mvn clean install

Build a release

To publish a release to maven central, invoke

# Create release and tag it in git:
$ mvn release:prepare
# Deploy it to maven central:
$ mvn release:perform

Usage

Context

Setup of a basic environment for executing jobs, described in Spring XML context syntax:

<!-- Setup Redis data source. -->
<bean id="jobRedis" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  <property name="hostName" value="localhost" />
  <property name="port" value="6379" />
  <property name="database" value="0" />
  <property name="timeout" value="2000" />
</bean>

<!-- Configure JSON serialization. -->
<bean id="executions" class="com.s24.redjob.worker.json.ExecutionRedisSerializer" />
<!-- Search job class in classpath. -->
<bean id="typeScanner" class="com.s24.redjob.worker.json.TypeScanner"
    p:executions-ref="executions"
    p:basePackages="com.s24.myapp" />
<!-- Factory for creating job runners for jobs. -->     
<bean id="jobRunnerFactory" class="com.s24.redjob.worker.runner.InterfaceJobRunnerFactory" />

<!-- DAO for worker states. -->
<bean id="workerDao" class="com.s24.redjob.worker.WorkerDaoImpl"
    p:connectionFactory-ref="jobRedis"
    p:namespace="mynamespace" />

<!-- DAO for queues. -->
<bean id="fifoDao" class="com.s24.redjob.queue.FifoDaoImpl"
    p:connectionFactory-ref="jobRedis"
    p:namespace="mynamespace"
    p:executions-ref="executions" />

<!-- Worker polling the queue and executing jobs. -->
<bean id="defaultWorker" class="com.s24.redjob.queue.FifoWorkerFactoryBean"
    p:workerDao-ref="workerDao"
    p:name="myworker:[hostname]"
    p:fifoDao-ref="fifoDao"
    p:queues="myqueue"
    p:jobRunnerFactory-ref="jobRunnerFactory" />

<!-- Client, e.g. for adding jobs. -->
<bean id="redjobClient" class="com.s24.redjob.client.ClientFactoryBean"
    p:connectionFactory-ref="jobRedis"
    p:namespace="mynamespace"
    p:executions-ref="executions" />

Jobs, runners and factories

Every bean that can be serialized to JSON by Jackson can be a job. To execute a job, a job runner is required. Creating job runners is done by a JobRunnerFactory. The easiest version of a JobRunnerFactory is the InterfaceJobRunnerFactory. For this factory to work, the job runners need to implement the JobRunner interface. The factory evaluates the type parameter J used by the concrete JobRunner implementation to determine for which job the job runner is for.

Example

Assuming you have a Spring application context configured like described above, here are some very basic classes to create a RedJob "Hello world!":

@JsonTypeName
public class HelloWorld {
   public String message = "Hello world!";
}

@JobRunnerComponent
public class HelloWorldJobRunner implements JobRunner<HelloWorld> {
   @Override
   public void execute(HelloWorld helloWord) {
      System.out.println(helloWord.message);      
   }
}

@Service
public class Sender {
   @Autowired
   private Client client;
   
   public void send() {
      client.enqueue("myqueue", new HelloWorld());
   }
}

You need a local Redis to make the example work:

redis-server

Calling the send method on Sender adds a HelloWorld job to the myqueue queue.

As long as the application context is active, the worker is polling the queue. It removes the job from the queue, gets a job runner for it from the job runner factory and starts the job runner.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.