Step 2 : Run... First , See the effect , Learn again Step 3 : Imitation and troubleshooting Step 4 : TestQuartz Step 5 : MailJob Step 6 : What's the use of grouping ? Step 7 : Static import Step 8 : log4j.xml
So far (2018-3-23), Use 2.2.3 edition
The old rule , First download the runnable project in the upper right corner , Configure to run , After confirming availability , Then learn what steps have been taken to achieve this effect .
function TestQuartz , See the effect shown in the figure , Every 2 second , A total of 11 second
After ensuring that the runnable project can run correctly , Then strictly follow the steps in the tutorial , Imitate the code again .
The imitation process inevitably has code differences , As a result, the expected operation results cannot be obtained , At this moment, by comparison The correct answer ( Runnable project ) And your own code , To locate the problem . In this way , Learning is effective , Troubleshooting is efficient , It can obviously improve the learning speed , Across all the barriers on the way to learning . It is recommended to use diffmerge Software , Compare folders . Put your own project folder , Compare with my runnable project folder . This software is awesome , You can know which two files in the folder are wrong , And clearly marked Here is a green installation and use tutorial : diffmerge Download and use tutorials
Several concepts should be clarified first :
Trigger Trigger: When to work Mission Job: what's your job Scheduler Scheduler: Match Trigger and Job The comments in the code are very clear , Look directly at the notes to understand
package com.how2java; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; public class TestQuartz { public static void main(String[] args) throws Exception{ // Create scheduler Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); // Define a trigger Trigger trigger = newTrigger().withIdentity("trigger1", "group1") // Define the name and the group to which you belong .startNow() .withSchedule(simpleSchedule() .withIntervalInSeconds(2) // Every 2 Execute once per second .withRepeatCount(10)) // A total of 11 second ( The first execution does not ) .build(); // Define a JobDetail JobDetail job = newJob(MailJob.class) // Specify the working class MailJob .withIdentity("mailjob1", "mailgroup") // Define task names and groups .usingJobData("email", "[email protected]") // Define properties .build(); // Dispatch to join this job scheduler.scheduleJob(job, trigger); // Start scheduler.start(); // wait for 20 second , After all the previous tasks are completed , Then close the scheduler Thread.sleep(20000); scheduler.shutdown(true); } }
MailJob Realized Job Interface , Provide execute, Do specific work
package com.how2java; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class MailJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { JobDetail detail = context.getJobDetail(); String email = detail.getJobDataMap().getString("email"); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); String now = sdf.format(new Date()); System.out.printf(" Give the email address %s Sent a regular email , The current time is : %s%n" ,email, now); } }
.withIdentity("mailjob1", "mailgroup") mailgroup It means grouping . For example, a system has 3 individual job Is to back up the database , have 4 individual job It's by email , Then group them , It can be easily managed , Similar to stopping all emails at once .
import static This is called static import , It refers to importing a
Class , So you can use it directly , Instead of having to write :
JobBuilder.newJob()
log4j Put it here , Only open error The above log information , Otherwise, it looks chaotic
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="default" class="org.apache.log4j.ConsoleAppender"> <param name="target" value="System.out"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c]%n%m%n%n"/> </layout> </appender> <logger name="com.how2java"> <level value="error" /> </logger> <root> <level value="error" /> <appender-ref ref="default" /> </root> </log4j:configuration>
The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .
![]()
Q & A area
2019-08-18
The webmaster forgot to say Jar Wrapped up
1 One answer
Falling stars Jump to the problem location Answer time :2019-09-18
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency>
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2019-08-05
about log4j Configuration of
1 One answer
I will study hard JAVA of Jump to the problem location Answer time :2019-08-18
log4j This thing , You've learned this , I don't know why , It's for printing logs ,
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2019-04-03
What if you want to delay execution , I set every 10 Seconds to execute , And set the number of repetitions to 0, But it was carried out immediately
2018-07-17
not bad , Just in need
2018-04-10
Stationmaster cow force !
Please... Before asking questions land
The question has been submitted successfully , Auditing . Please
My question Check the question record at , thank you
|