参考分布式定时任务基础使用:ES-JOB——分布式定时任务基础使用_择业的博客-CSDN博客

再上面链接代码后进行代码编译

分布式定时任务:按照分片执行,同一个jar在不同服务器上发布,同一定时任务,按照定时任务的分片执行。

高级用法思想:

一个job用后台创建job方式,后台创建设置管理定时任务(可以修改,不能新增)

用@job自定义注解方式,后台设置创建管理定时任务

1.将数据源与Job关联起来JobEventConfig


package com.bfxy.esjob.config;import javax.sql.DataSource;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;@Configuration
public class JobEventConfig {@Autowiredprivate DataSource dataSource;@Beanpublic JobEventConfiguration jobEventConfiguration() {return new JobEventRdbConfiguration(dataSource);}
}

2.将JobEventConfiguration注入到MySimpleJobConfig

package com.bfxy.esjob.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import com.bfxy.esjob.listener.SimpleJobListener;
import com.bfxy.esjob.task.MySimpleJob;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;@Configuration
public class MySimpleJobConfig {@Autowiredprivate ZookeeperRegistryCenter registryCenter;@Autowiredprivate JobEventConfiguration jobEventConfiguration;@Beanpublic SimpleJob simpleJob() {return new MySimpleJob();}/*** @param simpleJob* @return*/@Bean(initMethod = "init")public JobScheduler simpleJobScheduler(final SimpleJob simpleJob,@Value("${simpleJob.cron}") final String cron,@Value("${simpleJob.shardingTotalCount}") final int shardingTotalCount,@Value("${simpleJob.shardingItemParameters}") final String shardingItemParameters,@Value("${simpleJob.jobParameter}") final String jobParameter,@Value("${simpleJob.failover}") final boolean failover,@Value("${simpleJob.monitorExecution}") final boolean monitorExecution,@Value("${simpleJob.monitorPort}") final int monitorPort,@Value("${simpleJob.maxTimeDiffSeconds}") final int maxTimeDiffSeconds,@Value("${simpleJob.jobShardingStrategyClass}") final String jobShardingStrategyClass) {return new SpringJobScheduler(simpleJob,registryCenter,getLiteJobConfiguration(simpleJob.getClass(),cron,shardingTotalCount,shardingItemParameters,jobParameter,failover,monitorExecution,monitorPort,maxTimeDiffSeconds,jobShardingStrategyClass),jobEventConfiguration,new SimpleJobListener());}private LiteJobConfiguration getLiteJobConfiguration(Class<? extends SimpleJob> jobClass, String cron,int shardingTotalCount, String shardingItemParameters, String jobParameter, boolean failover,boolean monitorExecution, int monitorPort, int maxTimeDiffSeconds, String jobShardingStrategyClass) {JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).misfire(true).failover(failover).jobParameter(jobParameter).shardingItemParameters(shardingItemParameters).build();SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, jobClass.getCanonicalName());LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration).jobShardingStrategyClass(jobShardingStrategyClass).monitorExecution(monitorExecution).monitorPort(monitorPort).maxTimeDiffSeconds(maxTimeDiffSeconds).overwrite(false).build();return liteJobConfiguration;}}

3.流式Job,DataflowJobConfig/循环执行定时任务,每十秒钟执行一次

