Skip to content

Dinger 1.3 个性化配置项

Jaemon edited this page Dec 26, 2020 · 1 revision

Dinger个性化扩展配置项

一、独立配置

1. 手动发送方式消息自定义

@Component
@Slf4j
public class AppInit implements InitializingBean {
    @Autowired
    private DingerSender dingerSender;
    @Override
    public void afterPropertiesSet() throws Exception {
        // 发送text类型消息
        dingerSender.send(
                MessageSubType.TEXT,
                DingerRequest.request("Hello World, Hello Dinger")
        );

        // 发送markdown类型消息
        dingerSender.send(
                MessageSubType.MARKDOWN,
                DingerRequest.request("Hello World, Hello Dinger", "启动通知")
        );
    }
}
@Configuration
public class MyDingerConfiguration {

    // 自定义text类型消息体,仅限手动发送功能,不适用于xml标签或注解统一管理消息体功能
    @Bean
    public CustomMessage textMessage() {
        return (projectId, request) ->
                MessageFormat.format(
                        "【Text通知】 {0}\n- 内容: {1}.",
                        projectId, request.getContent());
    }

    // 自定义markdown类型消息体,仅限手动发送功能,不适用于xml标签或注解统一管理消息体功能
    @Bean
    public CustomMessage markDownMessage() {
        return (projectId, request) ->
                MessageFormat.format(
                        "#### 【Markdown通知】 - 项目名称: {0}\n- 内容: {1}",
                        projectId, request.getContent());
    }
}

 

2. 其他扩展配置

以下个性化配置项均可适用于手动发送和消息体统一管理方式

@Configuration
@Slf4j
public class MyDingerConfiguration {
    // 自定义异步回调函数-用于异步发送时
    @Bean
    public DingerAsyncCallback dingerAsyncCallback() {
        return (dingerId, result) -> {
            log.info("dingerId={}, response result={}.", dingerId, result);
        };
    }

    
    // 自定义DingerId生成器, dingerId为每次调用返回体中的logid值
    @Bean
    public DingerIdGenerator dingerIdGenerator() {
        return () -> UUID.randomUUID().toString();
    }


    // 自定义异常回调函数
    @Bean
    public DingerExceptionCallback dingerExceptionCallback() {
        return dingerCallback -> {
            log.error("dingerId={}, message={}.", 
                      dingerCallback.getDkid(), dingerCallback.getMessage());
        };
    }


    // 自定义异步执行线程池
    @Bean
    public Executor dingerExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix("DingerAsync-");
        executor.setCorePoolSize(30);
        executor.setMaxPoolSize(30);
        executor.initialize();
        return executor;
    }
    
    
    // 自定义restTemplate客户端
    @Bean
    public RestTemplate dingerRestTemplate() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(100 * 1000);
        factory.setConnectTimeout(100 * 1000);
        return new RestTemplate(factory);
    }
}

 

二、整体配置

@Configuration
@Slf4j
public class MyDingerConfiguration extends DingerConfigurerAdapter {

    @Override
    public void configure(DingerManagerBuilder config) throws Exception {
        config
            	.textMessage(...)
                .markDownMessage(...)
                .dingerAsyncCallback(...)
                .dingerIdGenerator(...)
                .dingerExceptionCallback(...)
                .dingTalkExecutor(...)
                .dingerRestTemplate(...);

    }
}
Clone this wiki locally