elastic-job集成到springboot教程,和它的一个异常处理办法:Sharding item parameters '1' format error, should be int=xx,int=xx
先说这个Sharding item parameters '1' format error, should be int=xx,int=xx异常吧,这是在做动态添加调度任务的时候出现的,网上找了一会没有搜到任何信息,最后发现,是添加任务这个方法里有一个漏洞。
这个源码出自:
private ShardingItem parse(final String shardingItemParameter, final String originalShardingItemParameters) {
String[] pair = shardingItemParameter.trim().split(KEY_VALUE_DELIMITER);
if (2 != pair.length) {
throw new JobConfigurationException("Sharding item parameters '%s' format error, should be int=xx,int=xx", originalShardingItemParameters);
}
try {
return new ShardingItem(Integer.parseInt(pair[0].trim()), pair[1].trim());
} catch (final NumberFormatException ex) {
throw new JobConfigurationException("Sharding item parameters key '%s' is not an integer.", pair[0]);
}
}
修改前代码(报这个异常的代码):
public void addJobScheduler(final Class<? extends SimpleJob> jobClass,
final String cron,
final int shardingTotalCount,
final String shardingItemParameters) {
JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build();
SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(coreConfig, jobClass.getCanonicalName());
JobScheduler jobScheduler = new JobScheduler(regCenter, LiteJobConfiguration.newBuilder(simpleJobConfig).build());
jobScheduler.init();
}
是不是发现不管你怎么设置,都给你报这个,你明明传的就不是1这个参数,还是给你报这个,问题出在build()那里,需要overwrite。修改后:
public void addJobScheduler(final Class<? extends SimpleJob> jobClass,
final String cron,
final int shardingTotalCount,
final String shardingItemParameters) {
JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build();
SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(coreConfig, jobClass.getCanonicalName());
JobScheduler jobScheduler = new JobScheduler(regCenter, LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build());
jobScheduler.init();
}
红色代码为修改后加的代码。
先说说这个dangdang的elastic-job,它是一个分布式任务调度插件。今天我遇到的问题就是,有部分任务,在多节点环境中,不需要每个节点执行,比如只需要一个节点(确切地说就是作业分片总数=1)上运行的任务,这时候elastic-job就是个不错的选择,它可以很灵活的配置作业分片总数等。它的官方文档,链接指向配置说明:http://elasticjob.io/docs/elastic-job-lite/02-guide/config-manual/
那么spring boot中如何集成进它。需要的一个前提条件是zookeeper服务,这个一般项目里都会用到,你只需要连就好了,如果dev或者你们还没用上,可以找个教程安装一下。链接指向在 CentOS7 上安装 Zookeeper服务 。
然后.propertis配置文件(yml类同):
regCenter.serverList=10.0.30.140:2181
regCenter.namespace=elastic-job simpleJob.cron=0/5 * * * * ?
# 作业分片总数,设为1只在一个节点执行
simpleJob.shardingTotalCount=1
simpleJob.shardingItemParameters=0=A,1=B,2=C
当然,pom中需要引入elastic-job:
<dependency>
<artifactId>elastic-job-common-core</artifactId>
<groupId>com.dangdang</groupId>
<version>${elastic-job.version}</version>
</dependency>
<dependency>
<artifactId>elastic-job-lite-core</artifactId>
<groupId>com.dangdang</groupId>
<version>${elastic-job.version}</version>
</dependency>
<dependency>
<artifactId>elastic-job-lite-spring</artifactId>
<groupId>com.dangdang</groupId>
<version>${elastic-job.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<version>${curator.version}</version>
</dependency>
本示例代码用到的版本是:
<elastic-job.version>2.1.5</elastic-job.version>
<curator.version>2.10.0</curator.version>
连接zookeeper注册中心:
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author binhy
*@date 2019-1-22
*/
@Configuration
@ConditionalOnExpression("'${regCenter.serverList}'.length() > 0")
public class RegistryCenterConfig { @Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter(@Value("${regCenter.serverList}") final String serverList, @Value("${regCenter.namespace}") final String namespace) {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
}
}
然后做一个任务信息持久化:
import javax.annotation.Resource;
import javax.sql.DataSource; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration; /**
* @author binhy
*@date 2019-1-22
*/
@Configuration
public class JobEventConfig { @Resource
private DataSource dataSource; @Bean
public JobEventConfiguration jobEventConfiguration() {
return new JobEventRdbConfiguration(dataSource);
}
}
当你最后运行后会发现你的库里多了两张表job_execution_log和job_status_trace_log他们会详细的记录你的任务执行信息,包括执行ip,开始结束时间等,还是非常不错的。
然后需要一个任务管理类,初始化一些任务,我这里把动态添加任务的方法也写在了这里。因为这种形式需要你一个任务写一个配置和类去实现。动态添加会省很多事。
import com.goopal.exdata.dangdang.DemoJob;
import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; /**
* @author binhy
* @date 2019-1-22
*/
@Configuration
public class SimpleJobConfig { @Resource
private ZookeeperRegistryCenter regCenter; @Resource
private JobEventConfiguration jobEventConfiguration; @Bean
public SimpleJob simpleJob() {
return new DemoJob();
} @Bean(initMethod = "init")
public JobScheduler simpleJobScheduler(final SimpleJob simpleJob, @Value("${simpleJob.cron}") final String cron, @Value("${simpleJob.shardingTotalCount}") final int shardingTotalCount,
@Value("${simpleJob.shardingItemParameters}") final String shardingItemParameters) {
return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters), jobEventConfiguration);
} 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();
} /**
* 动态添加
* @param jobClass
* @param cron
* @param shardingTotalCount
* @param shardingItemParameters
*/
public void addJobScheduler(final Class<? extends SimpleJob> jobClass,
final String cron,
final int shardingTotalCount,
final String shardingItemParameters) {
JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build();
SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(coreConfig, jobClass.getCanonicalName());
JobScheduler jobScheduler = new JobScheduler(regCenter, LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build());
jobScheduler.init();
}
}
到这里,你在配置文件中配置的定时任务就已经可以在多节点环境中,仅在1个节点执行了。需要添加更多的不同cron的任务,只需要在代码业务逻辑处调用即可。如:
@Autowired
private SimpleJobConfig simpleJobConfig;
@Override
public void run(ApplicationArguments args) throws Exception { simpleJobConfig.addJobScheduler(AnalysisData.class,"0/3 * * * * ?",3,"0=A,1=B,2=C");//0=A,1=B,2=C
}
如果有帮助到你,给我点个赞哦~
elastic-job集成到springboot教程,和它的一个异常处理办法:Sharding item parameters '1' format error, should be int=xx,int=xx的更多相关文章
- Spring Data JPA系列2:SpringBoot集成JPA详细教程,快速在项目中熟练使用JPA
大家好,又见面了. 这是Spring Data JPA系列的第2篇,在上一篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring Data JPA,傻傻分不清楚?给你个 ...
- SpringBoot教程——检视阅读
SpringBoot教程--检视阅读 参考 SpringBoot教程--一点--蓝本--springboot2.1.1 SpringBoot教程--易百--springboo2.0.5.RELEASE ...
- 移动应用开发测试工具Bugtags集成和使用教程
前段时间,有很多APP突然走红,最终却都是樱花一现.作为一个创业团队,突然爆红是非常难得的机会.然并卵,由于没有经过充分的测试,再加上用户的激增,APP闪退.服务器数据异常等问题就被暴露出来,用户的流 ...
- 移动应用开发测试工具Bugtags集成和使用教程【转载】
前段时间,有很多APP突然走红,最终却都是樱花一现.作为一个创业团队,突然爆红是非常难得的机会.然并卵,由于没有经过充分的测试,再加上用户的激增,APP闪退.服务器数据异常等问题就被暴露出来,用户的流 ...
- SpringBoot | 教程
Spring Boot 2.0(一):[重磅]Spring Boot 2.0权威发布 Spring Boot 2.0(二):Spring Boot 2.0尝鲜-动态 Banner Spring Boo ...
- SpringBoot系列教程web篇之自定义异常处理HandlerExceptionResolver
关于Web应用的全局异常处理,上一篇介绍了ControllerAdvice结合@ExceptionHandler的方式来实现web应用的全局异常管理: 本篇博文则带来另外一种并不常见的使用方式,通过实 ...
- SpringBoot系列教程web篇之全局异常处理
当我们的后端应用出现异常时,通常会将异常状况包装之后再返回给调用方或者前端,在实际的项目中,不可能对每一个地方都做好异常处理,再优雅的代码也可能抛出异常,那么在 Spring 项目中,可以怎样优雅的处 ...
- SpringBoot教程(学习资源)
SpringBoot教程 SpringBoot–从零开始学SpringBoot SpringBoot教程1 SpringBoot教程2 --SpringBoot教程2的GitHub地址 SpringB ...
- springboot2.x基础教程:动手制作一个starter包
上一篇博客介绍了springboot自动装配的原理.springboot本身有丰富的spring-boot-starter-xx集成组件,这一篇趁热打铁加深理解,我们利用springboot自动装配的 ...
随机推荐
- 2017-10-29—英语发音的一些技巧总结
学习了这么多年英语还是一句口语也说不出口,大家一定像我一样有hin多的f*k想说. 在很小的时候我们就学了英语音标,知道了有前元音.中元音.后元音(很多同志多年不用应该已经把这些忘得差不多了,like ...
- java @Override 报错解决
有时候Java的Eclipse工程换一台电脑后编译总是@override报错,把@override去掉就好了,但不能从根本上解决问题,因为有时候有@override的地方超级多. 这是jdk的问题,@ ...
- 关于finally代码块是否一定被执行的问题
一般来说,只要执行了try语句,finally就会执行 但是,有以下几种情况需要特殊考虑 具体例子看链接 点击这里 第一点 try代码块没有被执行,意思就是错误在try代码块之前就发生了. 第二点 ...
- postgresql----JOIN之多表查询
JOIN用于多张表的关联查询,如SELECT子句(SELECT A.a,A.b,B.a,B.d)中既有表A的字段,同时还有B表的字段,此时使用单独使用FROM A或FROM B已经解决不了问题了,使用 ...
- Ringo替换Paul
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'goodsController' defined in file [D:\eclipse\eclipse-space\pinyougou_parent\pinyou
由于错误太宽,没法截取完整的,所以不怎么连贯,但是不影响错误的解决. 这个错误是因为service无法自动注入.显示嵌套状态异常. 我就查看了一下我的坐标和配置文件,配置文件的路径和访问地址都是正确的 ...
- Install sublime text for elementary os
1. download sublime_text_3_build_3176_x86.tar.gz from http://www.sublimetext.com/3 2. extract it to ...
- python中剔除字典重复项,可以使用集合(set)。
使用集合(set)剔除字典中的重复项(value). 1)具体例子: #甲乙丙丁使用的编程语言programming_languages = { '甲':'java', '乙':'python', ' ...
- 2019OO第一单元总结
第一次作业 (你没看错,就一个类...) 通过正则表达式处理输入的字符串,提取出每一项的系数和指数,在输出的时候,应当考虑到合并同类项和正项提前的问题,使得最终的输出最短. 我第一次作业的代码超级难看 ...
- iostat查看io情况
查看TPS和吞吐量信息[root@controller ~]#iostat -d -k 1 10Device: tps kB_read/s kB_wrtn/s k ...