当前位置:网站首页>Quick start to elastic job, three minutes to experience distributed scheduled tasks

Quick start to elastic job, three minutes to experience distributed scheduled tasks

2022-06-10 20:48:00 Wangfugui who lost his hair

First we need to understand ElasticJob What is it? ?

ElasticJob It is a distributed scheduling solution for Internet Ecology and massive tasks , By two independent subprojects ElasticJob-Lite and ElasticJob-Cloud form .
It's through flexible scheduling 、 Resource control 、 And the function of job management , Build a distributed scheduling solution for Internet scenarios , And through open architecture design , To provide diversified operation Ecology .
Its products use a unified job API, Developers only need to develop once , You can deploy it as you like .

Look at the difference between them ?

 Insert picture description here
What we are going to talk about this time is lite Version of

The environment we need to prepare :

jdk + zookeeper + maven

Quick start

You can also go directly to my code warehouse to pull it down and run it directly :

https://gitee.com/WangFuGui-Ma/elastic-job-quickstart

First step , Create a maven Project and import jar package

	<dependencies>
        <dependency>
            <groupId>com.dangdang</groupId>
            <artifactId>elastic-job-lite-core</artifactId>
            <version>2.1.5</version>
        </dependency>
    </dependencies>

 Insert picture description here

The second step , Create a myjob Class inheritance simplejob class

 Insert picture description here

public class MyJob implements SimpleJob {
    
    public void execute(ShardingContext shardingContext) {
    
        System.out.println(" Scheduled tasks started "+ new Date());
    }
}

The third step , Create a JobDemo Start class

 Insert picture description here

public class JobDemo {
    
    public static void main(String[] args) {
    

        new JobScheduler(createRegistryCenter(), createJobConfiguration()).init();
    }

    private static LiteJobConfiguration createJobConfiguration() {
    
        // Create job configuration 
        JobCoreConfiguration demoSimpleJob = JobCoreConfiguration.newBuilder("demoSimpleJob", "0/3 * * * * ?", 1).build();
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(demoSimpleJob, MyJob.class.getCanonicalName());

        return LiteJobConfiguration.newBuilder(simpleJobConfiguration).build();
    }

    private static CoordinatorRegistryCenter createRegistryCenter() {
    
        ZookeeperConfiguration zkConfig = new ZookeeperConfiguration("localhost:2181", "elasticJob");
        zkConfig.setSessionTimeoutMilliseconds(100);
        CoordinatorRegistryCenter elasticJob = new ZookeeperRegistryCenter(zkConfig);
        elasticJob.init();
        return elasticJob;
    }

}

Step four , Start the local zookeeper

If you don't know how to start and install, you can refer to my article :

https://blog.csdn.net/csdnerM/article/details/121848173

Step five , start-up jobdemo

The console will start printing !! Congratulations on completing Elastic-Job A quick start to !!

 Insert picture description here

原网站

版权声明
本文为[Wangfugui who lost his hair]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101946088647.html