Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 302353afc0267e470460db885f9ddc84d6e16542 0 parents
Vagmi vagmi authored
5 .gitignore
@@ -0,0 +1,5 @@
+*.tmp
+*.swp
+.~
+pkg
+tmp
4 History.txt
@@ -0,0 +1,4 @@
+=== 0.1.0 2010-12-01
+
+* Basic cron ability with java quartz
+ * Initial release
11 LICENSE
@@ -0,0 +1,11 @@
+Copyright (c) 2010 Vagmi Mudumbai
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing, software distributed under
+the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
14 Manifest.txt
@@ -0,0 +1,14 @@
+History.txt
+LICENSE
+Manifest.txt
+README.markdown
+Rakefile
+lib/quartz.rb
+lib/quartz/cron_job.rb
+lib/quartz/cronify.rb
+lib/quartz/job_detail.rb
+lib/quartz/job_factory.rb
+lib/quartz/scheduler.rb
+lib/quartz/version.rb
+test/test_helper.rb
+test/test_quartz-jruby.rb
21 README.markdown
@@ -0,0 +1,21 @@
+## Quartz for JRuby
+
+From [Quartz Scheduler's website](http://www.quartz-scheduler.org/)
+
+> Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may executed virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.
+
+
+## Usage
+
+ class TestScheduler
+ include Quartz::Scheduler
+ schedule(:say_hello_5, :every=>5.seconds) { info "every 5 seconds" }
+ schedule(:say_hello_30, :every=>30.seconds) { error "every 30 seconds" }
+ end
+ TestScheduler.instance.run
+
+## Feedback
+
+If you have any feedback, send me an email vagmi <at> [artha42.com](http://www.artha42.com).
+
+
26 Rakefile
@@ -0,0 +1,26 @@
+require 'rubygems'
+gem 'hoe', '>= 2.1.0'
+require 'hoe'
+require 'fileutils'
+require './lib/quartz/version'
+
+Hoe.plugin :newgem
+# Hoe.plugin :website
+# Hoe.plugin :cucumberfeatures
+
+# Generate all the Rake tasks
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
+$hoe = Hoe.spec 'quartz-jruby' do
+ self.version = Quartz::VERSION
+ self.developer 'Vagmi Mudumbai', 'vagmi@artha42.com'
+ self.rubyforge_name = self.name # TODO this is default value
+ self.extra_deps = [['activesupport','>= 3.0.3']]
+
+end
+
+require 'newgem/tasks'
+Dir['tasks/**/*.rake'].each { |t| load t }
+
+# TODO - want other tests/tasks run by default? Add them to the list
+# remove_task :default
+# task :default => [:spec, :features]
BIN  java/commons-collections-3.2.1.jar
Binary file not shown
BIN  java/commons-logging.jar
Binary file not shown
BIN  java/log4j.jar
Binary file not shown
BIN  java/quartz-1.6.4.jar
Binary file not shown
14 lib/quartz.rb
@@ -0,0 +1,14 @@
+require 'java'
+$:.unshift(File.dirname(__FILE__)) unless
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
+
+require 'rubygems'
+require 'singleton'
+require 'active_support'
+require 'quartz/version'
+require 'quartz/job_factory'
+require 'quartz/job_detail'
+require 'quartz/cron_job'
+require 'quartz/cronify'
+require 'quartz/scheduler'
+
15 lib/quartz/cron_job.rb
@@ -0,0 +1,15 @@
+require 'java'
+import org.quartz.Job
+module Quartz
+ class CronJob
+ include org.quartz.Job
+ attr_accessor :name
+ def initialize(name)
+ @name=name
+ end
+ def execute(context)
+ job_block = JobFactory.instance.jobs[@name]
+ job_block.call
+ end
+ end
+end
55 lib/quartz/cronify.rb
@@ -0,0 +1,55 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/core_ext'
+
+# this stuff is based on whenver
+# https://github.com/javan/whenever/
+
+module Quartz
+ class Cronify
+ def self.comma_separated_timing(frequency, max, start = 0)
+ return start if frequency.blank? || frequency.zero?
+ return frequency if frequency > (max * 0.5).ceil
+
+ original_start = start
+
+ start += frequency unless (max + 1).modulo(frequency).zero? || start > 0
+ output = (start..max).step(frequency).to_a
+
+ max_occurances = (max.to_f / (frequency.to_f)).round
+ max_occurances += 1 if original_start.zero?
+
+ output[0, max_occurances].join(',')
+ end
+
+
+ def self.cronify(time,at=0)
+ timing = %w(0 * * * * ?)
+ case time
+ when 0.seconds...1.minute
+ timing[0] = "0/#{time.to_s}"
+ when 1.minute...1.hour
+ minute_frequency = time / 60
+ timing[1] = comma_separated_timing(minute_frequency, 59, at || 0)
+ when 1.hour...1.day
+ hour_frequency = (time / 60 / 60).round
+ timing[1] = at.is_a?(Time) ? at.min : at
+ timing[2] = comma_separated_timing(hour_frequency, 23)
+ when 1.day...1.month
+ day_frequency = (time / 24 / 60 / 60).round
+ timing[1] = at.is_a?(Time) ? at.min : 0
+ timing[2] = at.is_a?(Time) ? at.hour : at
+ timing[3] = comma_separated_timing(day_frequency, 31, 1)
+ when 1.month..12.months
+ month_frequency = (time / 30 / 24 / 60 / 60).round
+ timing[1] = at.is_a?(Time) ? at.min : 0
+ timing[2] = at.is_a?(Time) ? at.hour : 0
+ timing[3] = at.is_a?(Time) ? at.day : (at.zero? ? 1 : at)
+ timing[4] = comma_separated_timing(month_frequency, 12, 1)
+ else
+ return 'bummer'
+ end
+ timing.join(' ')
+ end
+ end
+end
13 lib/quartz/job_detail.rb
@@ -0,0 +1,13 @@
+module Quartz
+ class JobDetail < Java::OrgQuartz::JobDetail
+ attr_accessor :job
+ def initialize(name,group,job)
+ super()
+ set_name name
+ set_group group
+ @job = job
+ end
+ def validate
+ end
+ end
+end
14 lib/quartz/job_factory.rb
@@ -0,0 +1,14 @@
+module Quartz
+ class JobFactory
+ include Singleton
+ include org.quartz.spi.JobFactory
+ attr_accessor :jobs
+ def initialize
+ @jobs||={}
+ end
+ def new_job(event)
+ event.get_job_detail.job
+
+ end
+ end
+end
66 lib/quartz/scheduler.rb
@@ -0,0 +1,66 @@
+require 'java'
+
+# java quartz imports
+import org.quartz.CronTrigger
+import org.quartz.impl.StdSchedulerFactory
+
+
+module Quartz
+ module Scheduler
+
+ def self.included(base)
+ base.class_eval <<-EOF
+ include InstanceMethods
+ extend ClassMethods
+ include Singleton
+ EOF
+ end
+
+ module ClassMethods
+ def schedule(name,options,&block)
+ instance.schedule(name,options,block)
+ end
+ def log
+ org.apache.log4j.Logger.get_logger(self.name)
+ end
+ def info(msg)
+ log.log(org.apache.log4j.Level::INFO,msg)
+ end
+ def error(msg)
+ log.log(org.apache.log4j.Level::ERROR,msg)
+ end
+ def warn(msg)
+ log.log(org.apache.log4j.Level::WARN,msg)
+ end
+ end
+
+ module InstanceMethods
+ def schedule(name, options, block)
+ options=defaults.merge options
+ job_factory.jobs[name.to_s] = block
+ job_detail = JobDetail.new(name.to_s, "cronjob",CronJob.new(name.to_s))
+ cron_expression = Cronify.cronify(options[:every],options[:at])
+ trigger=CronTrigger.new("#{name.to_s}_crontrig","cront_trig_group",name.to_s,"cronjob",cron_expression)
+ scheduler.set_job_factory(job_factory)
+ scheduler.schedule_job(job_detail,trigger)
+ end
+ def defaults
+ {
+ :at=>0
+ }
+ end
+ def scheduler_factory
+ @scheduler_factor ||= StdSchedulerFactory.new
+ end
+ def scheduler
+ scheduler_factory.get_scheduler
+ end
+ def job_factory
+ JobFactory.instance
+ end
+ def run
+ scheduler.start
+ end
+ end
+ end
+end
3  lib/quartz/version.rb
@@ -0,0 +1,3 @@
+module Quartz
+ VERSION="0.1.0"
+end
10 script/console
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+# File: script/console
+irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
+
+libs = " -r irb/completion"
+# Perhaps use a console_lib to store any extra methods I may want available in the cosole
+# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
+libs << " -r #{File.dirname(__FILE__) + '/../lib/quartz-jruby.rb'}"
+puts "Loading quartz-jruby gem"
+exec "#{irb} #{libs} --simple-prompt"
14 script/destroy
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/destroy'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Destroy.new.run(ARGV)
14 script/generate
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/generate'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Generate.new.run(ARGV)
12 test/test_helper.rb
@@ -0,0 +1,12 @@
+require 'stringio'
+#require 'test/unit'
+
+JAVA_HOME = File.expand_path(File.join(File.dirname(__FILE__),"..","java"))
+JARS = Dir.glob(JAVA_HOME+"/*.jar").collect { |e| e.split(JAVA_HOME)[1].slice(1..-1) }
+$:.unshift(JAVA_HOME)
+JARS.each { |jar|
+ puts "requiring #{jar}"
+ require jar
+}
+
+require File.dirname(__FILE__) + '/../lib/quartz'
10 test/test_quartz-jruby.rb
@@ -0,0 +1,10 @@
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+org.apache.log4j.BasicConfigurator.configure
+class TestScheduler
+ include Quartz::Scheduler
+ schedule(:say_hello_5, :every=>5.seconds) { info "every 5 seconds" }
+ schedule(:say_hello_30, :every=>30.seconds) { error "every 30 seconds" }
+
+end
+TestScheduler.instance.run
Please sign in to comment.
Something went wrong with that request. Please try again.