techwhizbang / jruby-quartz

A JRuby implementation of the Quartz scheduling framework that makes it easy to integrate into Ruby and Rails based projects.

This URL has Read+Write access

jruby-quartz / README
100644 75 lines (50 sloc) 2.254 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
= jruby-quartz
 
== Instructions
 
Requires the following jars in your application container and if running with just jruby place these in your $JRUBY_HOME/lib
 
commons-collections-3.2.x.jar
commons-logging.jar
log4j.jar
quartz-1.6.x.jar
 
If you are running a current version of JBoss you already have these!
If you are running an alternative container you will need to check it's lib
directory for the presence of these jars.
 
If you need these jars you can find them within the apache commons project and
the quartz jar on opensymphony
 
== Installation
 
  gem install git://github.com/techwhizbang/jruby-quartz.git
 
== Usage
 
The best way to leverage jruby-quartz is to simply extend the BaseScheduler and
implement any number of jobs you need to run.
 
For example all you need to do is specify some basic attributes including the
cron expression representing the frequency of the job(s) you want to with it:
 
class IntegrationScheduler < BaseScheduler
 
  def initialize(cron_expression)
    super
    @cron_expression = cron_expression
    @base_jobs_group = "IntegrationJobs"
    @base_triggers_group = "IntegrationTriggers"
  end
end
 
Then just extend the base job by putting all of the work you want to perform inside the
perform_job method.
 
class IntegrationJob < Jobs::BaseJob
 
  def perform_job
    worker_one = mock('worker class one', :whatever => "123")
    worker_two = mock('worker class two', :whatever => '123')
  end
end
 
If you are running Rails you can throw the schedule! method into a custom initializer.
An example of this would be like so:
Notice the Rails.env /container/, I defined custom environments that only run when
inside a Java container so that the schedule! method doesn't get fired off during
testing.
 
 
if RUBY_PLATFORM =~ /java/
 
  LoggerOneScheduler.new("0 0/1 * * * ?").schedule!(Jobs::LoggerOneJob)
  LoggerTwoScheduler.new("0 0/2 * * * ?").schedule!(Jobs::LoggerTwoJob)
 
end
 
If you are planning on using this with a Rails stack, I would recommend using this
only with Rails 2.2 or higher because thread safety == 1 runtime
 
1 runtime means your schedule initializer is only called once.
 
=== Other
 
Problems, comments, and suggestions all welcome. techwhizbang@gmail.com or visit my blog
http://techwhizbang.com