8 Haziran 2023 Perşembe

SpringScheduling Shedlock @SchedulerLock Anotasyonu

name Alanı
Bu alan veri tabanındaki name sütununda görülür.

Örnek
Şöyle yaparız
import net.javacrumbs.shedlock.core.SchedulerLock;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyScheduledJob {

  /**
    * Define your cron expression, 
    * better if it is on properties configurations
    */
  @Scheduled(cron = "0 0/5 * * * ?")
  @SchedulerLock(name = "myScheduledJobLock") // Provide a unique lock name
  public void scheduledMethod() {
    // Your job logic here
  }
}
Örnek
Şöyle yaparız
@Scheduled(cron = "0 0 * * * *")
@SchedulerLock(name = "myTask")
public void run() {
   // do something
}
Örnek
Şöyle yaparız
@Component
public class SchedulerTask {

  @Scheduled(fixedDelay = 10,timeUnit = TimeUnit.SECONDS)
  @SchedulerLock(name = "someOperation")
  public void someOperation() {
    // To assert that the lock is held (prevents misconfiguration errors)
    LockAssert.assertLocked();
    Logger.getAnonymousLogger().info("In Scheduler");
  }
}
lockAtLeastFor Alanı
Açıklaması şöyle
lockAtLeastFor attribute which specifies minimum amount of time for which the lock should be kept. Its main purpose is to prevent execution from multiple nodes in case of really short tasks and clock difference between the nodes.
Örnek
Şöyle yaparız
@Component
public class TaskScheduler {

  @Scheduled(cron = "0/15 * * * * ?")
  @SchedulerLock(name = "myTask1", lockAtLeastFor = "PT10S", lockAtMostFor = "PT15S")
  public void task1() {
  }

  @Scheduled(cron = "0/15 * * * * ?")
  @SchedulerLock(name = "myTask2", lockAtLeastFor = "PT10S", lockAtMostFor = "PT15S")
  public void task2() {
  }
}
lockAtMostFor Alanı
Açıklaması şöyle
lockAtMostFor we make sure that the lock is released even if the node dies. Please note that lockAtMostFor is just a safety net in case that the node executing the task dies, so set it to a time that is significantly larger than maximum estimated execution time. If the task takes longer than lockAtMostFor, it may be executed again and the results will be unpredictable (more processes will hold the lock).
Örnek
Şöyle yaparız
@Component
public class ScheduledTasks {
  @Scheduled(fixedRateString = "PT5M")
  @SchedulerLock(name="someScheduledTask", lockAtmostFor = "PT2M",
lockAtLeastFor = "PT1M")
  public void someScheduledTask() {
    //Task runs every 5 minutes
  }
}

Hiç yorum yok:

Yorum Gönder