[xuxueli/xxl-job]maven surefire单测问题

2024-05-15 110 views
3

多个spring test context会报job handler名称冲突

1.8.2

具体导致代码
private static void initJobHandlerRepository(ApplicationContext applicationContext){
        // init job handler action
        Map<String, Object> serviceBeanMap = applicationContext.getBeansWithAnnotation(JobHander.class);

        if (serviceBeanMap!=null && serviceBeanMap.size()>0) {
            for (Object serviceBean : serviceBeanMap.values()) {
                if (serviceBean instanceof IJobHandler){
                    String name = serviceBean.getClass().getAnnotation(JobHander.class).value();
                    IJobHandler handler = (IJobHandler) serviceBean;
                    if (loadJobHandler(name) != null) {
                        throw new RuntimeException("xxl-job jobhandler naming conflicts.");
                    }
                    registJobHandler(name, handler);
                }
            }
        }
    }
//这个静态的属性,会在maven surefire单测的时候导致相同的job handler会有多个。可能是maven同一jvm实例跑多个单测导致。
 private static ConcurrentHashMap<String, IJobHandler> jobHandlerRepository = new ConcurrentHashMap<String, IJobHandler>();
    public static IJobHandler registJobHandler(String name, IJobHandler jobHandler){
        logger.info(">>>>>>>>>>> xxl-job register jobhandler success, name:{}, jobHandler:{}", name, jobHandler);
        return jobHandlerRepository.put(name, jobHandler);
    }
    public static IJobHandler loadJobHandler(String name){
        return jobHandlerRepository.get(name);
    }

回答

3

执行器组件不支持重复初始化,该组件每次初始化会占用一个通讯端口,重复加载即使没有这个问题也会端口冲突。

不过针对上述问题做了修复,可以拉取master分支体验下。

0

版本:2.0.2 Caused by: java.lang.RuntimeException: xxl-job jobhandler naming conflicts. 在多个测试spring test context下,还是会报这个错误,怎么解决 image

2

执行器组件不支持重复初始化,该组件每次初始化会占用一个通讯端口,重复加载即使没有这个问题也会端口冲突。

不过针对上述问题做了修复,可以拉取master分支体验下。 版本:2.0.2 Caused by: java.lang.RuntimeException: xxl-job jobhandler naming conflicts. 在多个测试spring test context下,还是会报这个错误,怎么解决 image

1

版本:2.0.2 Caused by: java.lang.RuntimeException: xxl-job jobhandler naming conflicts. 在多个测试spring test context下,还是会报这个错误,怎么解决 image

遇到同样问题,兄弟你解决了吗?

9

同样的问题,xxl-job jobhandler[DATA_COMBINE_HANDLER] naming conflicts 版本:2.2.0

image

9

Steps to fix unit testing broken by XxlJobSpringExecutor:

1) Add a MockBeans class with a @Primary bean under src/test:

/**
 *  Mocked beans for unit-testing。
 */
@Component
public class MockBeans {

    /**
     * Get a mocked xxl-job executor for unit-testing。
     * @return a mocked xxl-job executor
     */
    @Bean
    @Primary
    public XxlJobSpringExecutor getMockedXxlJobExecutor() {
        return new MockedXxlJobExecutor();
    }

    /**
     * Mocked xxl job executor。
     */
    private static class MockedXxlJobExecutor
        extends XxlJobSpringExecutor
        implements ApplicationContextAware, SmartInitializingSingleton, DisposableBean {

        @Override
        public void afterSingletonsInstantiated() {
            // do nothing
        }

        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            // do nothing
        }
    }
}

2) Add the @ConditionalOnMissingBean annotation in the config class under src/main:

@Configuration
public class XxlJobConfig {
    @Bean
    @ConditionalOnMissingBean
    public XxlJobSpringExecutor xxlJobExecutor() {
        //...
    }
}