Elastic-job的简单使用

Github路径:https://github.com/dangdangdotcom/elastic-job

官方文档:https://dangdangdotcom.github.io/elastic-job/


1.增加maven依赖

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

			<!-- 使用springframework自定义命名空间时引入 -->
			<dependency>
				<groupId>com.dangdang</groupId>
				<artifactId>elastic-job-lite-spring</artifactId>
				<version>2.0.5</version>
			</dependency>

2. 增加相应的xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:reg="http://www.dangdang.com/schema/ddframe/reg"
       xmlns:job="http://www.dangdang.com/schema/ddframe/job"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.dangdang.com/schema/ddframe/reg
                        http://www.dangdang.com/schema/ddframe/reg/reg.xsd
                        http://www.dangdang.com/schema/ddframe/job
                        http://www.dangdang.com/schema/ddframe/job/job.xsd
                        ">
    <!--configure registry center -->
    <reg:zookeeper id="regCenter" server-lists="10.0.11.175:2181,10.0.11.176:2181,10.0.11.177:2181" namespace="ycFeed-driverRate-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />

    <!--configure job -->
    <job:simple id="myElasticJob" class="com.yc.feed.web.cron.MyElasticJob" registry-center-ref="regCenter" cron="0 * * * * ?"   sharding-total-count="3" sharding-item-parameters="0=A,1=B,2=C" />
</beans>

3. 编辑相应的class

package com.yc.feed.web.cron;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.yc.feed.service.DriverRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by lfeng on 2017/2/17.
 *
 *
 */

@Component
public class MyElasticJob implements SimpleJob {
    @Autowired
    private DriverRateService driverRateService;

    @Override
    public void execute(ShardingContext context) {
        int currentShard = context.getShardingItem();
        int shardTotal = context.getShardingTotalCount();
        driverRateService.runDriverCommentEvaluation(shardTotal,currentShard);
    }
}

4.runDriverCommentEvaluation安装正常的service类写就可以,只需要修改相应的sql语句,增加查询条件
and mod(driver_id,shardTotal) = currentShard








阅读更多
换一批

没有更多推荐了,返回首页