Skip to content

Commit

Permalink
scheduler模块:提高并行度
Browse files Browse the repository at this point in the history
  • Loading branch information
guolanren committed Apr 12, 2020
1 parent d60f2ca commit 452704f
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 47 deletions.
5 changes: 5 additions & 0 deletions rocketmq/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# rocketmq

## 概述

rocketmq 概述...
52 changes: 52 additions & 0 deletions rocketmq/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>name.guolanren</groupId>
<artifactId>rocketmq</artifactId>
<version>1.0.0</version>
<name>rocketmq</name>
<description>rocketmq</description>

<properties>
<java.version>1.8</java.version>
<rocketmq.version>2.1.0</rocketmq.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>${rocketmq.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
16 changes: 16 additions & 0 deletions rocketmq/src/main/java/name/guolanren/RocketMQApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package name.guolanren;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author guolanren
*/
@SpringBootApplication
public class RocketMQApplication {

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

}
3 changes: 3 additions & 0 deletions rocketmq/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring.application.name = rockeqmq

spring.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package name.guolanren.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author guolanren
*/
@Configuration
@EnableScheduling
public class SchedulerConfiguration implements SchedulingConfigurer {

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}

@Bean(destroyMethod="shutdown")
public ExecutorService taskExecutor() {
return Executors.newScheduledThreadPool(10);
}
}
100 changes: 53 additions & 47 deletions scheduler/src/main/java/name/guolanren/scheduler/CommonScheduler.java
Original file line number Diff line number Diff line change
@@ -1,68 +1,74 @@
package name.guolanren.scheduler;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
*
*
* @author guolanren
*/
@Component
@EnableScheduling
public class CommonScheduler {

/**
* 秒(0~59) 分(0~59) 时(0~23) 日(1~31) 月(1~12) 星期(1~7)
*/
@Scheduled(cron = "0 0 * * * *")
public void printCron1() {
System.out.println("print the top of every hour of every day...");
}

@Scheduled(cron = "*/10 * * * * *")
public void printCron2() {
System.out.println("print every ten seconds...");
}

@Scheduled(cron = "0 0 8-10 * * *")
public void printCron3() {
System.out.println("print 8, 9 and 10 o'clock of every day...");
}

@Scheduled(cron = "0 0 6,19 * * *")
public void printCron4() {
System.out.println("print 6:00 AM and 7:00 PM every day...");
}

@Scheduled(cron = "0 0/30 8-10 * * *")
public void printCron5() {
System.out.println("print 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day...");
}

@Scheduled(cron = "0 0 9-17 * * MON-FRI")
public void printCron6() {
System.out.println("print on the hour nine-to-five weekdays...");
}

@Scheduled(cron = "0 0 0 25 12 ?")
public void printCron7() {
System.out.println("print every Christmas Day at midnight...");
}

@Scheduled(fixedDelay = 1 * 1000)
public void printFixedDelay() throws InterruptedException {
System.out.printf("print fixed delay 1s at %s...%n", new Date());
TimeUnit.SECONDS.sleep(5);
}
// @Scheduled(cron = "0 0 * * * *")
// public void printCron1() {
// System.out.println("print the top of every hour of every day...");
// }
//
// @Scheduled(cron = "*/10 * * * * *")
// public void printCron2() {
// System.out.println("print every ten seconds...");
// }
//
// @Scheduled(cron = "0 0 8-10 * * *")
// public void printCron3() {
// System.out.println("print 8, 9 and 10 o'clock of every day...");
// }
//
// @Scheduled(cron = "0 0 6,19 * * *")
// public void printCron4() {
// System.out.println("print 6:00 AM and 7:00 PM every day...");
// }
//
// @Scheduled(cron = "0 0/30 8-10 * * *")
// public void printCron5() {
// System.out.println("print 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day...");
// }
//
// @Scheduled(cron = "0 0 9-17 * * MON-FRI")
// public void printCron6() {
// System.out.println("print on the hour nine-to-five weekdays...");
// }
//
// @Scheduled(cron = "0 0 0 25 12 ?")
// public void printCron7() {
// System.out.println("print every Christmas Day at midnight...");
// }
//
// @Scheduled(fixedDelay = 1 * 1000)
// public void printFixedDelay() throws InterruptedException {
// System.out.printf("print fixed delay 1s at %s...%n", new Date());
// TimeUnit.SECONDS.sleep(5);
// }
//
// @Scheduled(fixedRate = 1 * 1000)
// public void printFixedRate() throws InterruptedException {
// System.out.printf("print fixed rate 1s at %s...%n", new Date());
// TimeUnit.SECONDS.sleep(5);
// }

@Scheduled(fixedRate = 1 * 1000)
public void printFixedRate() throws InterruptedException {
System.out.printf("print fixed rate 1s at %s...%n", new Date());
TimeUnit.SECONDS.sleep(5);
@Schedules({
@Scheduled(fixedDelay = 1 * 5000),
@Scheduled(cron = "*/10 * * * * *")
})
public void printSchedules() {
System.out.printf("print schedules at %s...%n", new Date());
}
}

0 comments on commit 452704f

Please sign in to comment.