/** Copyright 1999-2015 dangdang.com.* <p>* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.* </p>*/package com.bfxy.esjob.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import com.bfxy.esjob.task.SpringDataflowJob;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.dataflow.DataflowJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;@Configuration
public class DataflowJobConfig {@Autowiredprivate ZookeeperRegistryCenter regCenter;@Autowiredprivate JobEventConfiguration jobEventConfiguration;@Beanpublic DataflowJob dataflowJob() {return new SpringDataflowJob();}@Bean(initMethod = "init")public JobScheduler dataflowJobScheduler(final DataflowJob dataflowJob, @Value("${dataflowJob.cron}") final String cron,@Value("${dataflowJob.shardingTotalCount}") final int shardingTotalCount,@Value("${dataflowJob.shardingItemParameters}") final String shardingItemParameters) {SpringJobScheduler springJobScheduler = new SpringJobScheduler(dataflowJob, regCenter, getLiteJobConfiguration(dataflowJob.getClass(), cron,shardingTotalCount, shardingItemParameters), jobEventConfiguration);
//      springJobScheduler.init();return springJobScheduler;}private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends DataflowJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {return LiteJobConfiguration.newBuilder(new DataflowJobConfiguration(JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName(),true))    //streamingProcess.overwrite(true).build();}
}

相关实体类

package com.bfxy.esjob.entity;public class Foo {private String id;private String name;public Foo() {}public Foo(String id, String name) {super();this.id = id;this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

具体job任务SpringDataflowJob

/** Copyright 1999-2015 dangdang.com.* <p>* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.* </p>*/package com.bfxy.esjob.task;import java.util.List;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import com.bfxy.esjob.entity.Foo;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;public class SpringDataflowJob implements DataflowJob<Foo> {private static final Logger LOGGER = LoggerFactory.getLogger(SpringDataflowJob.class);@Overridepublic List<Foo> fetchData(final ShardingContext shardingContext) {System.err.println("--------------@@@@@@@@@@ 抓取数据集合...--------------");return null;}@Overridepublic void processData(final ShardingContext shardingContext, final List<Foo> data) {System.err.println("--------------@@@@@@@@@ 处理数据集合...--------------");}
}

ES-JOB——分布式定时任务高级使用——控制台修改任务相关推荐

  1. quartz 分布式_6大分布式定时任务对比

    作者 | sharedCode 来源 | blog.csdn.net/u012394095/article/details/79470904 分布式定时任务简介 把分散的,可靠性差的计划任务纳入统一的 ...

  2. 6大分布式定时任务对比

    作者 | sharedCode 来源 | blog.csdn.net/u012394095/article/details/79470904 分布式定时任务简介 把分散的,可靠性差的计划任务纳入统一的 ...

  3. 分布式定时任务中间件

    在互联网应用中,各式各样的定时任务存于系统各个角落.我们希望由一个平台统一将这些作业管理起来.通过这个系统,作业的宕机.崩溃等状态就可收入运维同学掌控,直接对接报警系统,将发现的挂掉作业再启动就好.但 ...

  4. xxljob 配置具体定时任务_快速使用分布式定时任务 xxl-job

    快速使用分布式定时任务 xxl-job 需要linux服务器环境安装: jdk1.8 ,docker 安装步骤地址 : linux快速安装jdk   . 在linux里安装docker 1.docke ...

  5. php面试题 优信二手车_分布式定时任务对比 - sharedCode的个人空间 - OSCHINA - 中文开源技术交流社区...

    分布式定时任务 1. 什么是分布式定时任务 把分散的,可靠性差的计划任务纳入统一的平台,并实现集群管理调度和分布式部署的一种定时任务的管理方式.叫做分布式定时任务. 2. 常见开源方案 elastic ...

  6. Elastic-job实现分布式定时任务

    Elastic-job实现分布式定时任务 前言 最近接了一个新的需求,需要使用到定时任务,由于我们的系统是分布式的,所以Spring自带的定时任务无法满足我们当前系统对定时任务的需要.我们需要一个能够 ...

  7. 分布式定时任务技术选型

    1.目前的定时任务方案 Java中开发大多数使用Spring-Scheduler,只需要在Spring中的bean的对应方法加上@sheduler注解即可完成我们的定时任务,但是光是用这个注解还远远不 ...

  8. 全云化架构(十一):分布式定时任务框架对比

    分布式定时任务框架对比 把分散的,可靠性差的计划任务纳入统一的平台,并实现集群管理调度和分布式部署的一种定时任务的管理方式.叫做分布式定时任务. 常见的分布式定时任务平台有elastic-job , ...

  9. 6大分布式定时任务对比 就这?? 给你盘的明明白白

    分布式定时任务简介 把分散的,可靠性差的计划任务纳入统一的平台,并实现集群管理调度和分布式部署的一种定时任务的管理方式,叫做分布式定时任务. 常见开源方案 elastic-job xxl-job qu ...

最新文章

  1. c语言1余3,c语言1—3真题(含答案).ppt
  2. SerfJ REST
  3. Canny边缘检测原理及C#程序实现
  4. Windows编程---------MessageBox
  5. IOS笔记CALayer的position和anchorPoint
  6. linux下的五种io模型,Linux下的五种IO模型
  7. 【Linux】Ubuntu下进行C语言编程
  8. Sublime text 3 快捷键
  9. OCI runtime create failed: container_linux.go:370: starting container process caused: process_linux.
  10. V8声卡软件调试教程
  11. java直播在线人数怎么做_添加抖音直播间在线人数怎么做
  12. 计算机的时钟设置错误,谷歌浏览器用不了显示电脑时钟错误解决方法
  13. TCP三次握手中SYN,ACK,Seq含义
  14. 中国语言地图集 c1-12,中国语言地图集介绍——网上收集整理
  15. c++ Primer Plus笔记
  16. android 加花工具下载,Android 代码混淆并加花
  17. matlab模糊建模设计,毕业设计matlab建模
  18. 5分钟就能做一个Excel动态图表,你确定不学学?
  19. OneNote2016安装代码高亮插件
  20. 五年程序员谈软件工程师做职业规划的重要性

热门文章

  1. 分享几个实用的神器APP系列(四)
  2. 四路组相联原理_计算机组成原理中是如何求是几路组相联映像?
  3. 天翼云对象存储Java对接(经典版 Ⅱ型)
  4. 2022-04-19 Unity入门4——重要组件与API
  5. 【每日早报】2019/08/14
  6. signature=3d7534face990de7e25e7438440abe49,Designing the User Interface 5e
  7. 计算机一级的wps软件,计算机一级WPS
  8. 网络WireShark进行抓包
  9. Meta分析和网状Meta分析速成班( 2019年12月28-29日 上海)
  10. Win10 安装虚拟机ROS