Skip to content

Commit

Permalink
🙈 完善schedule方面的知识点
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyd-c committed Nov 8, 2018
1 parent 53d4e6c commit eefbcd9
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 227 deletions.
43 changes: 0 additions & 43 deletions springboot-schedule/README.md

This file was deleted.

28 changes: 19 additions & 9 deletions springboot-schedule/pom.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
<?xml version="1.0"?>
<project
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-schedule</artifactId>
<name>springboot-schedule</name>
<url>http://maven.apache.org</url>

<parent>
<groupId>me.zhyd.springboot</groupId>
<artifactId>springboot-learning</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springboot-schedule</artifactId>
<name>springboot-schedule</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.37</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,28 +1,77 @@
package me.zhyd.springboot.schedule;

import me.zhyd.springboot.schedule.task.DynamicScheduledConfig;
import com.alibaba.fastjson.JSONArray;
import me.zhyd.springboot.schedule.task.DynamicScheduledConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;

@SpringBootApplication
@RestController
@Controller
@EnableScheduling
public class ScheduleApplaction {

@Autowired
DynamicScheduledConfig dynamicScheduledConfig;
@Autowired
DynamicScheduledConfigurer dynamicScheduledConfigurer;

public static void main(String[] args) {
SpringApplication.run(ScheduleApplaction.class, args);
}

@RequestMapping("/")
public String index() {
return "index";
}

/**
* 修改动态定时任务的cron值
*/
@RequestMapping("/updateTask")
@ResponseBody
public void updateTask(String cron) {
dynamicScheduledConfigurer.setCron(cron);
}

// 修改动态定时任务的cron值
@RequestMapping("/updateTask")
public Object updateTask() {
dynamicScheduledConfig.setCron("0/2 * * * * ?");
return "success";
}
/**
* 预解析5此该cron将要执行的时间节点
*
* @param cron 带解析的cron
* @return
* @throws IOException
*/
@RequestMapping("/parseCron")
@ResponseBody
public List<String> parseCron(String cron) throws IOException {
String urlNameString = "http://cron.qqe2.com/CalcRunTime.ashx?CronExpression=" + URLEncoder.encode(cron, "UTF-8");
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
connection.connect();

public static void main(String[] args) throws Exception {
SpringApplication.run(ScheduleApplaction.class, args);
}
StringBuilder result = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return JSONArray.parseArray(result.toString(), String.class);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package me.zhyd.springboot.schedule.task;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
* 普通定时任务
*/
@Component
public class AppSchedulingConfigurer implements ApplicationListener<ContextRefreshedEvent> {

private AtomicInteger integer = new AtomicInteger(0);

/**
* 在11月7号晚上22点的7分到8分之间每隔半分钟(30秒)执行一次任务
*
* @author zhangyd
*/
@Scheduled(cron = "0/30 7-8 22 7 11 ? ")
public void doJobByCron() throws InterruptedException {
int index = integer.incrementAndGet();
System.out.println(String.format("[%s] %s doJobByCron start @ %s", index, Thread.currentThread(), LocalTime.now()));
// 这儿随机睡几秒,方便查看执行效果
TimeUnit.SECONDS.sleep(new Random().nextInt(5));
System.out.println(String.format("[%s] %s doJobByCron end @ %s", index, Thread.currentThread(), LocalTime.now()));
}

/**
* 上次任务执行完的3秒后再次执行
*
* @author zhangyd
*/
@Scheduled(fixedDelay = 3000)
public void doJobByFixedDelay() throws InterruptedException {
int index = integer.incrementAndGet();
System.out.println(String.format("[%s] %s doJobByFixedDelay start @ %s", index, Thread.currentThread(), LocalTime.now()));
// 这儿随机睡几秒,方便查看执行效果
TimeUnit.SECONDS.sleep(new Random().nextInt(10));
System.out.println(String.format("[%s] %s doJobByFixedDelay end @ %s", index, Thread.currentThread(), LocalTime.now()));
}

/**
* 固定每3秒执行一次
*
* @author zhangyd
*/
@Scheduled(fixedRate = 3000)
public void doJobByFixedRate() throws InterruptedException {
int index = integer.incrementAndGet();
System.out.println(String.format("[%s] %s doJobByFixedRate start @ %s", index, Thread.currentThread(), LocalTime.now()));
// 这儿随机睡几秒,方便查看执行效果
TimeUnit.SECONDS.sleep(new Random().nextInt(10));
System.out.println(String.format("[%s] %s doJobByFixedRate end @ %s", index, Thread.currentThread(), LocalTime.now()));
}

/**
* 第一次延迟5秒后执行,之后按fixedRate的规则每3秒执行一次
*
* @author zhangyd
*/
@Scheduled(initialDelay = 5000, fixedRate = 3000)
public void doJobByInitialDelay() throws InterruptedException {
int index = integer.incrementAndGet();
System.out.println(String.format("[%s] %s doJobByInitialDelay start @ %s", index, Thread.currentThread(), LocalTime.now()));
// 这儿随机睡几秒,方便查看执行效果
TimeUnit.SECONDS.sleep(new Random().nextInt(5));
System.out.println(String.format("[%s] %s doJobByInitialDelay end @ %s", index, Thread.currentThread(), LocalTime.now()));
}

/**
* 监听程序启动-Spring容器初始化完成
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent applicationReadyEvent) {
System.out.println(String.format("springboot上下文context准备完毕时. %s start @ %s", Thread.currentThread(), LocalTime.now()));
}

}
Loading

0 comments on commit eefbcd9

Please sign in to comment.