In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to achieve Quartz high availability timing tasks in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Guide to the use of scheduled tasks
If you want to do scheduled tasks, have the need for high availability, or just want to get started quickly and easily, you must choose it.
The timed task module further encapsulates the Quartz framework and uses it more succinctly.
1. Introduce dependency xin.altitude.cms ucode-cms-quartz 1.5.4.12 and get started quickly.
Implement the org.quartz.Job interface; add the scheduling policy for the task using the annotated CronExp; and inject the task into the container using the annotated Component.
Start the project, and the scheduled task will be monitored and running.
@ Component@DisallowConcurrentExecution@CronExp (cron = "0Accord 5 *?") public class DemoJob implements Job {@ Override public void execute (JobExecutionContext context) {System.out.println ("Task 1:" + LocalDateTime.now ());}} 3. Trigger scheduled tasks manually
In addition to running periodically at the existing frequency, scheduled tasks also have the ability to be triggered manually through the interface.
Call the following API to manually trigger the task whose ID number is jobId.
Http://localhost:8080/cms-api/quartz/job/{jobId}
If there is a need to manually trigger a scheduled task, the task ID needs to be unique and known, so it needs to be specified manually when writing the scheduled task.
@ CronExp (id = 1, cron = "0ram 5 *?")
You can specify the task ID by annotating the id attribute of the CronExp, or use a random ID if the assignment is not displayed, which does not meet the known conditions and cannot be triggered manually.
4. Task with parameters
Although most tasks do not require injection parameters, there are still a small number of scenarios that need to inject parameters into scheduled tasks.
Public void execute (JobExecutionContext context) {/ * if you pass a parameter when calling a task, you can get * / Map dataMap = context.getMergedJobDataMap () from Map; / * for example, if you get a key-value pair from Map, generally speaking, the parameters are of the basic data type * / Object key = dataMap.get ("key"); System.out.println ("Task 2:" + LocalDateTime.now () + ":" + key);}
When writing a scheduled task, you can parse a Map from the JobExecutionContext object to complete the parameter injection.
Http://localhost:8080/cms-api/quartz/job/1?key=a
The meaning of the above http call is to manually trigger a task with a task ID of [1] and pass it a parameter with a value of [key].
5. Task concurrency
This framework does not support task concurrency, in other words, concurrency is disadvantageous to scheduled tasks, so it needs to be prohibited manually.
It should be noted that the concurrency of Quartz refers to whether the last task is not finished and whether the new task is executed when the task execution time exceeds the task scheduling cycle.
Generally speaking, it is necessary to show that concurrency is prohibited, and adding an annotation DisallowConcurrentExecution to the task class can prohibit the concurrency of tasks.
6. Persistence
If a scheduled task has high availability requirements, then the task needs to be persisted. After the scheduled task data is persisted to the database, the application is supported to open more. After the timed task persistence multi-node deployment, the failure of a single node in the cluster does not affect the execution of the timed task.
Scheduled task persistence, only need to modify the yml file configuration to achieve the goal, no need to modify the code. Generally speaking, Mysql is used as a container for persistence.
Spring: quartz: properties: org.quartz.jobStore.isClustered: true org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.dataSource: qzDS org.quartz.dataSource.qzDS.driver: com.mysql.cj.jdbc.Driver org.quartz.dataSource.qzDS.URL: jdbc:mysql://localhost:3306/quartz-demo org. Quartz.dataSource.qzDS.user: root org.quartz.dataSource.qzDS.password: 123456
In addition to modify the host, port, database name, user name, password five parameters, the other parameters can use the default value.
After configuring the database connection, use the SQL script to initialize the database
This is the end of the content of "how to achieve Quartz highly available timing tasks in Java". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.