把log4j的输出级别定义为info级别。
[html]
< xml version="1.0" encoding="UTF-8" >
< xml version="1.0" encoding="UTF-8" >
重试的代码示例:
[java]
/**
*
*/
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
/**
* 设置发生哪些异常时,重试
*/
retryExceptionMap.put(Exception.class, true);
policy.setRetryableExceptions(retryExceptionMap);
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(policy);
boolean runResult = false;
try {
runResult = template.execute(new RetryCallback
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("失败");
}
}
}
/**
*
*/
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
/**
* 设置发生哪些异常时,重试
*/
retryExceptionMap.put(Exception.class, true);
policy.setRetryableExceptions(retryExceptionMap);
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(policy);
boolean runResult = fal