30 Ocak 2023 Pazartesi

JobRunr Kullanımı

Giriş
JobRunr, Quartz üzerine inşa edilmiş bir kütüphane. Açıklaması şöyle
JobRunr requires a datastore to store the jobs and its metadata, ...
Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>com.jobrunr</groupId>
  <artifactId>jobrunr-spring-boot-starter</artifactId>
  <version>5.3.3</version>
</dependency>
application.properties
Örnek
Şöyle yaparız
jobrunr.enabled=true

org.jobrunr.dashboard.enabled=true 
org.jobrunr.dashboard.port=8000
Örnek
Şöyle yaparız
org.jobrunr.background-job-server.enabled=true
org.jobrunr.dashboard.enabled=true
Monitoring jobs
Açıklaması şöyle
JobRunr provides a dashboard that allows you to monitor the status of your jobs. You can access the dashboard by navigating to http://localhost:8000/dashboard in your browser.
1. Fire And Forget Job
enqueue metodu kullanılır  Açıklaması şöyle
These jobs are executed once, and the result is not returned to the caller. This is useful for tasks that don’t require any feedback or response from the job. You can create a fire-and-forget job using the BackgroundJob.enqueue() method, which accepts a lambda expression as a parameter. 

2. Scheduled Job
schedule metodu kullanılır. Açıklaması şöyle
These jobs are executed at a specified time or interval. You can create a scheduled job by using the BackgroundJob.schedule() method, and it accepts a lambda expression, or you can inject an instance of a class.
Örnek
Şöyle yaparız
@Service
public class MyJobs {

  private final JobScheduler jobScheduler;

  @Autowired
  public MyJobs(JobScheduler jobScheduler) {
    this.jobScheduler = jobScheduler;
  }

  @Scheduled(cron = "1 1 19 * * ? *")
  public void scheduleJob() {
    jobScheduler.schedule("once at 5pm", () -> doSomething());
  }
}
3. Recurring jobs
 Açıklaması şöyle
These jobs are executed on a recurring basis. You can create a recurring job using the BackgroundJob.scheduleRecurringly() method, which accepts a cron expression or a Cron object.
4. Adding loggers and progress bars
Açıklaması şöyle
Loggers: JobRunr supports logging, allowing you to log messages from your background jobs. You can add loggers by using the JobContext.logger() method. You can easily inject JobContext it into your background jobs by adding it as a parameter to your lambda expression.
Örnek
Şöyle yaparız
BackgroundJob.enqueue(() -> myService.doWork(JobContext.Null))

// Actual implementation
public class MyService {
    public void doWork(JobContext jobContext) {
        jobContext.logger().info("Hello from JobRunr!");
    }
}
Açıklaması şöyle
Progress bars: You can report the accurate progress of your background jobs using the JobContext.progress() method. The progress bar is visible in the JobRunr dashboard, which will help users understand the status of their jobs. progressBar is available as part of the same JobContext object, so we can use it the same way as the logger.
Örnek
Şöyle yaparız
public class MyService {
  public void doWork(JobContext jobContext) {
    JobDashboardProgressBar progressBar = jobContext.progressBar(1000);
    for(int i = 0; i < 1000; i++) {
      // or use progressBar.setValue(i) to set the value directly
      progressBar.increaseByOne(); 
    }
  }
}
5. Deleting jobs
Açıklaması şöyle
Sometimes, you may want to delete the scheduled or recurring job. You can do this by using the BackgroundJob.delete() method. 
Örnek
Şöyle yaparız
JobId jobId = BackgroundJob.<EmailSender>enqueue(x -> x.doWork());
BackgroundJob.delete(jobId);


















Hiç yorum yok:

Yorum Gönder