简体   繁体   中英

How do I implement a Scheduled Notification service?

How do I implement a Scheduled Notification service that sends alerts to the app's users at their preferred times? For the time being, they are receiving immediate notification; however, can someone advise on how I might schedule it?

在此处输入图片说明


String emailBody = "Hello,\n" + "\n" + "You have received a new notification " ;
emailService.quickEmail(String.join(" ,", emailList), emailSubject, emailBody);

1. Using ScheduledExecutorService

ScheduledExecutorService is an ExecutorService that may perform instructions after a specified delay or on a regular basis. The schedule methods construct tasks with different delays and return a task object that may be used to cancel or check their execution. The scheduleAtFixedRate() and scheduleWithFixedDelay() methods generate and run tasks that run at a fixed rate or with a fixed delay until they are cancelled.

Here's a class that has a method that creates a ScheduledExecutorService that runs the procedure every second. :


import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
class Main
{
    private static void run() {
        System.out.println("Running: " + new java.util.Date());
    }
 
    public static void main(String[] args)
    {
        ScheduledExecutorService executorService;
        executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleAtFixedRate(Main::run, 0, 1, TimeUnit.SECONDS);
    }
}

2. Using Timer Class

Threads can use the Timer class to schedule tasks for later execution in a background thread. Tasks can be scheduled for a single execution or for recurrence at regular intervals. The schedule() and scheduleAtFixedRate() functions, respectively, schedule the provided job for repeated fixed-delay and fixed-rate execution. These approaches are currently overburdened.

The following code uses the Timer class to run the method run every second (in Java 8 and above):

import java.util.Timer;
import java.util.TimerTask;
 
class Main
{
    public static void main(String[] args) throws InterruptedException
    {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("Running: " + new java.util.Date());
            }
        }, 0, 1000);
    }
}

3. Using Guava Library

Another option is to utilize Guava's AbstractScheduledService class to do some periodic activities while the application is running. Subclasses provide runOneIteration(), as well as the standard startUp() and shutDown() methods, to define one iteration of the job.

The scheduler() function must be implemented to specify the execution schedule. Typically, you'll utilise one of AbstractScheduledService's given schedules. Scheduler, either newFixedRateSchedule() or newFixedDelaySchedule(), matching to ScheduledExecutorService's common functions.

import com.google.common.util.concurrent.AbstractScheduledService;
 
import java.util.Date;
import java.util.concurrent.TimeUnit;
 
class ScheduledExecutor extends AbstractScheduledService
{
    @Override
    protected void startUp() {
        System.out.println("Job started at: " + new java.util.Date());
    }
 
    @Override
    protected void runOneIteration() throws Exception {
        System.out.println("Running: " + new java.util.Date());
    }
 
    @Override
    protected Scheduler scheduler() {
        // execute every second
        return Scheduler.newFixedRateSchedule(0, 1, TimeUnit.SECONDS);
    }
 
    @Override
    protected void shutDown() {
        System.out.println("Job terminated at: " + new java.util.Date());
    }
}
 
class Main
{
    public static void main(String[] args) throws InterruptedException
    {
        ScheduledExecutor executor = new ScheduledExecutor();
        executor.startAsync();
        Thread.sleep(10000);
        executor.stopAsync();
    }
}

4. Using Quartz Scheduler

We may also make use of the Quartz job scheduling library, which can be used in almost any Java application. Quartz may be used to design simple or complicated schedules for running tens, hundreds, or even thousands of jobs, jobs whose activities are described as standard Java components that can perform almost whatever you want them to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM