马府强
2 years ago
5 changed files with 338 additions and 0 deletions
@ -0,0 +1,54 @@ |
|||||
|
HELP.md |
||||
|
target/ |
||||
|
!.mvn/wrapper/maven-wrapper.jar |
||||
|
!**/src/main/**/target/ |
||||
|
!**/src/test/**/target/ |
||||
|
|
||||
|
### STS ### |
||||
|
.apt_generated |
||||
|
.classpath |
||||
|
.factorypath |
||||
|
.project |
||||
|
.settings |
||||
|
.springBeans |
||||
|
.sts4-cache |
||||
|
|
||||
|
### IntelliJ IDEA ### |
||||
|
.idea |
||||
|
*.iws |
||||
|
*.iml |
||||
|
*.ipr |
||||
|
|
||||
|
### NetBeans ### |
||||
|
/nbproject/private/ |
||||
|
/nbbuild/ |
||||
|
/dist/ |
||||
|
/nbdist/ |
||||
|
/.nb-gradle/ |
||||
|
build/ |
||||
|
!**/src/main/**/build/ |
||||
|
!**/src/test/**/build/ |
||||
|
|
||||
|
### VS Code ### |
||||
|
.vscode/ |
||||
|
|
||||
|
# Log file |
||||
|
*.log |
||||
|
/logs* |
||||
|
|
||||
|
# BlueJ files |
||||
|
*.ctxt |
||||
|
|
||||
|
# Mobile Tools for Java (J2ME) |
||||
|
.mtj.tmp/ |
||||
|
|
||||
|
# Package Files # |
||||
|
*.jar |
||||
|
*.war |
||||
|
*.ear |
||||
|
*.zip |
||||
|
*.tar.gz |
||||
|
*.rar |
||||
|
*.cmd |
||||
|
|
||||
|
/data |
@ -0,0 +1,86 @@ |
|||||
|
## xxljob-autoregister-spring-boot-starter |
||||
|
|
||||
|
********************************** |
||||
|
|
||||
|
自动注册xxl-job执行器以及任务 |
||||
|
|
||||
|
## 1、打包 |
||||
|
|
||||
|
``` |
||||
|
mvn clean install |
||||
|
``` |
||||
|
|
||||
|
## 2、项目中引入 |
||||
|
|
||||
|
```xml |
||||
|
<dependency> |
||||
|
<groupId>com.cn.woniu</groupId> |
||||
|
<artifactId>xxljob-autoregister-spring-boot-starter</artifactId> |
||||
|
<version>0.0.1</version> |
||||
|
</dependency> |
||||
|
``` |
||||
|
|
||||
|
## 3、配置 |
||||
|
|
||||
|
springboot项目配置文件application.properties: |
||||
|
|
||||
|
```properties |
||||
|
server.port=8082 |
||||
|
|
||||
|
# 原生xxl-job配置 |
||||
|
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin |
||||
|
xxl.job.accessToken=default_token |
||||
|
xxl.job.executor.appname=xxl-job-executor-test |
||||
|
xxl.job.executor.address= |
||||
|
xxl.job.executor.ip=127.0.0.1 |
||||
|
xxl.job.executor.port=9999 |
||||
|
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler |
||||
|
xxl.job.executor.logretentiondays=30 |
||||
|
|
||||
|
# 新增配置项,必须项 |
||||
|
# admin用户名 |
||||
|
xxl.job.admin.username=admin |
||||
|
# admin 密码 |
||||
|
xxl.job.admin.password=123456 |
||||
|
# 执行器名称 |
||||
|
xxl.job.executor.title=Exe-Titl |
||||
|
|
||||
|
# 新增配置项,可选项 |
||||
|
# 执行器地址类型:0=自动注册、1=手动录入,默认为0 |
||||
|
xxl.job.executor.addressType=1 |
||||
|
# 在上面为1的情况下,手动录入执行器地址列表,多地址逗号分隔 |
||||
|
xxl.job.executor.addressList=http://127.0.0.1:9999 |
||||
|
``` |
||||
|
|
||||
|
`XxlJobSpringExecutor`参数配置与之前相同 |
||||
|
|
||||
|
## 4、添加注解 |
||||
|
需要自动注册的方法添加注解`@XxlRegister`,不加则不会自动注册 |
||||
|
|
||||
|
```java |
||||
|
@Service |
||||
|
public class TestService { |
||||
|
|
||||
|
@XxlJob(value = "testJob") |
||||
|
@XxlRegister(cron = "0 0 0 * * ? *", |
||||
|
author = "woniu", |
||||
|
jobDesc = "测试job") |
||||
|
public void testJob(){ |
||||
|
System.out.println("#公众号:程序员蜗牛g"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@XxlJob(value = "testJob222") |
||||
|
@XxlRegister(cron = "59 1-2 0 * * ?", |
||||
|
triggerStatus = 1) |
||||
|
public void testJob2(){ |
||||
|
System.out.println("#作者:woniu"); |
||||
|
} |
||||
|
|
||||
|
@XxlJob(value = "testJob444") |
||||
|
@XxlRegister(cron = "59 59 23 * * ?") |
||||
|
public void testJob4(){ |
||||
|
System.out.println("hello xxl job"); |
||||
|
} |
||||
|
} |
||||
|
``` |
@ -0,0 +1,88 @@ |
|||||
|
<?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> |
||||
|
|
||||
|
<groupId>com.cn.woniu</groupId> |
||||
|
<artifactId>xxljob-autoregister-spring-boot-starter</artifactId> |
||||
|
<version>0.0.1</version> |
||||
|
|
||||
|
<properties> |
||||
|
<java.version>8</java.version> |
||||
|
<xxl-job.version>2.3.0</xxl-job.version> |
||||
|
<spring-boot.version>2.6.7</spring-boot.version> |
||||
|
<hutool.version>5.4.2</hutool.version> |
||||
|
</properties> |
||||
|
|
||||
|
<dependencyManagement> |
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<!-- Import dependency management from Spring Boot (依赖管理:继承一些默认的依赖,工程需要依赖的jar包的管理,申明其他dependency的时候就不需要version) --> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-parent</artifactId> |
||||
|
<version>${spring-boot.version}</version> |
||||
|
<type>pom</type> |
||||
|
<scope>import</scope> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
</dependencyManagement> |
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-web</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-autoconfigure</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<!-- xxl-job-core --> |
||||
|
<dependency> |
||||
|
<groupId>com.xuxueli</groupId> |
||||
|
<artifactId>xxl-job-core</artifactId> |
||||
|
<version>${xxl-job.version}</version> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>cn.hutool</groupId> |
||||
|
<artifactId>hutool-all</artifactId> |
||||
|
<version>${hutool.version}</version> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
<build> |
||||
|
<plugins> |
||||
|
<plugin> |
||||
|
<groupId>org.apache.maven.plugins</groupId> |
||||
|
<artifactId>maven-compiler-plugin</artifactId> |
||||
|
<configuration> |
||||
|
<source>${java.version}</source> |
||||
|
<target>${java.version}</target> |
||||
|
<encoding>UTF-8</encoding> |
||||
|
</configuration> |
||||
|
</plugin> |
||||
|
|
||||
|
<!-- 要将源码放上去,需要加入这个插件 --> |
||||
|
<plugin> |
||||
|
<artifactId>maven-source-plugin</artifactId> |
||||
|
<version>3.0.1</version> |
||||
|
<configuration> |
||||
|
<attach>true</attach> |
||||
|
</configuration> |
||||
|
<executions> |
||||
|
<execution> |
||||
|
<phase>compile</phase> |
||||
|
<goals> |
||||
|
<goal>jar</goal> |
||||
|
</goals> |
||||
|
</execution> |
||||
|
</executions> |
||||
|
</plugin> |
||||
|
|
||||
|
</plugins> |
||||
|
</build> |
||||
|
|
||||
|
</project> |
@ -0,0 +1,108 @@ |
|||||
|
package com.xxl.job.plus.executor.core; |
||||
|
|
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.concurrent.*; |
||||
|
import java.util.concurrent.atomic.AtomicInteger; |
||||
|
|
||||
|
/** |
||||
|
* 看看这段线程池的代码 |
||||
|
* 有没有啥问题? |
||||
|
*/ |
||||
|
public class ThreadPoolExecutorTest { |
||||
|
|
||||
|
static ThreadPoolExecutor productThreadPoolExecutor = new ThreadPoolExecutor( |
||||
|
1, |
||||
|
1, |
||||
|
1, |
||||
|
TimeUnit.SECONDS, |
||||
|
new LinkedBlockingQueue<>(1), |
||||
|
new MyThreadFactory("product"), |
||||
|
new MyRejectedPolicy() |
||||
|
); |
||||
|
|
||||
|
public static void main(String[] args) throws Exception { |
||||
|
while (true) { |
||||
|
TimeUnit.SECONDS.sleep(1); |
||||
|
new Thread(() -> { |
||||
|
ArrayList<Future<Integer>> futureList = new ArrayList<>(); |
||||
|
int productNum = 5; |
||||
|
for (int i = 0; i < productNum; i++) { |
||||
|
try { |
||||
|
int finalI = i; |
||||
|
Future<Integer> future = productThreadPoolExecutor.submit(() -> { |
||||
|
System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName()); |
||||
|
return finalI * 10; |
||||
|
}); |
||||
|
futureList.add(future); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
for (Future<Integer> future: futureList) { |
||||
|
try { |
||||
|
// Integer integer = future.get();
|
||||
|
Integer integer = future.get(10,TimeUnit.SECONDS); |
||||
|
System.out.println(integer); |
||||
|
System.out.println("future.get() = " + integer); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
}).start(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
static class MyThreadFactory implements ThreadFactory { |
||||
|
private static final AtomicInteger poolNumber = new AtomicInteger(1); |
||||
|
private final ThreadGroup group; |
||||
|
private final AtomicInteger threadNumber = new AtomicInteger(1); |
||||
|
private final String namePrefix; |
||||
|
private final String threadFactoryName; |
||||
|
|
||||
|
public String getThreadFactoryName() { |
||||
|
return threadFactoryName; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public MyThreadFactory(String threadStartName) { |
||||
|
SecurityManager s = System.getSecurityManager(); |
||||
|
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); |
||||
|
namePrefix = threadStartName + "-pool-" + poolNumber.getAndIncrement() + "-thread-"; |
||||
|
threadFactoryName = threadStartName; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Thread newThread(Runnable r) { |
||||
|
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); |
||||
|
if (t.isDaemon()) { |
||||
|
t.setDaemon(false); |
||||
|
} |
||||
|
if (t.getPriority() != Thread.NORM_PRIORITY) { |
||||
|
t.setPriority(Thread.NORM_PRIORITY); |
||||
|
} |
||||
|
return t; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static class MyRejectedPolicy implements RejectedExecutionHandler{ |
||||
|
|
||||
|
@Override |
||||
|
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { |
||||
|
if (e.getThreadFactory() instanceof MyThreadFactory) { |
||||
|
MyThreadFactory myThreadFactory = (MyThreadFactory)e.getThreadFactory(); |
||||
|
if ("product".equals(myThreadFactory.getThreadFactoryName())) { |
||||
|
System.out.println("线程池有任务被拒绝了,请关注"); |
||||
|
} |
||||
|
throw new RejectedExecutionException("Task " + r.toString() + |
||||
|
" rejected from " + |
||||
|
e.toString()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
@ -0,0 +1,2 @@ |
|||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ |
||||
|
com.xxl.job.plus.executor.config.XxlJobPlusConfig |
Loading…
Reference in new issue