Rafael Sanches
Rafael Sanches

Reputation: 1823

Hazelcast scheduled jobs (Quartz support?)

I know it's unfair with the terracotta guys, but has anyone tried to use Hazelcast in order to use scheduled jobs in a clustered environment?

The simplest implementation I can image is the following architecture:

  1. A global Hazelcast lock for ensuring only one server has startup the Quartz config.
  2. Running the actual tasks as DistributedTask. (this can be done later, for the moment the heavy scheduled tasks will need to take care of triggering DistributedTask)
  3. As soon as the server holding the lock is down, another server gets the lock.

I believe this would be a great advantage for people who already has the Hazelcast, since they won't require the whole dev-environment hassle by opening the terracotta stuff all the time.

For the moment I have coded the simplest solution of making only one node to be in charge of executing Quartz triggers. Since I only use Cron-like triggers, it could be an acceptable solution if I take care of creating DistributedTasks for the heavy trigger tasks.

Here's my org.springframework.scheduling.quartz.SchedulerFactoryBean extension that makes it happen:

@Override
public void start() throws SchedulingException {
    new Thread(new Runnable() {
        @Override
        public void run() {
            final Lock lock = getLock();
            lock.lock();
            log.warn("This node is the master Quartz");
            SchedulerFactoryBean.super.start();
        }
    }).start();
    log.info("Starting..");
}

@Override
public void destroy() throws SchedulerException {
    super.destroy();
    getLock().unlock();
}

Please, let me know if I am missing something Big and if this can be done.

I have added the two files to github. Here's the RAMJobStore extension:

https://github.com/mufumbo/quartz-hazelcast/blob/master/src/main/java/com/mufumbo/server/scheduler/hazelcast/HazelcastRAMJobStore.java

And here's the Spring SchedulerFactoryBean extension:

https://github.com/mufumbo/quartz-hazelcast/blob/master/src/main/java/com/mufumbo/server/scheduler/hazelcast/SchedulerFactoryBean.java

Upvotes: 8

Views: 8073

Answers (2)

ᄂ ᄀ
ᄂ ᄀ

Reputation: 5782

Starting with the version 3.8 you can simply use distributed Scheduled Executor Service:

  • scheduleOnMember()
  • scheduleOnKeyOwner()
  • scheduleOnAllMembers()
  • scheduleOnAllMembers()

See Scheduled Executor Service and IScheduledExecutorService for more details.

Upvotes: 4

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

I was thinking about the same concept some time ago. You can actually integrate Hazelcast with quartz-scheduler easily by implementing JobStore SPI interface. Check out RAMJobStore for reference on how to implement job store based on in-memory data structures and JobStoreTX - clustered, database-backed store.

This interface is quite large, but it should be the only place required to switch from RAM or Terracotta to Hazelcast. The latter library already provides distrubuted storage and locks so it should be rather straightforward.

Would be awesome if you could share your implemention (GitHub?), guess it would be a viable alternative to Terracotta cluster for many people.

Upvotes: 9

Related Questions