Giter Site home page Giter Site logo

mars-job's Introduction

分布式任务调度框架使用

概述

该框架可以做什么
1. 继承simplejob保证多机同时只有一台机器执行任务
2. 继承shardingjob可以获取分片,多机协调完成任务
3. 任务在一台机器上添加,可以选择同步到集群上,方便集群间任务启停同步,动态添加任务
4. 任务负载均衡方式,默认随机方式,可配置任务的执行调度方式改为一致性hash的方式,保证宕机后任务的分配。

快速开始

1.POM依赖

<dependency>
   <groupId>org.lem</groupId>
   <artifactId>mars-api</artifactId>        
   <version>0.0.1-SNAPSHOT</version>
</dependency>

2.启用任务调度 @EnableMarsScheduling

3.所需配置,框架自带quartz调度如需修改配置添加quartz.properties到项目中

zk:
 address: localhost:2181  #注册的zk
project:
 group: mars     #项目名
job_event:
   expire_day: 7  #事务日志(删除七天前的事务日志)
job_execute_log:
   expire_day: 7  #任务执行日志清理时间(删除七天前的日志)

4.添加任务 使用spring自动注入JobScheduleService jobScheduleService

 @Autowired
 private JobScheduleService jobScheduleService;
 ------------------------------
 JobParam jobParam=new JobParam(SimpleJob.class,"0 * * * * ?","0","0");
 jobScheduleService.addJob(jobParam,false);

addjob方法第一个参数为任务配置,第二个为是否同步到集群上,同理还有updateJob,deleteJob等方法
jobParam中的jobData可以在任务执行中被获取

 Map<String, String> jobMap = new HashMap<>();
 jobMap.put("property1", "1234");
 JobParam jobParam = new JobParam(DelegateSimpleJob.class, scheduled.cron(), group, name);
 jobParam.setJobData(jobMap);
public class ExampleJob extends SimpleJobExecutor {
    @Override
    protected void internalJobExecute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            String property1 = (String) jobExecutionContext.getMergedJobDataMap().get("property1");
        } catch (Exception e) {
            throw new RuntimeException("execute job fail",e);
        }
    }
}

5.spring 注解

@Component
public class Job {
    @SimpleJobSchedule(cron = "* * * * * ?", group = "a", name = "b", startTime = "2019-01-21 12:00:00", endTime = "2019-01-21 18:00:00",balance=true)
    public void say() {
        System.out.println("-------say--------------");
    }
}

其中cron为必填项,group默认为类名,name默认为方法名,同一方法可以有多个注解 balance表明采用一致性hash

6. 例子

如不使用spring框架,可参考mars-job-example中mars-example-java模块

mars-job's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.