elastic job简单用法
Posted wxw_wang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于elastic job简单用法的主要内容,如果未能解决你的问题,请参考以下文章