ElasticJob分布式调度,监听器的使用附源码(四)

ElasticJob分布式调度,监听器的使用附源码(四)

问题背景

上一篇介绍了ElasticJob分布式动态定时任务,这个篇章介绍一下分布式监听器定时任务
注意事项:

项目搭建

1 根据上一个篇章的代码,更改上一篇的代码,在配置类里面添加监听器的bean配置

package com.yg.elasticjob.config;


import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.yg.elasticjob.elasticjob.ElasticJobListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author suolong
 * @Date 2022/4/14 11:11
 * @Version 2.0
 */
@Configuration
public class ElasticJobConfig {

    //配置文件中的zookeeper的ip和端口
    @Value(value = "${zkserver}")
    private String serverlists;
    //指定一个命名空间
    @Value("${zknamespace}")
    private String namespace;

    /***
     * 配置Zookeeper和namespace
     * @return
     */
    @Bean
    public ZookeeperConfiguration zkConfig() {
        return new ZookeeperConfiguration(serverlists, namespace);
    }


    /***
     * 向zookeeper注册初始化信息
     * @param
     * @return
     */
    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter regCenter(@Value("${zkserver}") final String serverList, @Value("${zknamespace}") final String namespace) {
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(serverList, namespace);
       // zookeeperConfiguration.setSessionTimeoutMilliseconds(10000);
        return new ZookeeperRegistryCenter(zookeeperConfiguration);
    }

    /****
     * 创建ElasticJob的监听器实例
     * @return
     */
    @Bean
    public ElasticJobListener elasticJobListener() {
        //初始化要给定超时多少秒重连
        return new ElasticJobListener(100L, 100L);
    }
}

2 监听器的类,类似于AOP拦截器的before和after一样,在执行任务前先执行doBeforeJobExecutedAtLastStarted,在执行任务之后执行doAfterJobExecutedAtLastCompleted

package com.yg.elasticjob.elasticjob;

import ch.qos.logback.core.util.TimeUtil;
import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.AbstractDistributeOnceElasticJobListener;
import lombok.extern.slf4j.Slf4j;
//import org.apache.shardingsphere.elasticjob.infra.listener.ShardingContexts;
//import org.apache.shardingsphere.elasticjob.lite.api.listener.AbstractDistributeOnceElasticJobListener;

import java.util.Date;

/**
 * @Author suolong
 * @Date 2022/4/14 11:18
 * @Version 1.5
 */
@Slf4j
public class ElasticJobListener extends AbstractDistributeOnceElasticJobListener {

    /****
     * 构造函数
     * @param startedTimeoutMilliseconds
     * @param completedTimeoutMilliseconds
     */
    public ElasticJobListener(long startedTimeoutMilliseconds, long completedTimeoutMilliseconds) {
        super(startedTimeoutMilliseconds, completedTimeoutMilliseconds);
    }

    /***
     * 任务初始化前要做的事情,类似前置通知
     * @param shardingContexts
     */
    @Override
    public void doBeforeJobExecutedAtLastStarted(ShardingContexts shardingContexts) {
        log.info("doBeforeJobExecutedAtLastStarted: {}", new Date());
    }

    /***
     * 任务执行完成后要做的事情,类似后置通知
     * @param shardingContexts
     */
    @Override
    public void doAfterJobExecutedAtLastCompleted(ShardingContexts shardingContexts) {
        log.info("doAfterJobExecutedAtLastCompleted: {}", new Date());
    }
}

3 整体项目目录

代码测试

1 可以看到监听器的打印

总结

  • 用起来就和AOP一样,可以根据需求写之前和之后需要做什么




作为程序员第 114 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric: 如果要走请你记得我

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值