Skip to content

Instantly share code, notes, and snippets.

@denistsyplakov
Last active March 5, 2020 09:14
Show Gist options
  • Save denistsyplakov/fd4b3479166103b78947fdbbd7dbbd3a to your computer and use it in GitHub Desktop.
Save denistsyplakov/fd4b3479166103b78947fdbbd7dbbd3a to your computer and use it in GitHub Desktop.
Hazelcast scheduling
Mar 05, 2020 12:04:32 PM com.dataart.hzc.scheduling.test.HzcSchedulingTestApplication$SomeTask run
INFO: ---- Executing task on instance: _hzInstance_1_dev-hz-proto runs: 25
Mar 05, 2020 12:04:32 PM com.dataart.hzc.scheduling.test.HzcSchedulingTestApplication$SomeTask run
INFO: ---- Executing task on instance: _hzInstance_2_dev-hz-proto runs: 24
Mar 05, 2020 12:04:32 PM com.dataart.hzc.scheduling.test.HzcSchedulingTestApplication$SomeTask run
INFO: ---- Executing task on instance: _hzInstance_1_dev-hz-proto runs: 23
Mar 05, 2020 12:04:32 PM com.dataart.hzc.scheduling.test.HzcSchedulingTestApplication$SomeTask run
INFO: ---- Executing task on instance: _hzInstance_2_dev-hz-proto runs: 22
Mar 05, 2020 12:04:32 PM com.dataart.hzc.scheduling.test.HzcSchedulingTestApplication$SomeTask run
INFO: ---- Executing task on instance: _hzInstance_3_dev-hz-proto runs: 21
Mar 05, 2020 12:04:32 PM com.dataart.hzc.scheduling.test.HzcSchedulingTestApplication$SomeTask run
INFO: ---- Executing task on instance: _hzInstance_1_dev-hz-proto runs: 20
Mar 05, 2020 12:04:32 PM com.dataart.hzc.scheduling.test.HzcSchedulingTestApplication$SomeTask run
INFO: ---- Executing task on instance: _hzInstance_2_dev-hz-proto runs: 19
Mar 05, 2020 12:04:32 PM com.dataart.hzc.scheduling.test.HzcSchedulingTestApplication$SomeTask run
INFO: ---- Executing task on instance: _hzInstance_3_dev-hz-proto runs: 18
<?xml version="1.0" encoding="UTF-8"?>
<!--suppress XmlDefaultAttributeValue -->
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-config-3.12.xsd">
<group>
<name>dev-hz-proto</name>
</group>
<network>
<join>
<multicast enabled="true"/>
</join>
</network>
<scheduled-executor-service name="testExec">
<pool-size>2</pool-size>
<durability>2</durability>
<capacity>1000</capacity>
<quorum-ref>testQuorum</quorum-ref>
</scheduled-executor-service>
</hazelcast>
package com.dataart.hzc.scheduling.test;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.HazelcastInstanceAware;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
public class HzcSchedulingTestApplication {
static Logger log = Logger.getLogger(HzcSchedulingTestApplication.class.getName());
public static class SomeTask
implements Runnable, Serializable, HazelcastInstanceAware {
private transient HazelcastInstance hazelcastInstance;
public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
this.hazelcastInstance = hazelcastInstance;
}
@Override
public void run() {
Integer runs = (Integer) hazelcastInstance.getMap("map").get("runs");
log.info("---- Executing task on instance: " + hazelcastInstance.getName() + " runs: " + runs );
hazelcastInstance.getMap("map").replace("runs",runs,runs-1);
if (runs > 0 ){
hazelcastInstance.getScheduledExecutorService("testExec").schedule(new SomeTask(), 50, TimeUnit.MILLISECONDS);
}
}
}
public static void main(String[] args) throws InterruptedException {
HazelcastInstance[] instances = new HazelcastInstance[3];
instances[0] = Hazelcast.newHazelcastInstance();
instances[1] = Hazelcast.newHazelcastInstance();
instances[2] = Hazelcast.newHazelcastInstance();
log.info(" -----------\n\n\n READY \n\n\n-----------");
instances[1].getMap("map").put("runs",25);
instances[0].getScheduledExecutorService("testExec").schedule(new SomeTask(), 50, TimeUnit.MILLISECONDS);
log.info(" ----------- \n\n\n schedule done \n\n\n---------------------");
Thread.sleep(5000);
for (HazelcastInstance hzi : instances) {
hzi.shutdown();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment