Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义 RejectedExecutionHandler 启动问题 #432

Open
dbige opened this issue Jul 3, 2024 · 3 comments
Open

自定义 RejectedExecutionHandler 启动问题 #432

dbige opened this issue Jul 3, 2024 · 3 comments
Labels
question Further information is requested

Comments

@dbige
Copy link

dbige commented Jul 3, 2024

版本

     <dependency>
            <groupId>org.dromara.dynamictp</groupId>
            <artifactId>dynamic-tp-spring-boot-starter-common</artifactId>
            <version>1.1.7</version>
        </dependency>

sprinboot 2.3.4.RELEASE
jdk8

问题

如果我使用自定义的拒绝策略,我将无法正常启动

@Slf4j
@Component
public class CustomRejectPolicy implements RejectedExecutionHandler {

    @SneakyThrows
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        // 这里可以根据需要抛出自定义异常
        log.error(message);
        throw new CommonException(566, "当前访问量过大!");
    }
}
    @Bean
    public DtpExecutor eagerDtpExecutor() {
        return ThreadPoolBuilder.newBuilder()
                .threadPoolName("TEST_POOL-%d")
                .threadFactory("TEST_POOL")
                .corePoolSize(2)
                .maximumPoolSize(8)
                .queueCapacity(100)
                .rejectedExecutionHandler(rejectPolicy)
                .eager(true)
                .buildDynamic();
    }

在 使用懒加载获取该自定义拒绝策略org.dromara.dynamictp.common.util.ExtensionServiceLoader.load(Class clazz)方法中
ServiceLoader.load(clazz); 将无法获取到我自定义的 CustomRejectPolicy ,我看到 ThreadPoolBuilder.newBuilder() 方法提供了rejectedExecutionHandler(RejectedExecutionHandler handler) 的方法.我改怎么做呢? CustomRejectPolicy 已经交由 spring 使用 @resource 注解获取

@dbige dbige added the question Further information is requested label Jul 3, 2024
@yanhom1314
Copy link
Collaborator

没有配置spi?

@dbige
Copy link
Author

dbige commented Jul 4, 2024

感谢您的回复,我没有理解你的意思? spi 配置是做什么配置呢? 我没有使用配置中心的动态线程池,而是使用框架案例中@bean方式生成的线程池,

ThreadPoolBuilder.newBuilder()
     .rejectedExecutionHandler(rejectPolicy)
   剩余配置省略

然后该配置与 dtpLifecycle 类自动配置相关有冲突报错截取如下

org.springframework.context.ApplicationContextException: Failed to start bean 'dtpLifecycle'; nested exception is org.dromara.dynamictp.common.ex.DtpException: Cannot find specified rejectedHandler CustomRejectPolicy
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:185)
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53)
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360)
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158)
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122)
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:895)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
	at com.pec.retail.mdc.MdcServiceApplication.main(MdcServiceApplication.java:23)
Caused by: org.dromara.dynamictp.common.ex.DtpException: Cannot find specified rejectedHandler CustomRejectPolicy

从报错上看应该是框架没法找到我自定义的拒绝策略CustomRejectPolicy ,上文有述说过不在赘述.
我太理解我还要做特殊配置,调用api去修改?还是我遗漏了什么?
我没在官网的举例中找到自定义拒绝策略的相关配置,如果有的话能否提供下举例截图,或者相关路径,感谢

@KamToHung
Copy link
Collaborator

ExtensionServiceLoader

https://dynamictp.cn/guide/notice/custom.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E9%80%9A%E7%9F%A5%E6%B8%A0%E9%81%93 可以参考这个,resources/META-INF/services 下配置 java spi 实现

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants