


Implementation of timer, introduction to java timer Timer and Quartz and configuration of timer in Spring
1 The role of timer
In actual development, if the project needs to be executed regularly or a certain amount of work needs to be repeated, the timer is particularly important.
Of course, if we don’t understand the timer, we will use threads to implement it, for example:
package org.lzstone.action
public class FinanceAction extends Thread{
private Date date;
Thread.sleep((int)(Math.random()*1000));
date = new Date();
‐ ‐ through
}
}
}
Implementing the timer yourself is very complicated. If it is not implemented well and takes up too much memory, the system will be over. Therefore, timers are a good choice for handling scheduled or repeated tasks
2.java Common timers in
1) Implemented with Java.util.Timer
2) Implemented with Quartz provided by the OpenSymphony community
3. Introduction to Timer
Using Timer to develop scheduled tasks is mainly divided into two steps:
1) Create timing Task class 示 Example code:
package org.lzstone.ACTION
IMPORT JAVA.UTIL.TIMETASK
PUBLIC CLASS LZSTONETIMETASK EXTENDS TIMETASK {
Public Void Run () {
// Perseverance The timer task of the line 器}}
2) Run Scheduled tasks, there are two ways to run scheduled tasks:
2.1) The program starts directly
Sample code:
package org.lzstone.action
public class LzstoneMain{
…..
public void run(){
//Execute the timer task
// Create an instance
Timer timer = new Timer();
Parameters:
‐ ’ ’ s ‐ ‐ ‐‐‐‐‐‐‐‐ the task to be scheduled.
0- Delay time before executing the task, unit is milliseconds.
1*1000- The time interval between executing each subsequent task, in milliseconds.
timer.schedule(new LzstoneTimeTask(),0,1*1000);
}
}
2.2) Web monitoring method
Sample code:
package org.lzstone.action
public class LzstoneMain implements ServletContextListener{
private Timer timer = null;
use using using using using through using ’ ’s out of ’s ’ through using ‐ ‐ ‐nll;
}
public void contextDestroyed(ServletContextEvent event) { The last task performed by this timer.
timer.cancel();
web.xml configuration
org.lzstone.action.LzstoneMain
gt;
4. Introducing Quartz
Quartz is another open source project in the field of job scheduling by the OpenSymphony open source organization. It can be used to create simple or complex scheduled tasks. The steps to develop scheduled tasks using Quartz are similar to the Timer class
.
Using Quartz to develop scheduled tasks is mainly divided into two steps:
1) Create a scheduled task class
Sample code:
package org.lzstone.action
public class LzstoneTimeTask implements Job{
public void execute(JobExecutionContext context) throws JobExecutionException {行 // executing timer task
}}}}2) Run scheduled tasks. There are two ways to run scheduled tasks:
2.1) Start the program directly, create a task scheduler and configure the corresponding task plan
Sample code:
package org.lzstone.action
public class LzstoneMain{
private static Scheduler sched;
public static void run() throws Exception{
//Goal creation task plan
Crontrigger Trigger = New Crontrigger ("LZSTONETRIGGER", "LZSTONE", "0 0 12 * *?");
// 0 0 12 * *? Discard the daily at 12 o'clock at noon. StdSchedulerFactory().getScheduler();
sched.shutdown();
}
}
//Execute
public class Main{
…........
public void run(){
LzstoneMain.run(); ....
}
2.2) Web listening method
Sample code:
package org.lzstone.action
public class LzstoneMainListener implements ServletContextListener{
private Timer timer = null; public void contextInitialized(ServletContextEvent event){
. in.stop();
}
}
web. xml configuration
org.lzstone.action.LzstoneMainListener
5. Compare the
Timer method to implement the timer, the principle is simple, It is easy to implement. It is more convenient to perform simple tasks. The disadvantage is that the execution time cannot be determined and the dependence is relatively strong. The specified class must be inherited. The Quartz method is used to implement the timer. It is convenient and clearly specifies the startup time. The timing parameters are more flexible. It is easy to implement relatively complex scheduled tasks. The disadvantage is that you need to implement a specific interface and load its framework. Both methods have their own advantages and disadvantages. They can be used according to their characteristics in specific situations.
6. Spring scheduled tasks
Spring scheduled tasks provide support for both Timer and Quartz, and the implementation steps are basically the same
First configure Spring’s support for Timer
1.1 Create a scheduled task class
package org.lzstone.action
import java.util .TimeTask
public class LzstoneTimeTask extends TimeTask{
public void run() use using use using through using ’s through through using ’s way through through through out through through through off ’s ‐ ‐ ‐ ‐ ‐ to to. Create the TimerConfig.xml file (generally called applicationContext.xml)
< ;bean id="lzstoneTimeTask" class="org.lzstone.action.LzstoneTimeTask"/>
1.3 Startup settings in web projects
xml
listener>
Configure Spring’s support for Quartz
2.1 Create a scheduled task class
package org.lzstone.action
public class LzstoneQuartzTask{
public void execute(){
.2 Registration timing Task class, configure task plan and task scheduler
Create QuartzConfig.xml file (generally called applicationContext.xml) under the project's WEB-INF
2.3 Startup settings in web projects
;listener>
More timer implementations and java timing For articles related to the introduction of Timer and Quartz and the configuration of timers in Spring, please pay attention to the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor