1. 程式人生 > >elastic job簡單用法

elastic job簡單用法

uil rman source simple pat 名稱 cat out ont

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簡單用法