当前位置:网站首页>Distributed scheduling problem
Distributed scheduling problem
2022-07-22 16:24:00 【Leisurely summer】
1、 The scene of timing task
Timing task form : Every once in a while / A particular ⼀ Execute at all times
for example :
- Order review 、 Out of stock
- Order timeout ⾃ Dynamic cancellation 、 Payment refund
- Gift certificate synchronization 、 Generate 、 Issue the homework
- Logistics information push 、 Grab job 、 Return and exchange processing
- Data backlog monitoring 、 Log monitoring 、 Service availability probe job
- Back up data regularly
- The financial system makes regular settlement every day
- Data archiving 、 Cleaning operations
- report form 、 Offline data analysis job
2、 What is distributed scheduling
What is distributed task scheduling ? There are two meanings
1) Scheduling tasks running in a distributed cluster environment ( Same as ⼀ Deploy multiple copies of a scheduled task program , There should only be ⼀ Scheduled tasks are executing )
2) Distributed scheduling —> Distributed scheduling of scheduled tasks —> Splitting of scheduled tasks ( That is to say ⼀ individual ⼤ The job task of is divided into several small job tasks , At the same time )
3、 The difference between scheduled tasks and message queues
Common ground
- Asynchronous processing
- Such as registration 、 Order event
- Should be ⽤ decoupling
- Whether it's a scheduled task or a job MQ Both can be used as gears between two applications to realize application decoupling , This gear can transfer data , Of course, individual services do not need to consider these , When splitting a service, you often consider
- Traffic peak clipping
- On double eleven , Tasks and assignments MQ Fine ⽤ To carry traffic , The back-end system processes orders regularly according to the service capacity or from MQ Grab an order. If an order arrival event is retrieved, the processing will be triggered , For front-end users, the result is that the order has been placed successfully , The order will not be affected
Different in nature
- Timed tasks are time driven , and MQ It's event driven ;
- Time driven is irreplaceable , For example, the daily interest settlement of the financial system , Not interest ⼀ strip ( Interest arrival event ) Even if the ⼀ Next , It is often calculated in batches through scheduled tasks ;
- therefore , Timed task jobs tend to be batch processing ,MQ Tends to deal with each item ;
4、 Implementation of scheduled tasks
There are many ways to implement timed tasks . In the early days when there was no timed task framework , We will use JDK Medium Timer Mechanism and multithreading mechanism (Runnable+ Thread to sleep ) To achieve timing or interval ⼀ Execute a certain for a period of time ⼀ This program ; Then came the timed task framework , For example, famous Quartz Task scheduling framework , send ⽤ Time expression ( Include : second 、 branch 、 when 、 Japan 、 Zhou 、 year ) Configure a ⼀ When to perform a task :
Task scheduling framework Quartz review
introduce jar
<!-- Task scheduling framework quartz-->
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
Scheduled task job main scheduler
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzMan {
// Create task scheduler
private static Scheduler createScheduler() throws SchedulerException {
StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = stdSchedulerFactory.getScheduler();
return scheduler;
}
// Create a task
private static JobDetail creatJob() {
JobBuilder jobBuilder = JobBuilder.newJob(demoJob.class); // TODO Custom task class
jobBuilder.withIdentity("jobName", "myJob");
JobDetail jobDetail = jobBuilder.build();
return jobDetail;
}
/**
* Create job task time trigger ( Similar to bus ⻋ Out ⻋ schedule )
* cron The expression consists of seven positions , The blank space to separate
* 1、Seconds( second ) 0~59
* 2、Minutes( branch ) 0~59
* 3、Hours(⼩ when ) 0~23
* 4、Day of Month( God )1~31, Note that there are ⽉ Copy no ⾜31 God
* 5、Month(⽉) 0~11, perhaps JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC
* 6、Day of Week( Zhou ) 1~7,1=SUN perhaps SUN,MON,TUE,WEB,THU,FRI,SAT
* 7、Year( year )1970~2099 optional
* Example :
* 0 0 11 * * ? Daily 11 Click to trigger execution ⾏⼀ Time
* 0 30 10 1 * ? Every time ⽉1 morning 10 Half past ten triggers execution ⾏⼀ Time
*/
public static Trigger createTrigger() {
// Create time trigger
CronTrigger cronTrigger = TriggerBuilder.newTrigger()
.withIdentity("triggerName", "myTrigger")
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule("*/2 * * * * ?")).build();
return cronTrigger;
}
public static void main(String[] args) throws SchedulerException {
// Create task scheduler
Scheduler scheduler = QuartzMan.createScheduler();
// Create a task
JobDetail job = QuartzMan.creatJob();
// Create job task time trigger
Trigger trigger = QuartzMan.createTrigger();
// Use the task scheduler to execute our tasks according to the time trigger
scheduler.scheduleJob(job, trigger);
scheduler.start();
}
}
Define a job, Need to implement Job Interface
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class demoJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println(" The keyboard is broken , The monthly salary is over ten thousand ");
}
}
above , Is to review the task scheduling framework Quartz The general usage of , Then use in a distributed architecture environment Quartz Can't better meet our needs , We can use professional distributed scheduling framework , Here we recommend Elastic-job.
边栏推荐
- 进程和线程面试问题
- [solution] solve typeerror: FC () got an unexpected keyword argument 'is when paddlepaddle runs reinforcement learning code_ Test 'error
- C# ftp检测目录是否存在和创建文件夹
- 字符编码问题
- Thinking about the transformation between string and char[]
- Lecture 7 pipeline, environment variables and common commands
- Preloading and lazy loading of DOM
- 操作教程:大华摄像头通过GB28181协议注册EasyCVR平台的详细配置
- St table (jump table)
- 计算机网络传输层面试题
猜你喜欢
SOC key control LED
How far can TTL, RS232 and 485 transmit?
NB-IOT的基礎知識
Basic knowledge of nb-iot
EasyCVR平台设备分组新增以及编辑操作异常的问题修复
一个15年ABAP老兵的建议:了解这些基础知识,对ABAP开发有百利而无一害
Elephant Swap的LaaS方案迅速崛起,构建全新DeFi2.0协议
Soc之按键控制LED
Command line program test automation
The principle of distributed transaction is simple, and it is full of holes
随机推荐
Glide 源码解析
C# 上传图片至共享文件夹
第七讲 管道、环境变量与常用命令
Docker数据管理案例——MySQL数据持久保存
MySQL starts the global log to locate and troubleshoot slow SQL
【解决】npm ERR! code E401
Scala variables and data types (2)
Is there a 35 year old crisis for hardware engineers?
Matlab simulation of BER performance of BCH coding and decoding
Lesson 3 shell syntax
About the recent online treatment of myopia with low concentration atropine
C language pthread_ Join() function
In what fields can nb-iot be applied
Android interview: 2022 please keep this experience of Netease Android development and Tiktok e-commerce Android engineers
shell语法个人运用中问题小结
Session共享问题
盒马两大供应链中心启用 多业态商品创新研发“有后台”
网络层面试题
Preloading and lazy loading of DOM
正畸学经典书籍