Skip to content

Commit

Permalink
perfect example
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhom1314 committed Dec 14, 2023
1 parent d87df10 commit ef3ee42
Show file tree
Hide file tree
Showing 67 changed files with 3,192 additions and 2,230 deletions.
16 changes: 12 additions & 4 deletions example/example-apollo/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ spring:
profiles:
active: dev

logging:
level:
#root: DEBUG
com.alibaba.nacos.client.config.impl: WARN
management:
endpoints:
web:
base-path: /actuator
exposure:
include: '*'
httptrace:
enabled: false
configprops:
enabled: false
shutdown:
enabled: false

apollo:
bootstrap:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@

package org.dromara.dynamictp.example.config;

import org.dromara.dynamictp.core.executor.DtpExecutor;
import org.dromara.dynamictp.core.executor.OrderedDtpExecutor;
import org.dromara.dynamictp.core.support.DynamicTp;
import org.dromara.dynamictp.core.support.ThreadPoolBuilder;
import org.dromara.dynamictp.core.support.ThreadPoolCreator;
import org.dromara.dynamictp.core.executor.DtpExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static org.dromara.dynamictp.common.em.QueueTypeEnum.SYNCHRONOUS_QUEUE;
import static org.dromara.dynamictp.common.em.QueueTypeEnum.MEMORY_SAFE_LINKED_BLOCKING_QUEUE;
import static org.dromara.dynamictp.common.em.RejectedTypeEnum.ABORT_POLICY;

