elastic job简单用法
public class JobMain { //配置注册中心 private ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(PropertiesUtil.getProperty("config/route.properties","zookeeper.url"),"third_recon_job"); private CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zkConfig); /** 创建任务 *JobCoreConfiguration.newBuilder("任务名称", "corn表达式", 所在分片 ).description("任务描述说明").build() *BlackListJob.class 任务类 */ private final LiteJobConfiguration blackListJob = LiteJobConfiguration.newBuilder( new SimpleJobConfiguration(JobCoreConfiguration.newBuilder("blackListJob", "0 0 15 3 * ?", 1) .description("1:拉取黑名单").build(), BlackListJob.class.getCanonicalName())).build(); /** * 配置数据源连接 **/ public static JobEventConfiguration createJobEventConfiguration() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setUsername(PropertiesUtil.getProperty("config/datasource.properties", "jdbc.username")); dataSource.setPassword(PropertiesUtil.getProperty("config/datasource.properties", "jdbc.password")); dataSource.setDriverClassName(PropertiesUtil.getProperty("config/datasource.properties", "jdbc.driver.class")); dataSource.setUrl(PropertiesUtil.getProperty("config/datasource.properties", "jdbc.url")); JobEventConfiguration jobEventConfig = new JobEventRdbConfiguration(dataSource); return jobEventConfig; } public void init() { //加载spring配置 ReconciliationTaskConstant.setContext(new ClassPathXmlApplicationContext("applicationContext/application*context.xml")); // 连接注册中心 regCenter.init(); // 初始化作业 new JobScheduler(regCenter, blackListJob, createJobEventConfiguration()).init(); //拉取黑名单任务 } public static void main(String[] args) { new JobMain().init(); } } /**** ** 简单任务类 */ public class BlackListJob implements SimpleJob { @Override public void execute(ShardingContext shardingContext) { //context.getShardingItem()获取分片,根据不同的分片,执行不同的任务 System.out.println("开始执行不分片的简单任务"); } }
执行方式:
直接打包成jar,然后:
java -jar xxxxx.jar
作者:wxw_wang
-------------------------------------------
个性签名:做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!