diff --git a/docs/docs/user_docs/dev_manual/_category_.json b/docs/docs/user_docs/dev_manual/_category_.json new file mode 100644 index 00000000..11f12432 --- /dev/null +++ b/docs/docs/user_docs/dev_manual/_category_.json @@ -0,0 +1,8 @@ +{ + "label": "开发者手册", + "position": 5, + "link": { + "type": "generated-index", + "description": "Hippo4J 留给使用者能够扩展的知识点。" + } +} diff --git a/docs/docs/user_docs/dev_manual/blocking-queue-custom.md b/docs/docs/user_docs/dev_manual/blocking-queue-custom.md new file mode 100644 index 00000000..3ab0d33c --- /dev/null +++ b/docs/docs/user_docs/dev_manual/blocking-queue-custom.md @@ -0,0 +1,7 @@ +--- +sidebar_position: 1 +--- + +# 阻塞队列自定义 + +同拒绝策略自定义。 \ No newline at end of file diff --git a/docs/docs/user_docs/dev_manual/rejected-policy-custom.md b/docs/docs/user_docs/dev_manual/rejected-policy-custom.md new file mode 100644 index 00000000..041adef8 --- /dev/null +++ b/docs/docs/user_docs/dev_manual/rejected-policy-custom.md @@ -0,0 +1,57 @@ +--- +sidebar_position: 0 +--- + +# 拒绝策略自定义 + +Hippo4J 通过 SPI 的方式对拒绝策略进行扩展,可以让用户在 Hippo4J 中完成自定义拒绝策略实现。 + +## Hippo4J Server 拒绝策略扩展 + +自定义拒绝策略,实现 `CustomRejectedExecutionHandler` 接口,示例如下: + +```java +public class ErrorLogRejectedExecutionHandler implements CustomRejectedExecutionHandler { + + @Override + public Integer getType() { + return 12; + } + + @Override + public RejectedExecutionHandler generateRejected() { + return new CustomErrorLogRejectedExecutionHandler(); + } + + public static class CustomErrorLogRejectedExecutionHandler implements RejectedExecutionHandler { + + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { + Logger logger = LoggerFactory.getLogger(this.getClass()); + logger.error("线程池抛出拒绝策略"); + } + } +} +``` + +创建 `src/main/resources/META-INF/services` 目录,创建 SPI 自定义拒绝策略文件 `cn.hippo4j.core.spi.CustomRejectedExecutionHandler`。 + +`cn.hippo4j.core.spi.CustomRejectedExecutionHandler` 文件内仅放一行自定义拒绝策略全限定名即可,示例: + +```text +cn.hippo4j.example.core.handler.ErrorLogRejectedExecutionHandler +``` + +创建、修改线程池页面选择 `CustomRejectedPolicy(自定义 SPI 策略)`。 + +![](https://images-machen.oss-cn-beijing.aliyuncs.com/image-20220801213735751.png) + +拒绝策略触发时,完成上述代码效果,仅打印异常日志提示。 + +```text +2022-08-01 21:27:49.515 ERROR 48928 --- [ateHandler.test] r$CustomErrorLogRejectedExecutionHandler : 线程池抛出拒绝策略 +``` + +:::tip +具体参考 `hippo4j-example/hippo4j-spring-boot-starter-example` 模块。 +::: diff --git a/docs/docs/user_docs/user_guide/_category_.json b/docs/docs/user_docs/user_guide/_category_.json index 6dc10c33..b97e89dc 100644 --- a/docs/docs/user_docs/user_guide/_category_.json +++ b/docs/docs/user_docs/user_guide/_category_.json @@ -1,5 +1,5 @@ { - "label": "用户指南", + "label": "用户手册", "position": 2, "link": { "type": "generated-index",