Spring Batch代碼塊級別的重試

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 happen1
INFO [com.test.springbatch.RetryTest] - exception happen2
INFO [com.test.springbatch.RetryTest] - exception happen3
INFO [com.test.springbatch.RetryTest] - exception happen4
INFO [com.test.springbatch.RetryTest] - here5
INFO [com.test.springbatch.RetryTest] - 成功

第五次執行成功,符合我們設想的結果。

如果把重試次數改爲4,會發生什麼呢。

policy.setMaxAttempts(4);
執行結果如下:

INFO [com.test.springbatch.RetryTest] - exception happen1
INFO [com.test.springbatch.RetryTest] - exception happen2
INFO [com.test.springbatch.RetryTest] - exception happen3
INFO [com.test.springbatch.RetryTest] - exception happen4
java.lang.Exception: exception happen4
	at 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次發生了異常之後,馬上就把異常拋給了我們自己定義的異常處理。

代碼級的重試給了我們很高的靈活度,把某些比較容易出錯的代碼放入其中,能很好的增強我們代碼的健壯性。

發佈了55 篇原創文章 · 獲贊 6 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章