Elastic Job 定时任务实现

官方文档:http://dangdangdotcom.github.io/elastic-job/elastic-job-lite/00-overview/intro/

该说的文档上都说了;在过程中遇到一些错误记下了


环境:zookeeper版本 zookeeper-3.4.6 或3.4.6以上 不然会莫名其妙的报错

我是maven 搭建的项目 需要包(当然spring的包是不可少的当然你用java来操作的话就不需要spring的包了):

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

spring-job配置(参数配置就不讲了官方文档都有配置;关键点是配置是配置的数据源作业维度操作历史记录看你自己了需要配你就配不需要就不配  event-trace-rdb-data-source="dataSource"):

<?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:context="http://www.springframework.org/schema/context" 
    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.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context.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 
                        ">
   
    <!--配置作业注册中心   -->
    <reg:zookeeper id="regCenter" server-lists="localhost:2181" namespace="erp-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />
    <!-- 0 0 0 * * ? -->
    <job:simple id="customerFollowElasticJob" class="com.isz.erp.job.CustomerFollowElasticJob" registry-center-ref="regCenter" cron="0 0 0 * * ?" 
    	sharding-total-count="2"    description="这里是描述" event-trace-rdb-data-source="dataSource"/>
    <!-- 配置作业-->
</beans>

java代码实现:

package com.isz.erp.job;

import org.springframework.beans.factory.annotation.Autowired;

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


/**
 * @Description: 
 * @date: 2017年9月8日 下午5:53:56
 */
public class CustomerFollowElasticJob  implements SimpleJob{

	
	
	@Override
	public void execute(ShardingContext context) {
	 
		
		testShareData(context.getShardingItem(), context.getShardingTotalCount(), 100, context.getTaskId());
	}
	
	
	/**
	* @Description: 测试分片数据 参数一当前分片 参数二分片总数 参数三 总条数
	* @return void 
	* @date 2017年9月7日 下午5:24:41
	 */
	public void testShareData(int sharItem,int sharCount,int dataCount,String taskId ){
		
		if(sharItem==(sharCount-1)){//如果总分片数据==当前分片 说明是最后数据
			System.out.println("分片...."+taskId+"......."+sharItem+"执行数据:"+((dataCount/sharCount)*sharItem)+"到"+((dataCount/sharCount)*(sharItem==0?1:sharItem+1)+(dataCount%sharCount)));	
		}else{
			System.out.println("分片...."+taskId+"........"+sharItem+"执行数据:"+((dataCount/sharCount)*sharItem)+"到"+(dataCount/sharCount)*(sharItem==0?1:sharItem+1));
		}
	}
	
	public static void main(String[] args) {
		
		//97条数据
		int dataCount=97;
		
		//5片
		int share=5;
		
		System.out.println("每个分片执行:"+97/5+"条数数据");
		
		System.out.println("最后一个分片执行:"+((97/5)+(dataCount%share))+"条数数据");
		
	}

}
启动服务:

package test.dubbo;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @描述: 启动Dubbo服务用的MainClass.
 * @作者:
 * @创建时间:
 * @版本: 1.0
 */
public class DubboProvider {

	private static final Log log = LogFactory.getLog(DubboProvider.class);

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		try {

			
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
					"classpath:spring/spring-context.xml");
			context.start();
		} catch (Exception e) {
			log.error("== DubboProvider context start error:", e);
		}
		synchronized (DubboProvider.class) {
			while (true) {
				try {
					DubboProvider.class.wait();
				} catch (InterruptedException e) {
					log.error("== synchronized error:", e);
				}
			}
		}
	}

}


启动服务后你应该就可以看到控制太打印的信息了..........................到此程序就玩了

可能要用到的工具:

官网上下载:elastic-job-lite-console-2.1.5 可以看到以下信息(相关参数官网上都有介绍相信你也看的懂是什么)

目录里bin下面有个start.bat如果是linux启动start.sh就好了 然后在浏览器上输入:http://localhost:8899/ 你就会看到如下信息



还可以下载:ZooInspector 看一下节点信息:




就这样完了。。。。。。。。。。。。。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用注解实现Elastic-Job定时任务可以让任务配置更加简洁和灵活,以下是一个简单的示例代码: 1. 引入Elastic-Job依赖 ```xml <dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-spring</artifactId> <version>2.1.5</version> </dependency> ``` 2. 定义任务实现类 ```java @JobHandler(value = "myJob") @Component public class MyJob implements SimpleJob { @Override public void execute(ShardingContext shardingContext) { System.out.println("分片项:" + shardingContext.getShardingItem() + ",总分片数:" + shardingContext.getShardingTotalCount()); // TODO: 执行具体的任务逻辑 } } ``` 在上面的示例中,我们使用@JobHandler注解定义了任务名称,并将任务实现类MyJob标记为Spring组件。 3. 配置任务 ```java @Configuration public class JobConfig { @Autowired private ZookeeperRegistryCenter registryCenter; @Bean(initMethod = "init") public JobScheduler simpleJobScheduler(final MyJob myJob, @Value("${myJob.cron}") final String cron, @Value("${myJob.shardingTotalCount}") final int shardingTotalCount, @Value("${myJob.shardingItemParameters}") final String shardingItemParameters) { return new SpringJobScheduler(myJob, registryCenter, getLiteJobConfiguration(myJob.getClass(), cron, shardingTotalCount, shardingItemParameters)); } private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) { return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder( jobClass.getName(), cron, shardingTotalCount) .shardingItemParameters(shardingItemParameters) .build(), jobClass.getCanonicalName())).overwrite(true).build(); } } ``` 在上面的示例中,我们使用@Configuration注解定义了任务配置类,并注入了ZookeeperRegistryCenter对象。然后定义了simpleJobScheduler方法,使用@Bean注解标记为Spring组件,并将MyJob实例和任务配置信息作为参数传入。 在getLiteJobConfiguration方法中,我们使用JobCoreConfiguration和SimpleJobConfiguration两个类实现任务的基本配置,然后使用LiteJobConfiguration进行总配置,并使用overwrite方法将原有任务配置覆盖。最后,在simpleJobScheduler方法中,使用SpringJobScheduler类启动任务。 4. 配置文件 在配置文件中,我们可以指定任务的cron表达式、分片数量和分片项参数等信息,例如: ```properties myJob.cron=0/5 * * * * ? myJob.shardingTotalCount=3 myJob.shardingItemParameters=0=A,1=B,2=C ``` 这样,就可以使用Java注解实现Elastic-Job定时任务功能,让任务配置更加简单和灵活。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值