Giriş
Şu satırı dahil ederiz
import com.hazelcast.scheduledexecutor.IScheduledExecutorService;
1. XML için IScheduledExecutorService Ayarları yazısına bakabilirsiniz.
2. Açıklaması şöyle. Yani cron tarzı yapıları desteklemez
IScheduledExecutorService is not a full cron scheduler
3. retry ve failure recovery için herhangi bir şey sağlamaz.
schedule metodu
İmzası şöyle. Belirtilen delay değerinden sonra tek seferlik (one shot) bir işi rastgele bir cluster üyesinde çalıştırır.
<V> IScheduledFuture<V> schedule(Runnable command, long delay, TimeUnit unit); <V> IScheduledFuture<V> schedule(Callable<V> command, long delay, TimeUnit unit);
Örnek
Şöyle yaparız
HazelcastInstance instance = ...; IScheduledExecutorService scheduler = instance .getScheduledExecutorService("distributed-scheduler"); scheduler.schedule(new MyRunnableTask(), 10, TimeUnit.SECONDS);
Örnek
Şöyle yaparız
@Scheduled(fixedDelay = 10000, initialDelay = 60000) void startJob() { String leaderAddress = getOldestMember().getSocketAddress().toString(); String currentAddress = instance.getCluster().getLocalMember().getSocketAddress() .toString(); //Run the task only on leader node if (currentAddress.equals(leaderAddress)) { System.out.println("I am leader, use me to poll database, distribute task etc"); IScheduledExecutorService scheduler = instance .getScheduledExecutorService("distributed-scheduler"); scheduler.schedule(new MyScheduledTask(), 10, TimeUnit.SECONDS); } } private Member getOldestMember() { Cluster cluster = instance.getCluster(); Member oldestMember = null; for (Member member : cluster.getMembers()) { if (oldestMember == null || member.getUuid().compareTo(oldestMember.getUuid()) < 0) { oldestMember = member; } } return oldestMember; } static class MySchedulerJob implements Serializable, Runnable { ... }
scheduleAtFixedRate metodu
İmzası şöyle
<V> IScheduledFuture<V> scheduleAtFixedRate(Runnable command,long initialDelay, long period,TimeUnit unit);
The solution is to use com.hazelcast.scheduledexecutor.NamedTask or com.hazelcast.scheduledexecutor.TaskUtils#named(String, Runnable) helper method
Örnek - Çift (Duplicate) İşi Tespit Etmek
Şöyle yaparız
HazelcastInstance hz = Hazelcast.newHazelcastInstance();IScheduledExecutorService myScheduler = hz.getScheduledExecutorService("myScheduler");try {IScheduledFuture<Object> future = myScheduler.scheduleAtFixedRate(TaskUtils.named( "myTask",(RunnableEx) () -> System.out.println(System.currentTimeMillis() + " Hello World!")),0, 5, SECONDS);} catch (DuplicateTaskException e) {// expected on one of the members}
Örnek
Şöyle yaparız
try { IScheduledExecutorService es = hz.getScheduledExecutorService("default"); /* Having same task name in the cluster will restrict the multiple tasks in the cluster and execution will be single instance when 1st instance will execute below code the task will be created further instances will not be able to register same name tasks and the will have ‘DuplicateTaskException’ error */ IScheduledFuture<?> future = es.scheduleAtFixedRate( TaskUtils.named("MyTask", new MyTask()), 5, 30, TimeUnit.SECONDS); } catch (DuplicateTaskException e) { System.out.println("Already scheduled task"); }
scheduleOnMember metodu
Eğer işin nerede çalıştığı önemliyse bu belirtilebilir. Açıklaması şöyle
Tasks can be scheduled to run on a specific member, all members, or a randomly selected member of the cluster.
İmzası şöyle
<V> IScheduledFuture<V> scheduleOnMember(Runnable command, Member member, long delay, TimeUnit unit); <V> IScheduledFuture<V> scheduleOnMember(Callable<V> command, Member member, long delay, TimeUnit unit);
Hiç yorum yok:
Yorum Gönder