001/* 002 * Copyright 2013 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.springframework.batch.jsr.repeat; 017 018import javax.batch.api.chunk.CheckpointAlgorithm; 019import javax.batch.operations.BatchRuntimeException; 020 021import org.springframework.batch.repeat.CompletionPolicy; 022import org.springframework.batch.repeat.RepeatContext; 023import org.springframework.batch.repeat.RepeatStatus; 024import org.springframework.util.Assert; 025 026/** 027 * Wrapper for the {@link CheckpointAlgorithm} to be used via the rest 028 * of the framework. 029 * 030 * @author Michael Minella 031 * @see CheckpointAlgorithm 032 * @see CompletionPolicy 033 */ 034public class CheckpointAlgorithmAdapter implements CompletionPolicy { 035 036 private CheckpointAlgorithm policy; 037 private boolean isComplete = false; 038 039 public CheckpointAlgorithmAdapter(CheckpointAlgorithm policy) { 040 Assert.notNull(policy, "A CheckpointAlgorithm is required"); 041 042 this.policy = policy; 043 } 044 045 /* (non-Javadoc) 046 * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext, org.springframework.batch.repeat.RepeatStatus) 047 */ 048 @Override 049 public boolean isComplete(RepeatContext context, RepeatStatus result) { 050 try { 051 isComplete = policy.isReadyToCheckpoint(); 052 return isComplete; 053 } catch (Exception e) { 054 throw new BatchRuntimeException(e); 055 } 056 } 057 058 /* (non-Javadoc) 059 * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext) 060 */ 061 @Override 062 public boolean isComplete(RepeatContext context) { 063 try { 064 isComplete = policy.isReadyToCheckpoint(); 065 return isComplete; 066 } catch (Exception e) { 067 throw new BatchRuntimeException(e); 068 } 069 } 070 071 /* (non-Javadoc) 072 * @see org.springframework.batch.repeat.CompletionPolicy#start(org.springframework.batch.repeat.RepeatContext) 073 */ 074 @Override 075 public RepeatContext start(RepeatContext parent) { 076 try { 077 policy.beginCheckpoint(); 078 } catch (Exception e) { 079 throw new BatchRuntimeException(e); 080 } 081 082 return parent; 083 } 084 085 /** 086 * If {@link CheckpointAlgorithm#isReadyToCheckpoint()} is true 087 * we will call {@link CheckpointAlgorithm#endCheckpoint()} 088 * 089 * @param context a {@link RepeatContext} 090 */ 091 @Override 092 public void update(RepeatContext context) { 093 try { 094 if(isComplete) { 095 policy.endCheckpoint(); 096 } 097 } catch (Exception e) { 098 throw new BatchRuntimeException(e); 099 } 100 } 101}