-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
389 additions
and
17 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
austin-cron/src/main/java/com/java3y/austin/cron/config/AsyncConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.java3y.austin.cron.config; | ||
|
||
import com.google.common.base.Throwables; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
|
||
/** | ||
* 处理定时任务的线程池配置信息,为@Async注解服务 | ||
* | ||
* @author 3y | ||
*/ | ||
@Slf4j | ||
@Configuration | ||
@EnableAsync | ||
public class AsyncConfiguration implements AsyncConfigurer { | ||
|
||
@Bean("austinExecutor") | ||
public ThreadPoolTaskExecutor executor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(10); | ||
executor.setMaxPoolSize(10); | ||
executor.setQueueCapacity(30); | ||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); | ||
executor.setThreadNamePrefix("austinAsyncExecutor-"); | ||
executor.setWaitForTasksToCompleteOnShutdown(true); | ||
executor.setAwaitTerminationSeconds(10); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
|
||
@Override | ||
public Executor getAsyncExecutor() { | ||
return executor(); | ||
} | ||
|
||
@Override | ||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { | ||
return (ex, method, params) -> log.error("austinExecutor execute fail!method:{},params:{},ex:{}", method, params, Throwables.getStackTraceAsString(ex)); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
austin-cron/src/main/java/com/java3y/austin/cron/constants/PendingConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.java3y.austin.cron.constants; | ||
|
||
/** | ||
* @author 3y | ||
* @date 2022/2/13 | ||
* 缓冲pending 常量 | ||
*/ | ||
public class PendingConstant { | ||
|
||
/** | ||
* 阻塞队列大小 | ||
*/ | ||
public static final Integer QUEUE_SIZE = 100; | ||
|
||
/** | ||
* 触发执行的数量阈值 | ||
*/ | ||
public static final Integer NUM_THRESHOLD = 100; | ||
|
||
/** | ||
* batch 触发执行的时间阈值,单位毫秒【必填】 | ||
*/ | ||
public static final Long TIME_THRESHOLD = 1000L; | ||
|
||
/** | ||
* 消费线程数 | ||
*/ | ||
public static final Integer THREAD_NUM = 2; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
austin-cron/src/main/java/com/java3y/austin/cron/pending/CrowdBatchTaskPending.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.java3y.austin.cron.pending; | ||
|
||
import com.java3y.austin.cron.domain.CrowdInfoVo; | ||
import com.java3y.austin.support.pending.BatchPendingThread; | ||
import com.java3y.austin.support.pending.Pending; | ||
import com.java3y.austin.support.pending.PendingParam; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 批量处理任务信息 | ||
* | ||
* @author 3y | ||
*/ | ||
@Component | ||
@Slf4j | ||
public class CrowdBatchTaskPending extends Pending<CrowdInfoVo> { | ||
|
||
@Override | ||
public void initAndStart(PendingParam pendingParam) { | ||
threadNum = pendingParam.getThreadNum() == null ? threadNum : pendingParam.getThreadNum(); | ||
queue = pendingParam.getQueue(); | ||
|
||
for (int i = 0; i < threadNum; ++i) { | ||
BatchPendingThread<CrowdInfoVo> batchPendingThread = new BatchPendingThread(); | ||
batchPendingThread.setPendingParam(pendingParam); | ||
batchPendingThread.setName("batchPendingThread-" + i); | ||
batchPendingThread.start(); | ||
} | ||
} | ||
|
||
@Override | ||
public void doHandle(List<CrowdInfoVo> list) { | ||
log.info("theadName:{},doHandle:{}", Thread.currentThread().getName(), list.size()); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
austin-support/src/main/java/com/java3y/austin/support/pending/BatchPendingThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.java3y.austin.support.pending; | ||
|
||
import com.google.common.base.Throwables; | ||
import com.google.common.collect.Lists; | ||
import lombok.Data; | ||
import lombok.experimental.Accessors; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* 延迟消费的线程 实现 | ||
* 积攒一定的数量 或 时间 才消费,达到批量消费的效果 | ||
* | ||
* @author 3y | ||
*/ | ||
@Data | ||
@Accessors(chain = true) | ||
@Slf4j | ||
public class BatchPendingThread<T> extends Thread { | ||
|
||
private PendingParam<T> pendingParam; | ||
|
||
/** | ||
* 批量装载任务 | ||
*/ | ||
private List<T> tasks = new ArrayList<>(); | ||
|
||
/** | ||
* 当前装载任务的大小 | ||
*/ | ||
private Integer total = 0; | ||
|
||
/** | ||
* 上次执行的时间 | ||
*/ | ||
private Long lastHandleTime = System.currentTimeMillis(); | ||
|
||
|
||
@Override | ||
public void run() { | ||
while (true) { | ||
try { | ||
T obj = pendingParam.getQueue().poll(pendingParam.getTimeThreshold(), TimeUnit.MILLISECONDS); | ||
if (null != obj) { | ||
tasks.add(obj); | ||
} | ||
|
||
// 处理条件:1. 数量超限 2. 时间超限 | ||
if ((tasks.size() >= pendingParam.getNumThreshold()) | ||
|| (System.currentTimeMillis() - lastHandleTime >= pendingParam.getTimeThreshold())) { | ||
List<T> taskRef = tasks; | ||
tasks = Lists.newArrayList(); | ||
lastHandleTime = System.currentTimeMillis(); | ||
pendingParam.getPending().handle(taskRef); | ||
} | ||
} catch (Exception e) { | ||
log.error("BatchPendingThread#run failed:{}", Throwables.getStackTraceAsString(e)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.