/**
* @author Redick01
Expand All @@ -41,56 +45,84 @@ public class ThreadPoolConfiguration {
*
* @return 线程池实例
*/
@DynamicTp("commonExecutor")
@DynamicTp("jucThreadPoolExecutor")
@Bean
public ThreadPoolExecutor commonExecutor() {
public ThreadPoolExecutor jucThreadPoolExecutor() {
return (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
}

@DynamicTp("threadPoolTaskExecutor")
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
return new ThreadPoolTaskExecutor();
}

/**
* 通过{@link ThreadPoolCreator} 快速创建一些简单配置的动态线程池
* tips: 建议直接在配置中心配置就行,不用@Bean声明
*
* @return 线程池实例
*/
@Bean
public DtpExecutor dtpExecutor1() {
return ThreadPoolCreator.createDynamicFast("dtpExecutor1");
public DtpExecutor dtpExecutor0() {
return ThreadPoolCreator.createDynamicFast("dtpExecutor0");
}

/**
* 通过{@link ThreadPoolBuilder} 设置详细参数创建动态线程池(推荐方式),
* ioIntensive,参考tomcat线程池设计,实现了处理io密集型任务的线程池,具体参数可以看代码注释
*
* tips: 建议直接在配置中心配置就行,不用@Bean声明
* @return 线程池实例
*/
@Bean
public DtpExecutor ioIntensiveExecutor() {
public ThreadPoolExecutor dtpExecutor1() {
return ThreadPoolBuilder.newBuilder()
.threadPoolName("ioIntensiveExecutor")
.corePoolSize(20)
.maximumPoolSize(50)
.queueCapacity(2048)
.eager(true)
.threadPoolName("dtpExecutor1")
.threadFactory("test-dtp-common")
.corePoolSize(10)
.maximumPoolSize(15)
.keepAliveTime(40)
.timeUnit(TimeUnit.SECONDS)
.workQueue(MEMORY_SAFE_LINKED_BLOCKING_QUEUE.getName(), 2000)
.buildDynamic();
}

/**
* 通过{@link ThreadPoolBuilder} 设置详细参数创建动态线程池(推荐方式),
* ioIntensive,参考tomcat线程池设计,实现了处理io密集型任务的线程池,具体参数可以看代码注释
* <p>
* tips: 建议直接在配置中心配置就行,不用@Bean声明
* @return 线程池实例
*/
@Bean
public ThreadPoolExecutor dtpExecutor2() {
public DtpExecutor eagerDtpExecutor() {
return ThreadPoolBuilder.newBuilder()
.threadPoolName("dtpExecutor2")
.corePoolSize(10)
.maximumPoolSize(15)
.keepAliveTime(15000)
.timeUnit(TimeUnit.MILLISECONDS)
.workQueue(SYNCHRONOUS_QUEUE.getName(), null, false, null)
.waitForTasksToCompleteOnShutdown(true)
.awaitTerminationSeconds(5)
.threadPoolName("eagerDtpExecutor")
.threadFactory("test-eager")
.corePoolSize(2)
.maximumPoolSize(4)
.queueCapacity(2000)
.eager(true)
.buildDynamic();
}

@Bean
public OrderedDtpExecutor orderedDtpExecutor() {
return (OrderedDtpExecutor) ThreadPoolBuilder.newBuilder()
.threadPoolName("orderedDtpExecutor")
.threadFactory("test-ordered")
.corePoolSize(4)
.maximumPoolSize(4)
.queueCapacity(2000)
.ordered(true)
.buildDynamic();
}

@Bean
public ScheduledExecutorService scheduledDtpExecutor() {
return ThreadPoolBuilder.newBuilder()
.threadPoolName("scheduledDtpExecutor")
.corePoolSize(2)
.threadFactory("test-scheduled")
.rejectedExecutionHandler(ABORT_POLICY.getName())
.buildScheduled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,55 @@

package org.dromara.dynamictp.example.controller;

import org.dromara.dynamictp.core.DtpRegistry;
import org.dromara.dynamictp.core.support.task.runnable.NamedRunnable;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.example.feign.DynamicTpFeign;
import org.slf4j.MDC;
import org.dromara.dynamictp.example.service.TestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
* @author Redick01
*/
@RestController
@Slf4j
@SuppressWarnings("all")
@RestController
@AllArgsConstructor
public class TestController {

@Resource
private ThreadPoolExecutor dtpExecutor1;
private final TestService testService;

@GetMapping("/dtp-consul-example/testJucTp")
public String testJuc() {
testService.testJucTp();
return "testJucTp success";
}

@GetMapping("/dtp-consul-example/testSpringTp")
public String testSpring() {
testService.testSpringTp();
return "testSpringTp success";
}

@Resource
private DynamicTpFeign dynamicTpFeign;
@GetMapping("/dtp-consul-example/testCommonDtp")
public String testCommon() {
testService.testCommonDtp();
return "testCommonDtp success";
}

@GetMapping("/index")
public String index() throws InterruptedException {
Thread.sleep((int) (Math.random() * 100));
return "index";
@GetMapping("/dtp-consul-example/testEagerDtp")
public String testEager() {
testService.testEagerDtp();
return "testEagerDtp success";
}

@GetMapping("/dtp-consul-example/test")
public String test() throws InterruptedException {
task();
return "success";
@GetMapping("/dtp-consul-example/testScheduledDtp")
public String testScheduled() {
testService.testScheduledDtp();
return "testScheduledDtp success";
}

public void task() throws InterruptedException {
Executor dtpExecutor2 = DtpRegistry.getExecutor("dtpExecutor2");
MDC.put("traceId", UUID.randomUUID().toString());
for (int i = 0; i < 100; i++) {
Thread.sleep(100);
dtpExecutor1.execute(() -> {
log.info("i am dynamic-tp-test-1 task");
});
dtpExecutor2.execute(NamedRunnable.of(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("i am dynamic-tp-test-2 task");
}, "task-" + i));
}
@GetMapping("/dtp-consul-example/testOrderedDtp")
public String testOrdered() {
testService.testOrderedDtp();
return "testOrderedDtp success";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.dynamictp.example.service;

/**
* TestService related
*
* @author yanhom
* @since 1.1.0
*/
public interface TestService {

/**
* Test juc tp.
*/
void testJucTp();

/**
* Test spring tp.
*/
void testSpringTp();

/**
* Test common dtp.
*/
void testCommonDtp();

/**
* Test eager dtp.
*/
void testEagerDtp();

/**
* Test scheduled dtp.
*/
void testScheduledDtp();

/**
* Test ordered dtp.
*/
void testOrderedDtp();
}
Loading

0 comments on commit ef3ee42

Please sign in to comment.