diff --git a/threadpooldemoquestion/.gitignore b/threadpooldemoquestion/.gitignore new file mode 100644 index 0000000..8b761f3 --- /dev/null +++ b/threadpooldemoquestion/.gitignore @@ -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 \ No newline at end of file diff --git a/threadpooldemoquestion/README.md b/threadpooldemoquestion/README.md new file mode 100644 index 0000000..bc17e90 --- /dev/null +++ b/threadpooldemoquestion/README.md @@ -0,0 +1,86 @@ +## xxljob-autoregister-spring-boot-starter + +********************************** + +自动注册xxl-job执行器以及任务 + +## 1、打包 + +``` +mvn clean install +``` + +## 2、项目中引入 + +```xml + + com.cn.woniu + xxljob-autoregister-spring-boot-starter + 0.0.1 + +``` + +## 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"); + } +} +``` diff --git a/threadpooldemoquestion/pom.xml b/threadpooldemoquestion/pom.xml new file mode 100644 index 0000000..3ef2172 --- /dev/null +++ b/threadpooldemoquestion/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + + com.cn.woniu + xxljob-autoregister-spring-boot-starter + 0.0.1 + + + 8 + 2.3.0 + 2.6.7 + 5.4.2 + + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring-boot.version} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-autoconfigure + + + + + com.xuxueli + xxl-job-core + ${xxl-job.version} + + + + cn.hutool + hutool-all + ${hutool.version} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + UTF-8 + + + + + + maven-source-plugin + 3.0.1 + + true + + + + compile + + jar + + + + + + + + + \ No newline at end of file diff --git a/threadpooldemoquestion/src/main/java/com/xxl/job/plus/executor/core/ThreadPoolExecutorTest.java b/threadpooldemoquestion/src/main/java/com/xxl/job/plus/executor/core/ThreadPoolExecutorTest.java new file mode 100644 index 0000000..d441477 --- /dev/null +++ b/threadpooldemoquestion/src/main/java/com/xxl/job/plus/executor/core/ThreadPoolExecutorTest.java @@ -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> futureList = new ArrayList<>(); + int productNum = 5; + for (int i = 0; i < productNum; i++) { + try { + int finalI = i; + Future 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 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()); + } + } + } + + +} + + + diff --git a/threadpooldemoquestion/src/main/resources/META-INF/spring.factories b/threadpooldemoquestion/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..c712ad3 --- /dev/null +++ b/threadpooldemoquestion/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.xxl.job.plus.executor.config.XxlJobPlusConfig \ No newline at end of file