Spring-boot統合elastic-job


Spring-boot統合elastic-jobノート


1.依存を追加【追加する必要があるelastic-job依存jarのみをここにリストする】
<dependency>
    <groupId>com.github.kuhn-hegroupId>
    <artifactId>elastic-job-lite-spring-boot-starterartifactId>
    <version>2.1.5version>
dependency>

2.該当する構成項目の追加
elaticjob.zookeeper.server-lists=10.140.6.18:2181,10.140.6.19:2181
elaticjob.zookeeper.namespace=my-project

3.タイミングタスクの作成
import com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob;

import org.springframework.stereotype.Component;

import com.dangdang.ddframe.job.api.ShardingContext;

@ElasticSimpleJob(cron="* * * * * ?",jobName="test123",shardingTotalCount=2,jobParameter=" ",shardingItemParameters="0=A,1=B")
@Component
public class MyJob implements com.dangdang.ddframe.job.api.simple.SimpleJob {

    @Override
    public void execute(ShardingContext content) {
        //do something
        System.out.println("JobName:"+content.getJobName());
        System.out.println("JobParameter:"+content.getJobParameter());
        System.out.println("ShardingItem:"+content.getShardingItem());
        System.out.println("ShardingParameter:"+content.getShardingParameter());
    System.out.println("ShardingTotalCount:"+content.getShardingTotalCount());
        System.out.println("TaskId:"+content.getTaskId());
        System.out.println("---------------------------------------");

    }
}

4.実行結果テスト結果から見ると、jobNameの設定が無効で、jobParameterが手に入らない.この二つの問題はしばらく解決していないので、まだ検討しなければならない.
---------------------------------------
JobName:com.lenovo.job.MyJob
JobParameter:
ShardingItem:1
ShardingParameter:B
ShardingTotalCount:2
TaskId:com.lenovo.job.MyJob@-@0,1@-@READY@-@10.250.129.181@-@6024
---------------------------------------
JobName:com.lenovo.job.MyJob
JobParameter:
ShardingItem:0
ShardingParameter:A
ShardingTotalCount:2
TaskId:com.lenovo.job.MyJob@-@0,1@-@READY@-@10.250.129.181@-@6024
---------------------------------------

動的elastic-jobタスクの追加


現在解決されていない問題:動的に追加されたタスクは、追加されたマシンでのみ実行でき、平行に配置された他のマシンでは実行されません.
上の構成に基づいて、次の構成を追加します.
1.zookeeper構成クラスの追加
package com.lenovo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
@Configuration
public class ElasticJobConfig {

    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter regCenter(@Value("${elaticjob.zookeeper.server-lists}") final String serverList, @Value("${elaticjob.zookeeper.namespace}") final String namespace) {
        return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
    }
}

2.タスクロジックの動的追加
package com.lenovo.controller;

import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.lenovo.job.MyJob;

@RestController
public class TestController {

    @Autowired
    private ZookeeperRegistryCenter zookeeperRegistryCenter;

    @RequestMapping("/test")
    public void test() {
        int shardingTotalCount = 2;
        String jobName = UUID.randomUUID().toString() + "-test123";
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder(jobName, "* * * * * ?", shardingTotalCount).shardingItemParameters("0=A,1=B").build();

        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, MyJob.class.getCanonicalName());

        JobScheduler jobScheduler = new JobScheduler(zookeeperRegistryCenter, LiteJobConfiguration.newBuilder(simpleJobConfiguration).build());
        try {
            jobScheduler.init();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(" ");
        }

    }

}