Unfortunately, it doesn’t work with the newest versions of JRuby and
looks
like it isn’t being maintained. I looked a bit at the Quartz
documentation
and think this can not only be updated, but done in such a way that all
the
configurations are done in YAML or Ruby rather than in XML and requiring
a
Servlet to work  this should be something that can be used for ANY kind
of
Ruby application.
I’m thinking that the interface from the Ruby side could look something
like
this:
Unfortunately, the third parameter requires a Java class that conforms
to
the Job interface. Using “include” I can create a Ruby class that
implements
the interface, but I either get an error that it can’t accept
org.jruby.RubyClass or some crazy error like:
Problem instantiating class ‘org.jruby.gen.InterfaceImpl57204556’
Is there a way to pass a JRuby class to a Java object expecting a Java
class?
Write all the scheduling madness in Java and invoke a JRuby
interpreter.
So far I’ve tried this:
public class RubyJob implements Job
{
public void execute(JobExecutionContext context) throws
JobExecutionException {
System.out.println(“Executing job”);
Ruby runtime = Ruby.newInstance();
runtime.evalScriptlet(“require ‘job.rb’”);
}
}
Probably not the best way to do things. Is this the preferred way to
execute
Ruby from Java?
My preference is to figure out a way to make option 1 work, since I can
write most of the gem in Ruby and not face the possibility of the JRuby
interface breaking. Any ideas on a lightweight solution?
Hi Akai,
i dont know if it is an option but You might try Crone4J.
It’s java library used by me pretty much, and since You use JRuby You
have a choice.
Write all the scheduling madness in Java and invoke a JRuby interpreter.
}
Probably not the best way to do things. Is this the preferred way to execute
Ruby from Java?
My preference is to figure out a way to make option 1 work, since I can
write most of the gem in Ruby and not face the possibility of the JRuby
interface breaking. Any ideas on a lightweight solution?
Unfortunately, it doesn’t work with the newest versions of JRuby
I’m currently running Jruby-1.5 with jruby-rack-0.9.2 on tomcat using a
patched version of the Quartz-Rails plugin from Kraemer. I’ve posted it
a few weeks ago on this list.
However using Quartz with a YAML-config file without the need of a
servlet would be very nice!
I was looking a few weeks ago for the same thing (JRuby + Quartz + Job
implementation in Ruby + not limited to a JEE environment). Because I’m
busy
at the moment, I can’t finish my analysis.
A few things I have learned:
I subclass JobDetail to make it possible to pass a job instance rather
than a class name (RAILS_ROOT/lib/sharing_stateless_job_detail.rb):
class SharingStatelessJobDetail < org.quartz.JobDetail
attr_accessor :job
def initialize(name, group, job)
super()
setName name
setGroup group @job = job
end
def validate
raise org.quartz.SchedulerException.new(“Job’s name cannot be null”,
org.quartz.SchedulerException.ERR_CLIENT_ERROR) if getName == nil
raise org.quartz.SchedulerException.new(“Job’s group cannot be
null”,
org.quartz.SchedulerException.ERR_CLIENT_ERROR) if getGroup == nil
end
end
I create a new JobFactory which return my passed job instance, rather
than
create a new instance from the provided class
(RAILS_ROOT/lib/sharing_stateless_job_factory.rb):
Christian, wow, thanks for the awesome detailed email! I’ll have to dig
back
into this when I get some free cycles.
Regarding a new thread  when you do jruby script/server, it starts a
new
instance of Java, so hitting Control-C should kill the instance of Java,
which should, in turn, cull all the running threads. Quartz doesn’t do
anything analogous to forking, so I don’t think this’ll be a problem.
Also  in a JEE server, I believe the SchedulerFactory will return the
same
scheduler. This is because even though multiple Rails instances are
started,
Rails instances can share static Java objects. This should not be an
issue
if you are on Rails 2.2, the threadsafe release of Rails, since only one
instance of Rails is started.
Thanks for the great email. I’ll look into this when I get a chance.
I’ll
also look at Crone4J and rufus-scheduler, per the other comments I’ve
received in this thread.
today, I commit the first version of my quartz scheduler plugin. It has
some
issues, but you can play with this version and any notes are welcome…
You find the plugin with install instructions ans the list of known
issues
at http://github.com/muellerc/quartz_scheduler
My understanding (verified with a debugging session) is, Quartz create a
Threadpool with WorkerThreads (the default is 10). If I run a simple job
in
my java ide (eclipse) and exit the main method, the job is still
running.
If I run the job in rails and hitting Ctrl+C, I have the following
output:
chris:~/workspaceJRuby/scheduler mullerc$ jruby script/server
=> Booting Mongrel (use ‘script/server webrick’ to force WEBrick)
=> Rails 2.2.2 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment…
Jan 17, 2009 1:20:59 PM org.quartz.simpl.SimpleThreadPool initialize
INFO: Job execution threads will use class loader of thread: main
Jan 17, 2009 1:20:59 PM org.quartz.core.SchedulerSignalerImpl
INFO: Initialized Scheduler Signaller of type: class
org.quartz.core.SchedulerSignalerImpl
Jan 17, 2009 1:20:59 PM org.quartz.core.QuartzScheduler
INFO: Quartz Scheduler v.1.6.4 created.
Jan 17, 2009 1:20:59 PM org.quartz.simpl.RAMJobStore initialize
INFO: RAMJobStore initialized.
Jan 17, 2009 1:20:59 PM org.quartz.impl.StdSchedulerFactory instantiate
INFO: Quartz scheduler ‘DefaultQuartzScheduler’ initialized from default
resource file in Quartz package: ‘quartz.properties’
Jan 17, 2009 1:20:59 PM org.quartz.impl.StdSchedulerFactory instantiate
INFO: Quartz scheduler version: 1.6.4
Jan 17, 2009 1:21:00 PM org.quartz.core.QuartzScheduler setJobFactory
INFO: JobFactory set to: [email protected]
intializing quartz scheduler…
intializing job ‘LoggingJob’…
quartz scheduler and all jobs initialized
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready. TERM => stop. USR2 => restart. INT => stop (no
restart).
** Rails signals registered. HUP => reload (without restart). It might
not
work well.
** Mongrel 1.1.5 available at 0.0.0.0:3000
** Use CTRL-C to stop.
Jan 17, 2009 1:21:05 PM org.quartz.core.QuartzScheduler start
INFO: Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
LoggingJob scheduled at Sat Jan 17 13:21:05 +0100 2009
^C** INT signal received.
Exiting
I don’t know, why rails isn’t shutting down as expected. I must kill the
process… :o( Some test with the ShutdownHookPlugin from quartz are
also
not successful (with rails). Now, I will find out the reason…
Tomorrow, I will package the project with warbler and test the
SchedulerFactory
and Scheduler behavoir in a JEE environment.