Elastic-Job(四):簡單的例子

我們先讓代碼跑起來,來一個簡單的例子。

一、引入jar包

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

<!-- 這里要注意,我們和Spring集成,要引入這個jar包 -->
<dependency>
  <groupId>com.dangdang</groupId>
  <artifactId>elastic-job-lite-spring</artifactId>
  <version>2.1.5</version>
</dependency>

二、代碼開發(fā)

1.配置文件

在/Davis/src/main/resources/目錄下增加spring-job目錄,并新建文件spring-job.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
                        ">
    <!--配置作業(yè)注冊中心 zookeeper_url=10.136.19.170:2181,zookeeper_namespace=elastic-job這個可以自己任意起名字 -->
    <reg:zookeeper id="regCenter" 
        server-lists="${zookeeper_url}" 
        namespace="${zookeeper_namespace}" 
        base-sleep-time-milliseconds="1000" 
        max-sleep-time-milliseconds="3000" 
        max-retries="3" />
    
    <!-- 配置作業(yè) -->
    <job:simple id="demoSimpleSpringJob" class="com.spring.lw.job.UserElasticJob" 
        registry-center-ref="regCenter" 
        cron="0/10 * * * * ?" 
        sharding-total-count="3" 
        sharding-item-parameters="0=A,1=B,2=C" />

</beans>

2.代碼

package com.spring.lw.job;

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

public class UserElasticJob implements SimpleJob {

    @Override
    public void execute(ShardingContext context) {
        // TODO Auto-generated method stub
        switch (context.getShardingItem()) {
            case 0: 
                System.out.println("UserElasticJob execute context = " + 0);
                break;
            case 1: 
                System.out.println("UserElasticJob execute context = " + 1);
                break;
            case 2: 
                System.out.println("UserElasticJob execute context = " + 2);
                break;
            // case n: ...
        }
    }

}

3.運(yùn)行

放到Tomcat跑一下看看,當(dāng)然zookeeper要先啟動起來。
控制臺會輸出:

UserElasticJob execute context = 0
UserElasticJob execute context = 1
UserElasticJob execute context = 2

反正是跑起來了,雖然到現(xiàn)在看起來沒什么卵用。

我們把這個工程部署到兩個Tomcat試試,啟動兩個Tomcat,會發(fā)現(xiàn)。
Tomcat1的控制臺輸出:

UserElasticJob execute context = 0
UserElasticJob execute context = 2

Tomcat2的控制臺輸出:

UserElasticJob execute context = 1

好像體會到一點(diǎn)好處了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容