I am trying to implement Quartz Scheduler in Servlet and when I try to run and see the Log then Output is coming linke this. I cant understand what is wrong in this. I am new for this topic.
Code-
package Controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzScheduler implements Job {
private static final Logger LOG = Logger.getLogger(QuartzScheduler.class);
#Override
public void execute(JobExecutionContext jec) throws JobExecutionException {
System.out.println("Executing Job");
LOG.info("Executing JOb");
}
public static void main(String args[]) throws SchedulerException, ParseException {
String startDateStr = "2018-06-23 16:00:00.0";
Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
System.out.println(startDate);
JobDetail j = JobBuilder.newJob(QuartzScheduler.class).build();
Trigger t = TriggerBuilder.newTrigger()
.withIdentity("CroneTrigger")
.startAt(startDate)
.withSchedule(CronScheduleBuilder.cronSchedule("0 30 14 * * ?").withMisfireHandlingInstructionDoNothing()).build();
Scheduler s = StdSchedulerFactory.getDefaultScheduler();
s.start();
s.scheduleJob(j, t);
}
}
Output in Log-
[2018-06-28 14:23:15,906] main org.quartz.impl.StdSchedulerFactory INFO - Using default implementation for ThreadExecutor
[2018-06-28 14:23:15,971] main org.quartz.simpl.SimpleThreadPool INFO - Job execution threads will use class loader of thread: main
[2018-06-28 14:23:16,021] main org.quartz.core.SchedulerSignalerImpl INFO - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
[2018-06-28 14:23:16,021] main org.quartz.core.QuartzScheduler INFO - Quartz Scheduler v.2.2.3 created.
[2018-06-28 14:23:16,029] main org.quartz.simpl.RAMJobStore INFO - RAMJobStore initialized.
[2018-06-28 14:23:16,030] main org.quartz.core.QuartzScheduler INFO - Scheduler meta-data: Quartz Scheduler (v2.2.3) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
[2018-06-28 14:23:16,031] main org.quartz.impl.StdSchedulerFactory INFO - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
[2018-06-28 14:23:16,031] main org.quartz.impl.StdSchedulerFactory INFO - Quartz scheduler version: 2.2.3
[2018-06-28 14:23:16,031] main org.quartz.core.QuartzScheduler INFO - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
I have defined quartz.properties in src in default package and here is that file-
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
org.quartz.jobStore.misfireThreshold: 60000
org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
I am trying to sort this problem for last few days but is unable to do so.
You can get all possible scheduling(Quartz Scheduling) from below link:
https://github.com/javabypatel/spring-boot-quartz-demo
Please try this way it will execute every minute.
String exp="0 0/1 * 1/1 * ? *";//every 1 min schedular
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
JobDetail job = JobBuilder.newJob(QuartzScheduler.class).build();
Trigger trigger = TriggerBuilder.newTrigger()
.startNow()
.withSchedule(
CronScheduleBuilder.cronSchedule(exp))
.build();
scheduler.scheduleJob(job, trigger);
Related
This is the first time i am trying to use a Quartz scheduler in Mule.
I am trying to schedule the start time of a flow based on the list of DateTimes that i took from DB.
To study the working of a Quatrz scheduler and to schedule jobs based on a 'list of times' in java, i did the following sample.
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class Main {
public static void main(String[] args) throws SchedulerException {
List<SimpleTrigger> triggerList = scheduleMyJob();
int i=0;
for(SimpleTrigger trigger: triggerList){
JobDetail jobDetail = new JobDetail();
jobDetail.setJobClass(Hellojob.class);
jobDetail.setName("MyJob"+ ++i);
Scheduler jobScheduler = new StdSchedulerFactory().getScheduler();
jobScheduler.start();
jobScheduler.scheduleJob(jobDetail, trigger);
}
}
public static List<SimpleTrigger> scheduleMyJob(){
List<SimpleTrigger> triggerList = new ArrayList<SimpleTrigger>();
SimpleTrigger sTrigger = new SimpleTrigger();
sTrigger.setStartTime(new Date(System.currentTimeMillis()+10000));
sTrigger.setName("C Trigger 1");
triggerList.add(sTrigger);
sTrigger = new SimpleTrigger();
sTrigger.setStartTime(new Date(System.currentTimeMillis()+20000));
sTrigger.setName("C Trigger 2");
triggerList.add(sTrigger);
sTrigger = new SimpleTrigger();
sTrigger.setStartTime(new Date(System.currentTimeMillis()+30000));
sTrigger.setName("C Trigger 3");
triggerList.add(sTrigger);
sTrigger = new SimpleTrigger();
sTrigger.setStartTime(new Date(System.currentTimeMillis()+40000));
sTrigger.setName("C Trigger 4");
triggerList.add(sTrigger);
return triggerList;
}
}
My helloJob.java looks like..
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class Hellojob implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println(new Date() +": Hello Quartz World!! "+arg0.getJobDetail().getFullName());
}
}
This works well, I got the following output
Sat Oct 24 15:41:47 IST 2015: Hello Quartz World!! DEFAULT.MyJob1
Sat Oct 24 15:41:57 IST 2015: Hello Quartz World!! DEFAULT.MyJob2
Sat Oct 24 15:42:07 IST 2015: Hello Quartz World!! DEFAULT.MyJob3
Sat Oct 24 15:42:17 IST 2015: Hello Quartz World!! DEFAULT.MyJob4
Now i understood the working of Quartz,but i have a hardtime relating the java code with the concept of Quartz in mule. I want to implement exactly the same thing in mule, and later i can replace the dates with those from DB. If you could guide me or show me an example, it will be of great help.
I only know about Quartz scheduler, if you got a different idea, you are always welcome...
FYI. Hellojob will be replaced by a flow with a logger in it.
[Tyring to implemant the logic in mule ended up no where.So i am not posting that code here since it may give a wrong assumtion of what my real target is!!. Java code above is a perfect example]
To implement Batch Processing in Mule you can Mule Batch Processing Module which internally uses quartz scheduler and will make your life easier. You can assign CRON expression to schedule jobs. Please refer to the below mention docs to learn how Batch jobs works in mule.
https://docs.mulesoft.com/mule-user-guide/v/3.6/batch-processing
https://docs.mulesoft.com/mule-user-guide/v/3.7/batch-streaming-and-job-execution
I am trying to execute specific work in my Servlet/JSP application in everyday at 8.00AM and 12.00 PM. The Quartz library seems to be ideal for this, so I tried using it.
I used both of the tutorials and examples in below links.
http://www.mkyong.com/java/quartz-2-scheduler-tutorial/
http://www.javacodegeeks.com/2012/07/quartz-2-scheduler-example.html
Below is an attempt
Job class
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job
{
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("Hello Quartz!");
}
}
Trigger class
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class SimpleTriggerExample {
public static void main(String[] args) throws Exception {
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("dummyJobName", "group1").build();
// Trigger the job to run on the next round minute
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5).repeatForever())
.build();
// schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}
However, both are not satisfying my need. They do not run at specific time. How can I use the Quarts to execute the job everyday 8.00AM and 12.00 PM?
Try create a schedule with Cron:
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("triggerName","triggerGroup")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 12 * * ?")).build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("triggerName","triggerGroup")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 8 * * ?")).build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("triggerName","triggerGroup")
.withSchedule(CronScheduleBuilder.cronSchedule("0 30 12 * * ?")).build(); //schedules a job for 12:30am
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("triggerName","triggerGroup")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 14 * * ?")).build(); //schedules a job for 2pm
Use this site for great information on scheduling:
http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("reportTrigger").WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(23,0)).Build();
I want to develop a quartz job which will run on a particular time specified within the job. But while I am starting the job it starts at that time not on that time which is specified within the code.
Please help.
Here is my code:
import java.util.TimeZone;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class CronScheluderForActivityMilestone {
public boolean CronScheluderForActivityMilestone() throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sche = sf.getScheduler();
sche.start();
JobDetail jDetail = new JobDetail("SendMailOnActivityMileStone", "SendMailOnActivityMileStone", ActivityMileStoneSendMail.class);
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("mytriggerForActivityMileStone", "SendMailOnActivityMileStone")
.withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(19, 00).inTimeZone(TimeZone.getTimeZone("Asia/Calcutta")))
.forJob("SendMailOnActivityMileStone", "SendMailOnActivityMileStone")
.build();
sche.scheduleJob(jDetail, trigger);
return true;
}
}
You should change your inTimeZone configuration like this :
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger3", "group1")
.withSchedule(weeklyOnDayAndHourAndMinute(DateBuilder.WEDNESDAY, 10, 42)
.inTimeZone(TimeZone.getTimeZone("America/Los_Angeles")))
.forJob(job)
.build();
inTimeZone belongs to withSchedule()
For example, I want to write a Java program to print "Hello World" at each day 12 am, how can I use Quartz scheduler to achieve this?
Trigger trigger = TriggerUtils.makeDailyTrigger(0, 0);
trigger.setName("trigger1");
trigger.setGroup("group1");
Like this? Where should I put print "hello world" method?
You could use an expression to schedule the execution of the job. e.g.:
public static class HelloJob implements Job {
#Override
public void execute(JobExecutionContext ctx) throws JobExecutionException {
System.out.println("Hello World");
}
}
public static void main(String[] args) throws SchedulerException {
String exp = "0 0 0 1/1 * ? *";
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
JobDetail job = JobBuilder.newJob(HelloJob.class).build();
Trigger trigger = TriggerBuilder.newTrigger()
.startNow()
.withSchedule(
CronScheduleBuilder.cronSchedule(exp))
.build();
scheduler.scheduleJob(job, trigger);
}
See http://www.cronmaker.com/ for build another expression. e.g. 0 0/1 * 1/1 * ? * every minute for to see the output. See also Cron Expressions.
Download quartz Jar Put in lib folder build project
Create Class (Job) from which you want to schedule task
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class MyJob implements Job {
private Logger log = Logger.getLogger(MyJob.class);
#Override
public void execute(JobExecutionContext context) throws JobExecutionException {
log.debug("Hi....");
System.out.println("Corn Executing.....");
}
}
Create Class for schedule your task
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class JobScheduler {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("myjob").build();
Trigger trigger = TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(30).repeatForever()).build();
SchedulerFactory schFactory = new StdSchedulerFactory();
Scheduler scheduler = schFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}catch (SchedulerException e) {
e.printStackTrace();
}
}
}
You have to create your custom job by implementing Job interface and providing your implementation of execute method.In execute method you can print "hello world". Then you can schedule your job like this
scheduler.scheduleJob(job, trigger);
Refer this link for step by step details:
Quartz tutorial
you can create cron expression for this. to have quartz job you need to have following objects
Job
Task which will be associated to a Job
Finally create a trigger and associate a Job to the trigger
Triggers of two type
Simple triggers, where you can control job , you can run every min or 10 mins and so on. you can also have additional parameters
initial delay - to kick off
repeatcount - no of times the job should be executes, if -1 then job will be executed infinitely
In your case you can use cron triggers since you want to run every day at 12 am.
For more details and sample program look at this below link
http://www.mkyong.com/spring/spring-quartz-scheduler-example/
and about quartz cron expression , see the quartz documentation
http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
For the first time i stored the jobs and scheduled them using crontrigger with the below code.
package com.generalsentiment.test.quartz;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.util.Date;
import java.util.Properties;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CronTriggerExample {
public void run() throws Exception {
Logger log = LoggerFactory.getLogger(CronTriggerExample.class);
System.out.println("------- Initializing -------------------");
Xml config = new Xml("src/hibernate.cfg.xml", "hibernate-configuration");
Properties prop = new Properties();
prop.setProperty("org.quartz.scheduler.instanceName", "ALARM_SCHEDULER");
prop.setProperty("org.quartz.threadPool.class",
"org.quartz.simpl.SimpleThreadPool");
prop.setProperty("org.quartz.threadPool.threadCount", "4");
prop.setProperty("org.quartz.threadPool
.threadsInheritContextClassLoaderOfInitializingThread", "true");
prop.setProperty("org.quartz.jobStore.class",
"org.quartz.impl.jdbcjobstore.JobStoreTX");
prop.setProperty("org.quartz.jobStore.driverDelegateClass",
"org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
prop.setProperty("org.quartz.jobStore.dataSource", "tasksDataStore");
prop.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
prop.setProperty("org.quartz.jobStore.misfireThreshold", "60000");
prop.setProperty("org.quartz.jobStore.isClustered", "false");
prop.setProperty("org.quartz.dataSource.tasksDataStore.driver",
config.child("session-factory").children("property").get(1).content());
prop.setProperty("org.quartz.dataSource.tasksDataStore.URL", config.child("session-
factory").children("property").get(2).content());
prop.setProperty("org.quartz.dataSource.tasksDataStore.user", config.child("session-
factory").children("property").get(3).content());
prop.setProperty("org.quartz.dataSource.tasksDataStore.password",
config.child("session-factory").children("property").get(4).content());
prop.setProperty("org.quartz.dataSource.tasksDataStore.maxConnections", "20");
// First we must get a reference to a scheduler
SchedulerFactory sf = new StdSchedulerFactory(prop);
Scheduler sched = sf.getScheduler();
System.out.println("------- Initialization Complete --------");
System.out.println("------- Scheduling Jobs ----------------");
// jobs can be scheduled before sched.start() has been called
// job 1 will run exactly at 12:55 daily
JobDetail job = newJob(SimpleJob.class).withIdentity("job2", "group2").build();
CronTrigger trigger = newTrigger().withIdentity("trigger2", "group2")
.withSchedule(cronSchedule("00 15 15 * *
?")).build();
Date ft = sched.scheduleJob(job, trigger);
System.out.println(sched.getSchedulerName());
System.out.println(job.getKey() + " has been scheduled to run at: " + ft
+ " and repeat based on expression: "
+ trigger.getCronExpression());
System.out.println("------- Starting Scheduler ----------------");
/*
* All of the jobs have been added to the scheduler, but none of the
* jobs will run until the scheduler has been started. If you have
* multiple jobs performing multiple tasks, then its recommended to
* write it in separate classes, like SimpleJob.class writes
* organization members to file.
*/
sched.start();
System.out.println("------- Started Scheduler -----------------");
System.out.println("------- Waiting five minutes... ------------");
try {
// wait five minutes to show jobs
Thread.sleep(300L * 1000L);
// executing...
} catch (Exception e) {
}
System.out.println("------- Shutting Down ---------------------");
sched.shutdown(true);
System.out.println("------- Shutdown Complete -----------------");
SchedulerMetaData metaData = sched.getMetaData();
System.out.println("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
}
public static void main(String[] args) throws Exception {
CronTriggerExample example = new CronTriggerExample();
example.run();
}
}
And the details are stored in Tables - QRTZ_CRON_TRIGGERS, QRTZ_JOB_DETAILS & QRTZ_TRIGGERS
My doubt is How to schedule the jobs that are stored in DB. How to display the list of jobs in a jsp page & how to trigger them automatically.
Ours is a struts2 application with Hibernate3 ORM. I am trying to initialize the quartz scheduler when the application loads. But am unable to.
Date ft = sched.scheduleJob(job, trigger);
When this is called, your job would be scheduled for the next fire time. The scheduled job would stored in the appropriate DB tables.
To Display the list of jobs on a jsp, you should persist you job key as well as custom description of what your job entails to another DB table so that during retrieval you can retrieve this custom description as well as data Quartz persist into its own tables.
Triggering this jobs automatically is something Quartz handles for you. Once the crone expression is set to what is desired and your Job class implements org.quartz.Job, Quartz would run the execute() method at your desired next fire time
JobDetail job = newJob(SimpleJob.class).withIdentity("job2", "group2").build();
this means you will have a class named SimpleJob that implements org.quartz.Job. In that class execute() method need to be implemented. Job is triggered automatically at the time you specified by cron expression. That execute method is called when job is triggered.