Spring Batch代码块级别的重试

来源:互联网 发布:micro golang 编辑:程序博客网 时间:2024/05/17 04:37

Spring Batch的入门实例请参考我的另一篇文章:

http://blog.csdn.net/limiteewaltwo/article/details/8832771

把log4j的输出级别定义为info级别。

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'><appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"><param name="Target" value="System.out" /><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%p [%c] - %m%n" /></layout><!--过滤器设置输出的级别 --><filter class="org.apache.log4j.varia.LevelRangeFilter"><param name="levelMin" value="DEBUG" /><param name="levelMax" value="ERROR" /></filter></appender><!-- 根logger的设置 --><root><priority value="INFO" /><appender-ref ref="CONSOLE"/></root></log4j:configuration>

重试的代码示例:

/** *  */package com.test.springbatch;import java.util.HashMap;import java.util.Map;import org.apache.log4j.Logger;import org.springframework.batch.retry.RetryCallback;import org.springframework.batch.retry.RetryContext;import org.springframework.batch.retry.policy.SimpleRetryPolicy;import org.springframework.batch.retry.support.RetryTemplate;/** * @author hadoop * */public class RetryTest {private static Logger logger = Logger.getLogger(RetryTest.class);/** * @param args */public static void main(String[] args) {/** * SimpleRetryPolicy策略,重试固定的次数,包括第一次执行; */SimpleRetryPolicy policy = new SimpleRetryPolicy();/** * 最多尝试次数,第一次的序号为0,5表示共尝试5次(包括原始的第一次执行)。 */policy.setMaxAttempts(5);Map<Class<? extends Throwable>,Boolean> retryExceptionMap = new HashMap<Class<? extends Throwable>,Boolean>();/** * 设置发生哪些异常时,重试 */retryExceptionMap.put(Exception.class, true);policy.setRetryableExceptions(retryExceptionMap);RetryTemplate template = new RetryTemplate();template.setRetryPolicy(policy);boolean runResult = false;try {runResult = template.execute(new RetryCallback<Boolean>(){private int count = 0;public Boolean doWithRetry(RetryContext context) throws Exception {count++;if(count < 5){logger.info("exception happen" + count);throw new Exception("exception happen" + count);}logger.info("here" + count);return true;}});} catch (Exception e) {e.printStackTrace();}if(runResult){logger.info("成功");}else{logger.info("失败");}}}

设计的情形为,前4次抛出异常,第五次执行成功,执行代码,我们得到如下的输出:

INFO [com.test.springbatch.RetryTest] - exception happen1INFO [com.test.springbatch.RetryTest] - exception happen2INFO [com.test.springbatch.RetryTest] - exception happen3INFO [com.test.springbatch.RetryTest] - exception happen4INFO [com.test.springbatch.RetryTest] - here5INFO [com.test.springbatch.RetryTest] - 成功

第五次执行成功,符合我们设想的结果。

如果把重试次数改为4,会发生什么呢。

policy.setMaxAttempts(4);
执行结果如下:

INFO [com.test.springbatch.RetryTest] - exception happen1INFO [com.test.springbatch.RetryTest] - exception happen2INFO [com.test.springbatch.RetryTest] - exception happen3INFO [com.test.springbatch.RetryTest] - exception happen4java.lang.Exception: exception happen4at com.test.springbatch.RetryTest$1.doWithRetry(RetryTest.java:55)at com.test.springbatch.RetryTest$1.doWithRetry(RetryTest.java:1)at org.springframework.batch.retry.support.RetryTemplate.doExecute(RetryTemplate.java:240)at org.springframework.batch.retry.support.RetryTemplate.execute(RetryTemplate.java:147)at com.test.springbatch.RetryTest.main(RetryTest.java:46)INFO [com.test.springbatch.RetryTest] - 失败

第4次发生了异常之后,马上就把异常抛给了我们自己定义的异常处理。

代码级的重试给了我们很高的灵活度,把某些比较容易出错的代码放入其中,能很好的增强我们代码的健壮性。

原创粉丝点击