Clustering cron jobs with Hazelcast

1,252 views
Skip to first unread message

Roger

unread,
Jul 14, 2010, 7:46:24 AM7/14/10
to Hazelcast
I'd like to have way to make cron jobs (or Quartz jobs) fault-tolerant
using Hazelcast. What I want is that the job can be scheduled
identically on all servers in the cluster, but only one will actually
try to run it. Anyone got any ideas about how to achieve this?

regards,
Roger Armstrong
Armstrong Consulting GmbH

Talip Ozturk

unread,
Jul 14, 2010, 8:11:57 AM7/14/10
to haze...@googlegroups.com
Create a local scheduled executor on each node. Register your cron job
with the local scheduled executor service and wrap your cron job
execution with one of the following two logics.

1. If you don't care to balance the execution then let the first
member in the cluster always execute the cron job.

Set<Member> members = Hazelcast.getCluster.getMembers();
if (members.next().localMember()) { // only first member will execute
the scheduled cron job.
cronjob.run();
}

2. If you want to balance the execution then let the first member fire
the execution of the cron job in the cluster.

Set<Member> members = Hazelcast.getCluster.getMembers();
if (members.next().localMember()) {
Hazelcast.getExecutorService().execute(cronjob); // execute
the cron job somewhere in the cluster.
}

AtomicNumber counter = Hazelcast.getAtomicNumber(cronJobName);
long current = counter.get();
if (counter.compareAndSet(current, current+1)) {
cronjob.run();
}

Object cronJobId = cronjob.getId();
Set set
Lock lock = Hazelcast.getLock(cronJobId);
lock.lock();
if (
cronjob.run();
lock.unlock();

twitter @oztalip

> --
> You received this message because you are subscribed to the Google Groups "Hazelcast" group.
> To post to this group, send email to haze...@googlegroups.com.
> To unsubscribe from this group, send email to hazelcast+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/hazelcast?hl=en.
>
>

Talip Ozturk

unread,
Jul 14, 2010, 9:57:50 AM7/14/10
to haze...@googlegroups.com
Please ignore the last part of my email below, starting with
AtomicNumber. I was trying other options...

twitter @oztalip

Reply all
Reply to author
Forward
0 new messages