@@ -1,133 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.jsr.item;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import jakarta.batch.api.chunk.ItemReader;
|
||||
import jakarta.batch.api.chunk.ItemWriter;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStreamException;
|
||||
import org.springframework.batch.item.ItemStreamSupport;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.SerializationUtils;
|
||||
|
||||
/**
|
||||
* Provides support for JSR-352 checkpointing. Checkpoint objects are copied prior
|
||||
* to being added to the {@link ExecutionContext} for persistence by the framework.
|
||||
* If the checkpoint object cannot be copied and further changes occur to the same
|
||||
* instance, side effects may occur. In cases like this, it is recommended that a
|
||||
* copy of the object being acted upon in the reader/writer is returned via the
|
||||
* {@link ItemReader#checkpointInfo()} or {@link ItemWriter#checkpointInfo()} calls.
|
||||
*
|
||||
* @author Michael Minella
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 3.0
|
||||
*/
|
||||
public abstract class CheckpointSupport extends ItemStreamSupport{
|
||||
|
||||
private final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
private final String checkpointKey;
|
||||
|
||||
/**
|
||||
* @param checkpointKey key to store the checkpoint object with in the {@link ExecutionContext}
|
||||
*/
|
||||
public CheckpointSupport(String checkpointKey) {
|
||||
Assert.hasText(checkpointKey, "checkpointKey is required");
|
||||
this.checkpointKey = checkpointKey;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.item.ItemStreamSupport#open(org.springframework.batch.item.ExecutionContext)
|
||||
*/
|
||||
@Override
|
||||
public void open(ExecutionContext executionContext)
|
||||
throws ItemStreamException {
|
||||
try {
|
||||
String executionContextKey = getExecutionContextKey(checkpointKey);
|
||||
Serializable checkpoint = (Serializable) executionContext.get(executionContextKey);
|
||||
doOpen(checkpoint);
|
||||
} catch (Exception e) {
|
||||
throw new ItemStreamException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to open a batch artifact with previously saved checkpoint information.
|
||||
*
|
||||
* @param checkpoint previously saved checkpoint object
|
||||
* @throws Exception thrown by the implementation
|
||||
*/
|
||||
protected abstract void doOpen(Serializable checkpoint) throws Exception;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.item.ItemStreamSupport#update(org.springframework.batch.item.ExecutionContext)
|
||||
*/
|
||||
@Override
|
||||
public void update(ExecutionContext executionContext)
|
||||
throws ItemStreamException {
|
||||
try {
|
||||
executionContext.put(getExecutionContextKey(checkpointKey), deepCopy(doCheckpoint()));
|
||||
} catch (Exception e) {
|
||||
throw new ItemStreamException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to provide a {@link Serializable} representing the current state of the
|
||||
* batch artifact.
|
||||
*
|
||||
* @return the current state of the batch artifact
|
||||
* @throws Exception thrown by the implementation
|
||||
*/
|
||||
protected abstract Serializable doCheckpoint() throws Exception;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.item.ItemStreamSupport#close()
|
||||
*/
|
||||
@Override
|
||||
public void close() throws ItemStreamException {
|
||||
try {
|
||||
doClose();
|
||||
} catch (Exception e) {
|
||||
throw new ItemStreamException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to close the underlying batch artifact
|
||||
*
|
||||
* @throws Exception thrown by the underlying implementation
|
||||
*/
|
||||
protected abstract void doClose() throws Exception;
|
||||
|
||||
private Object deepCopy(Serializable orig) {
|
||||
Object obj = orig;
|
||||
|
||||
try {
|
||||
obj = SerializationUtils.deserialize(SerializationUtils.serialize(orig));
|
||||
} catch (Exception e) {
|
||||
logger.warn("Unable to copy checkpoint object. Updating the instance passed may cause side effects");
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.jsr.item;
|
||||
|
||||
import jakarta.batch.api.chunk.ItemProcessor;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class ItemProcessorAdapter<I, O> implements org.springframework.batch.item.ItemProcessor<I, O> {
|
||||
|
||||
private ItemProcessor delegate;
|
||||
|
||||
public ItemProcessorAdapter(ItemProcessor processor) {
|
||||
Assert.notNull(processor, "An ItemProcessor implementation is required");
|
||||
this.delegate = processor;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public O process(I item) throws Exception {
|
||||
return (O) delegate.processItem(item);
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.jsr.item;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import jakarta.batch.api.chunk.ItemReader;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Adapter that wraps an {@link ItemReader} for use by Spring Batch. All calls are delegated as appropriate
|
||||
* to the corresponding method on the delegate.
|
||||
*
|
||||
* @author Michael Minella
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ItemReaderAdapter<T> extends CheckpointSupport implements org.springframework.batch.item.ItemReader<T> {
|
||||
|
||||
private static final String CHECKPOINT_KEY = "reader.checkpoint";
|
||||
|
||||
private ItemReader delegate;
|
||||
|
||||
/**
|
||||
* @param reader the {@link ItemReader} implementation to delegate to
|
||||
*/
|
||||
public ItemReaderAdapter(ItemReader reader) {
|
||||
super(CHECKPOINT_KEY);
|
||||
Assert.notNull(reader, "An ItemReader implementation is required");
|
||||
this.delegate = reader;
|
||||
setExecutionContextName(ClassUtils.getShortName(delegate.getClass()));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.item.ItemReader#read()
|
||||
*/
|
||||
@Nullable
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T read() throws Exception {
|
||||
return (T) delegate.readItem();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.jsr.item.CheckpointSupport#doClose()
|
||||
*/
|
||||
@Override
|
||||
protected void doClose() throws Exception{
|
||||
delegate.close();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.jsr.item.CheckpointSupport#doCheckpoint()
|
||||
*/
|
||||
@Override
|
||||
protected Serializable doCheckpoint() throws Exception {
|
||||
return delegate.checkpointInfo();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.jsr.item.CheckpointSupport#doOpen(java.io.Serializable)
|
||||
*/
|
||||
@Override
|
||||
protected void doOpen(Serializable checkpoint) throws Exception {
|
||||
delegate.open(checkpoint);
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.jsr.item;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.batch.api.chunk.ItemWriter;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Adapter that wraps an {@link ItemWriter} for use by Spring Batch. All calls are delegated as appropriate
|
||||
* to the corresponding method on the delegate.
|
||||
*
|
||||
* @author Michael Minella
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ItemWriterAdapter<T> extends CheckpointSupport implements org.springframework.batch.item.ItemWriter<T> {
|
||||
|
||||
private static final String CHECKPOINT_KEY = "writer.checkpoint";
|
||||
|
||||
private ItemWriter delegate;
|
||||
|
||||
/**
|
||||
* @param writer a {@link ItemWriter} to delegate calls to
|
||||
*/
|
||||
public ItemWriterAdapter(ItemWriter writer) {
|
||||
super(CHECKPOINT_KEY);
|
||||
Assert.notNull(writer, "An ItemWriter implementation is required");
|
||||
this.delegate = writer;
|
||||
super.setExecutionContextName(ClassUtils.getShortName(delegate.getClass()));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.item.ItemWriter#write(java.util.List)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void write(List<? extends T> items) throws Exception {
|
||||
delegate.writeItems((List<Object>) items);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.jsr.item.CheckpointSupport#doOpen(java.io.Serializable)
|
||||
*/
|
||||
@Override
|
||||
protected void doOpen(Serializable checkpoint) throws Exception {
|
||||
delegate.open(checkpoint);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.jsr.item.CheckpointSupport#doCheckpoint()
|
||||
*/
|
||||
@Override
|
||||
protected Serializable doCheckpoint() throws Exception {
|
||||
Serializable checkpointInfo = delegate.checkpointInfo();
|
||||
return checkpointInfo;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.jsr.item.CheckpointSupport#doClose()
|
||||
*/
|
||||
@Override
|
||||
protected void doClose() throws Exception{
|
||||
delegate.close();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
/**
|
||||
* Components for adapting JSR item based components to Spring Batch.
|
||||
*
|
||||
* @author Michael Minella
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
@NonNullApi
|
||||
package org.springframework.batch.jsr.item;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.jsr.repeat;
|
||||
|
||||
import jakarta.batch.api.chunk.CheckpointAlgorithm;
|
||||
import jakarta.batch.operations.BatchRuntimeException;
|
||||
|
||||
import org.springframework.batch.repeat.CompletionPolicy;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Wrapper for the {@link CheckpointAlgorithm} to be used via the rest
|
||||
* of the framework.
|
||||
*
|
||||
* @author Michael Minella
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @see CheckpointAlgorithm
|
||||
* @see CompletionPolicy
|
||||
*/
|
||||
public class CheckpointAlgorithmAdapter implements CompletionPolicy {
|
||||
|
||||
private CheckpointAlgorithm policy;
|
||||
private boolean isComplete = false;
|
||||
|
||||
public CheckpointAlgorithmAdapter(CheckpointAlgorithm policy) {
|
||||
Assert.notNull(policy, "A CheckpointAlgorithm is required");
|
||||
|
||||
this.policy = policy;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext, org.springframework.batch.repeat.RepeatStatus)
|
||||
*/
|
||||
@Override
|
||||
public boolean isComplete(RepeatContext context, RepeatStatus result) {
|
||||
try {
|
||||
isComplete = policy.isReadyToCheckpoint();
|
||||
return isComplete;
|
||||
} catch (Exception e) {
|
||||
throw new BatchRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext)
|
||||
*/
|
||||
@Override
|
||||
public boolean isComplete(RepeatContext context) {
|
||||
try {
|
||||
isComplete = policy.isReadyToCheckpoint();
|
||||
return isComplete;
|
||||
} catch (Exception e) {
|
||||
throw new BatchRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.repeat.CompletionPolicy#start(org.springframework.batch.repeat.RepeatContext)
|
||||
*/
|
||||
@Override
|
||||
public RepeatContext start(RepeatContext parent) {
|
||||
try {
|
||||
policy.beginCheckpoint();
|
||||
} catch (Exception e) {
|
||||
throw new BatchRuntimeException(e);
|
||||
}
|
||||
|
||||
return parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* If {@link CheckpointAlgorithm#isReadyToCheckpoint()} is true
|
||||
* we will call {@link CheckpointAlgorithm#endCheckpoint()}
|
||||
*
|
||||
* @param context a {@link RepeatContext}
|
||||
*/
|
||||
@Override
|
||||
public void update(RepeatContext context) {
|
||||
try {
|
||||
if(isComplete) {
|
||||
policy.endCheckpoint();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new BatchRuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* APIs for JSR-352 repeat support.
|
||||
*
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
@NonNullApi
|
||||
package org.springframework.batch.jsr.repeat;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
Reference in New Issue
Block a user