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

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

ElasticJob分布式调度,监听器的使用附源码(四)
  • 问题背景
  • 项目搭建
  • 代码测试
  • 总结
  • Lyric: 如果要走请你记得我

问题背景

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

  • 默认安装zookeeper,可以参考我的另一篇文章zookeeper单机及集群部署,附安装包下载(二)
  • 默认安装JDK
  • 可以复制文章的代码自己创建工程,也可以自己下载源码进行参考
  • 依赖是有两个版本的,以前是当当网的开源项目,现在也有Apache的版本,本文使用的当当网的开源
项目搭建

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: 如果要走请你记得我

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/719241.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存