In my java web application, I want to schedule a task.
I have searched the web alot and couldn't find a suitable scheduler for my case. In the application I have different types of users. For a specific user type, I want to schedule a task.
By the time a critical action is taken by a user:
I want to send an email to that specific user and after 15 minutes
I want to send another email and after 30 minutes
I want to send another email and shut down the scheduler.
I know when the users take critical actions and how to send email but I don't have much experience about scheduling.
Can anyone help me for the case?
Why don't you use a ScheduledExecutor?
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html
It has a method schedule which allows you to schedule whatever task you want (you pass a runnable). So basically, for each user you schedule a task of sending an e-mail and scheduling another task.
import static java.util.concurrent.TimeUnit.*;
class Task implements Runnable {
private final User user;
private final int rep;
private final ScheduledExecutorService scheduler;
public Task(User user, int rep, ScheduledExecutorService scheduler) {
this.user = user;
this.rep = rep;
this.scheduler = scheduler;
}
public void run() {
// send an e-mail to user
if (rep > 0)
scheduler.schedule(new Task(user, rep - 1, scheduler), 15, MINUTES);
}
}
class Example {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void sendEmails() {
// foreach user
scheduler.submit(new Task(user, 3, scheduler));
}
}
You might want to use a scheduled thread pool with more than one thread.
Use Quartz Scheduler to schedule a task
Steps Required -
1) Quartz job
public class HelloJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Hello Quartz!");
}
}
2) Creating a trigger - CronTrigger – Run every 30 seconds
CronTrigger trigger = new CronTrigger();
trigger.setName("dummyTriggerName");
trigger.setCronExpression("0/30 * * * * ?");
3) Creating a scheduler
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
Here's a tutorial on how to use Java Timers:
http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html
You can create multiple Timer tasks in sequence to fufill your objective.
Example
Code Quote:
import java.util.Timer;
import java.util.TimerTask;
/**
* Simple demo that uses java.util.Timer to schedule a task
* to execute once 5 seconds have passed.
*/
public class Reminder {
Timer timer;
public Reminder(int seconds) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);
}
class RemindTask extends TimerTask {
public void run() {
System.out.format("Time's up!%n");
timer.cancel(); //Terminate the timer thread
}
}
public static void main(String args[]) {
new Reminder(5);
System.out.format("Task scheduled.%n");
}
}
You can also use JobRunr, an easy to use and open-source Java Scheduler.
To schedule a Job after 15 minutes using JobRunr, you would use the following code:
JobId firstEmailJobId = BackgroundJob.enqueue(() -> yourService.sendFirstEmail());
JobId secondEmailJobId = BackgroundJob.schedule(Instant.now().plus(15, minutes), () -> yourService.sendSecondEmail());
You can then repeat these steps for the other 2 emails. If you want to cancel sending these emails (e.g. when the user took action), you can easily delete these jobs again by means of the JobId (of course only if the job has not executed yet)
JobRunr also comes with automatic retries (e.g. your mailserver or sendgrid is down) and an embedded dashboard that allows you to follow-up on how your jobs are doing.
I would recommend you to take a look at the quartz scheduling API http://quartz-scheduler.org/
I have used it in several projects so far and it is really easy to setup and configure new jobs in. It supports cron based triggers or simpletriggers so you can either calculate the times for the scheduled events in your java code or you can simply pass it a cron string.
Another advantage is that its really easy to configure with spring.
Related
In the spring boot app, I have to call a method for a given frequency.This frequency will be read from the database. Frequent could be seconds or minutes or hours or days, etc. How do I implement this schedule for spring boot?
To give more other alternative solutions beside #Scheduled annotation.
You can use ScheduledExecutorService to execute task with fixed rate or fixed delay.
ScheduledExecutorService executorService = Executors
.newSingleThreadScheduledExecutor();
Then you can use below function which depend on your need:
scheduleAtFixedRate
scheduleWithFixedDelay
Example:
Future<String> resultFuture =
executorService.schedule(callableTask, 1, TimeUnit.SECONDS);
Future<String> resultFuture = service
.scheduleAtFixedRate(runnableTask, 100, 450, TimeUnit.MILLISECONDS);
service.scheduleWithFixedDelay(task, 100, 150, TimeUnit.MILLISECONDS);
Refer:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html
https://www.baeldung.com/java-executor-service-tutorial
PS: i highly recommend quartz framework also, it can schedule task with many different policies.
for this you can use a cron with this annoation #Scheduled
Spring will schedule the annotated method to run at 10:15 AM on the 15th day of every month
#Scheduled(cron = "0 15 10 15 * ?")
public void scheduleTaskUsingCronExpression() {
long now = System.currentTimeMillis() / 1000;
System.out.println(
"schedule tasks using cron jobs - " + now);
}
see this doc https://spring.io/guides/gs/scheduling-tasks/
and this https://spring.io/guides/gs/scheduling-tasks/
In Order to get the cron exp from the db :
see this How to change Spring's #Scheduled fixedDelay at runtime?
You may use Task Exection and Sceduling API of Spring.
Your task to run in a scheduled mode
public class MyTask implements Runnable {
public void run(){
System.out.println("MyTask is running");
}
}
A service to be used for dynamically creation of sceduled jobs.
Inspired by this so answer.
You may add it in your library.
#Component
public class DbScheduledJobs {
private final TaskScheduler executor;
#Autowired
public DbScheduledJobs(TaskScheduler taskExecutor) {
this.executor = taskExecutor;
}
public void schedule(final Runnable task, String cronLike) {
executor.schedule(task, new CronTrigger(cronLike));
}
public void schedule(final Runnable task, LocalDateTime runOnceAt) {
executor.schedule(task, Date.from(runAt.atZone(ZoneId.systemDefault()).toInstant()));
}
public void schedulingExamples(final Runnable task) {
// Schedule a task to run once at the given date (here in 1minute)
executor.schedule(task, Date.from(LocalDateTime.now().plusMinutes(1)
.atZone(ZoneId.systemDefault()).toInstant()));
// Schedule a task that will run as soon as possible and every 1000ms
executor.scheduleAtFixedRate(task, 1000);
// Schedule a task that will first run at the given date and every 1000ms
executor.scheduleAtFixedRate(task, Date.from(LocalDateTime.now().plusMinutes(1)
.atZone(ZoneId.systemDefault()).toInstant()), 1000);
// Schedule a task that will run as soon as possible and every 1000ms after the previous completion
executor.scheduleWithFixedDelay(task, 1000);
// Schedule a task that will run as soon as possible and every 1000ms after the previous completion
executor.scheduleWithFixedDelay(task, Date.from(LocalDateTime.now().plusMinutes(1)
.atZone(ZoneId.systemDefault()).toInstant()), 1000);
// Schedule a task with the given cron expression
executor.schedule(task, new CronTrigger("*/5 * * * * MON-FRI"));
}
}
Your actual service, which reads from Db.
#Service
public class DbService {
private final DbScheduledJobs dbScheduledJobs;
#Autowired
public DbService(DbScheduledJobs dbScheduledJobs) {
this.dbScheduledJobs = dbScheduledJobs;
}
public void example() {
// Read your cron like schedule from db
String cronLikeScedule = "fetched from db"; // Ex. "*/5 * * * * MON-FRI"
// Choose your Runnable instance - either statically or using Reflection.
Runnable myRunnable = new MyTask();
// Add this task to run on a schedule basis dynamically.
this.dbScheduledJobs.schedule(myRunnable, cronLikeScedule);
}
}
Hope that it helps, this solution is not tested.
I'm trying to solve a problem similar to downloading new mails from mail servers by mail client. I have a task, which is performed regularly (next iteration is 10 minutes after the last one ends for example) but there is also a possibility to run the task manually.
When I am trying to run the job manually and job is not running at this moment (is appointed for later), I cancel the appointment and schedule it for now. When the job is already running I do not cancel it, but wait until it finishes and run it again. But only one task can wait this way.
My problem is that I do not know how to synchronize the jobs to make it thread safe and make sure that job never runs twice at the same time.
To make it more clear. The main problem is that simple asking if the job is running and deciding based on what I get is not enough, because between the question and action the situation can change. It is a short span but the probability is not zero. And the same problem is with deciding if I should run the job again at the end of his run. If my decision is based on the value of some variable or some other if clause, then between testing its value and performing some action, some other thread can change it and I can end up with two scheduled jobs or none at all.
Have you considered using a DelayQueue?
To make your job run now you just need to persuade it to return 0 form getDelay(TimeUnit unit).
The main way to do that check you are telling about is to check, to lock and after that to repeat the same check:
public class OneQueuedThread extends Thread {
static int numberRunning =0;
public void run() {
if (numberRunning<2) {
synchronized (OneQueuedThread.class){
if (numberRunning<2) {
numberRunning++;
// ---------------your process runs here
numberRunning--;
}
}
}
}
}
So, only one attempt to run the thread while it is already running will wait until the end of the running thread and start after it.
As for scheduling, let's use the TimerTask features:
public class ScheduledTask extends TimerTask {
ScheduledTask instance;
/**
* this constructor is to be used for manual launching
*/
public void ScheduledTask(){
if (instance == null){
instance = this;
} else {
instance.cancel();
}
instance.run();
}
/**
* This constructor is to be used for scheduled launching
* #param deltaTime
*/
public ScheduledTask(long deltaTime){
instance = this;
Timer timer = new Timer();
timer.schedule(instance, deltaTime);
}
public void run() {
OneQueuedThread currentTread;
currentTread = new OneQueuedThread();
currentTread.start();
}
}
I am running a scheduled task in the web application using the java SingleThreadScheduledExecutor
The problem I have is - How do I identify whether the scheduler is still running and has not crashed?
Is there a better way of doing it rather than having another scheduler to check this particular scheduler
there is actually a way to check
public class TaskSchedulerService{
private final ThreadPoolTaskScheduler taskScheduler; //initialize it here or in constructor
private Map<String,ScheduledFuture<?>> scheduleMap = new ConcurrentHashMap<>();
public TaskSchedulerServiceImpl() {
this.schedulerName = schedulerName;
taskScheduler.initialize();
}
public boolean isScheduled(String taskId) {
final ScheduledFuture<?> exits = scheduledTasks.get(taskId);
return exits != null && exits.isDone();
}
public ScheduledFuture<?> schedule(String taskId, Runnable task, Date date) {
ScheduledFuture<?> scheduled = scheduleMap.get(taskId);
if (scheduled==null ) {
ScheduledFuture<?> future = taskScheduler.schedule(task, date);
scheduleMap.put(taskId, future);
return future;
} else {
// log it is already scheduled
return scheduled;
}
}
i know it is too late but i hope others can get benefit from it
The logic behind the implementation is whenever you are trying to schedule a task, you will have to add it to the map with the taskId as well, in this case it is better to find any task if exists in MAP or if needed remove it as well as checking if that task is done or not
The answer depends on what your scheduler does really. For instance, you can produce a file or update a field in a db or such thing that can be checked and the time interval (from now to last update) can be calculated. In your case, if the time interval of file creation or db updated is more than half an hour this means the job did stop. But notice that scheduled jobs are meant to last forever like love.
I am new to web development I am making some web services using java servlets, and I am stuck in so many How to do it? stuff :(
I am developing an application in which I need to reset the mysql table column values back to default values at some time let's say we need to reset the counter # every Sunday 10:00 pm.
is there any thing like alarm manager in Java or mysql that can run all the time in background and trigger # specific time.
Thanks,
Java has a good interface ScheduledExecutorService.
You can try this code
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
//every day to execute
long everyDayDelay = 24*60*60*1000;
//first time to execute, it can be your special date
//for example 5 seconds after launch
long timeToExecute = System.currentTimeMillis()+5*1000;
service.scheduleWithFixedDelay(new Task(), getTimeToLaunch(timeToExecute), everyDayDelay, TimeUnit.MILLISECONDS);
where
//return difference between now and timeToExecute
public static long getTimeToLaunch(long timeToExecute){
long current = System.currentTimeMillis();
return timeToExecute - current;
}
class Task implements Runnable{
#Override
public void run() {
System.out.println("Run task!");
}
}
UPDATE: Class to execute your sql-tasks
public class SqlExecutionService1 {
public static final long everyDayDelay = 24*60*60*1000;
public SqlExecutionService1(){
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
//every day to execute
//first time to execute
//for example 5 seconds after launch
long timeToExecute = System.currentTimeMillis()+5*1000;
service.scheduleWithFixedDelay(new SqlTask1(), getTimeToLaunch(timeToExecute), everyDayDelay, TimeUnit.MILLISECONDS);
}
private long getTimeToLaunch(long timeToExecute){
long current = System.currentTimeMillis();
return timeToExecute - current;
}
}
class SqlTask1 implements Runnable{
#Override
public void run() {
//your sql tasks
}
}
To create this class, when your app server starts - use method init() in one of your main servlets.
Example -
public class MainInitServlet extends HttpServlet {
public void init() {
new SqlExecutionService1();
}
}
The Data of a Database should be the basis for calculation, not the result of calculations. In example Bills wont ever be successfully revised.
I guess this fact will ever be a clash between Programmers and Architects.
you might also use the event scheduling mechanism of mysql
this depends on the version of mysql and whether it is enabled or not
is there any thing like alarm manager in Java or mysql that can run all the time in background and trigger # specific time.
look at a API called Quartz , where your program can schedule Jobs and it will run it at that time.
use method execute(JobExecutionContext jobExecution) use to fire trigger.
EG:
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve scheduler
Scheduler scheduler = null;
try {
scheduler = schedulerFactory.getScheduler();
}
catch (SchedulerException e) {
e.printStackTrace();
}
//this is a job
JobDetail job = new JobDetail("jobDetail", "jobDetailGroup", ImplementedJob.class);
SimpleTrigger trigger = new SimpleTrigger("Trigger Name","defaultGroup", DATE);
// schedule
scheduler.scheduleJob(job, trigger);
// start the scheduler
scheduler.start();
I have a servlet that starts on start of tomcat. I need a functionality in that servlet that triggers the event after a regular interval of time i.e 1 hour and
runs in the back ground? Here is my code :-
in main method
MyTask contentTask = new MyTask();
long interval = 60 * 60 * 1000;
Timer timer = new Timer();
timer.schedule(contentTask, new Date(), interval);//line 1
System.out.println("line2");//line2
MyTask
public class MyTask extends TimerTask {
#Override
public void run() {
System.out.println("Inside my task");
}
}
i am expecting as soon control comes to line 2 , run method gets executed and then it keeps on execting the task after ever 60 minutes like background thread does. But
control does not come to run method after line 2. I am not sure what i am missing here and why run method is not getting executed?
Edit:- I think problem is with interval value if i make it one minute i.e 1 * 60 * 1000; contol comes to run method . Looks like even the first time task will be executed after specified time interval i.e 60 minutes but i want to execute the task immediately as soon as it executes the line 1 and then repeat it after 60 minutes How to go for this?
May be you should start as daemon. new Timer(true)
It seems to me that you are looking for a ServletContextListener. You can execute code at the deployment before everything else. In this code you use Executors instead of a "Vanilla" Thread, and schedule it.
#WebListener
public class GlobalContext implements ServletContextListener {
private ServletContext app;
private ScheduledExecutorService scheduler;
//What to do when app is deployed
public void contextInitialized(ServletContextEvent event) {
//Init Context
app = event.getServletContext();
//In my case I store my data in the servlet context with setAttribute("myKey", myObject)
//and retrieve it with getAttribute("myKey") in any Servlet
// Scheduler
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new AutomateRefresh(), 0, 60, TimeUnit.MINUTES);
}
//Do not forget to destroyed your thread when app is destroyed
public void contextDestroyed(ServletContextEvent event) {
scheduler.shutdown();
}
//Your function
public class AutomateRefresh implements Runnable {
public void run() {
//Do Something
}
}
}
FYI, Executors is part of Java 5 and further.