Skip to content

BATCH-2108 #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ public ChunkMonitorData(int offset, int chunkSize) {

private ItemReader<?> reader;

public ChunkMonitor() {
this.setExecutionContextName(ChunkMonitor.class.getName());
}

/**
* @param stream the stream to set
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is problematic. While (from what I can see) all the other places we use the short name (MyClass for example), in this case, we use the full class name (org.springframework.batch.MyClass). Changing this would break restarts of this job.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will indeed break job restarts, but this will be the case for the other components as well, because with this PR the default execution context name will be overwritten with the bean name (unless the user has explicitly configured the name property).

Is it not acceptable that job executions started with spring-batch 2.2.x runtime cannot be restarted on spring-batch 3.x runtime?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemStreamReader;
import org.springframework.batch.item.support.AbstractItemStreamItemReader;
import org.springframework.util.ClassUtils;

/**
* {@link ItemStreamReader} with hard-coded input data.
Expand All @@ -24,11 +23,7 @@ public class ExampleItemReader extends AbstractItemStreamItemReader<String> {
private int max = Integer.MAX_VALUE;

public static volatile boolean fail = false;

public ExampleItemReader() {
this.setExecutionContextName(ClassUtils.getShortName(this.getClass()));
}


/**
* @param min the min to set
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void testClose() {
@Test
public void testOpen() {
ExecutionContext executionContext = new ExecutionContext();
executionContext.putInt(ChunkMonitor.class.getName() + ".OFFSET", 2);
executionContext.putInt(monitor.getExecutionContextKey("OFFSET"), 2);
monitor.open(executionContext);
assertEquals(2, count);
assertEquals(0, monitor.getOffset());
Expand All @@ -118,7 +118,7 @@ public String read() throws Exception, UnexpectedInputException, ParseException
}
});
ExecutionContext executionContext = new ExecutionContext();
executionContext.putInt(ChunkMonitor.class.getName() + ".OFFSET", 2);
executionContext.putInt(monitor.getExecutionContextKey("OFFSET"), 2);
monitor.open(executionContext);
}

Expand All @@ -129,7 +129,7 @@ public void testUpdateOnBoundary() {
monitor.update(executionContext);
assertEquals(0, executionContext.size());

executionContext.put(ChunkMonitor.class.getName() + ".OFFSET", 3);
executionContext.put(monitor.getExecutionContextKey("OFFSET"), 3);
monitor.update(executionContext);
assertEquals(0, executionContext.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.springframework.batch.item;

import org.springframework.batch.item.util.ExecutionContextUserSupport;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.util.ClassUtils;

/**
* Empty method implementation of {@link ItemStream}.
Expand All @@ -24,9 +26,17 @@
* @author Dean de Bree
*
*/
public abstract class ItemStreamSupport implements ItemStream {
public abstract class ItemStreamSupport implements ItemStream, BeanNameAware {

private final ExecutionContextUserSupport executionContextUserSupport = new ExecutionContextUserSupport();

private final String defaultName = ClassUtils.getShortName(getClass());

private String name;

public ItemStreamSupport() {
setName(defaultName);
}

/**
* No-op.
Expand Down Expand Up @@ -54,21 +64,38 @@ public void update(ExecutionContext executionContext) {

/**
* The name of the component which will be used as a stem for keys in the
* {@link ExecutionContext}. Subclasses should provide a default value, e.g.
* the short form of the class name.
* {@link ExecutionContext}. By default, the short form of the class name is used.
* If this component is a spring bean, the name of the bean will be used as default.
* Setting this property explicitly overrides this default behavior.
*
* @param name the name for the component
*/
public void setName(String name) {
this.setExecutionContextName(name);
}

/**
* Set the name of the bean in the bean factory that created this bean.
* The bean name will only be used as name of the component in case it hasn't
* already been explicitly set to a value other than the default.
*
* {@link #setName(String)}
* @see BeanNameAware#setBeanName(String)
*/
@Override
public void setBeanName(String name) {
if (defaultName.equals(this.name)) {
setName(name);
}
}

protected void setExecutionContextName(String name) {
this.name = name;
executionContextUserSupport.setName(name);
}

public String getExecutionContextKey(String key) {
return executionContextUserSupport.getKey(key);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> imple
private String fields;
private List<Object> parameterValues;

public MongoItemReader() {
super();
setName(ClassUtils.getShortName(MongoItemReader.class));
}

/**
* Used to perform operations against the MongoDB instance. Also
* handles the mapping of documents to objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ public class Neo4jItemReader<T> extends AbstractPaginatedDataItemReader<T> imple

private ResultConverter resultConverter;

public Neo4jItemReader() {
setName(ClassUtils.getShortName(Neo4jItemReader.class));
}

/**
* The start segment of the cypher query. START is prepended
* to the statement provided and should <em>not</em> be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ public class RepositoryItemReader<T> extends AbstractItemCountingItemStreamItemR

private String methodName;

public RepositoryItemReader() {
setName(ClassUtils.getShortName(RepositoryItemReader.class));
}

/**
* Arguments to be passed to the data providing method.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ public abstract class AbstractPagingItemReader<T> extends AbstractItemCountingIt

private Object lock = new Object();

public AbstractPagingItemReader() {
setName(ClassUtils.getShortName(AbstractPagingItemReader.class));
}

/**
* The current page number.
* @return the current page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ public class HibernateCursorItemReader<T> extends AbstractItemCountingItemStream

private HibernateItemReaderHelper<T> helper = new HibernateItemReaderHelper<T>();

public HibernateCursorItemReader() {
setName(ClassUtils.getShortName(HibernateCursorItemReader.class));
}

private ScrollableResults cursor;

private boolean initialized = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ public class HibernatePagingItemReader<T> extends AbstractPagingItemReader<T>
private Map<String, Object> parameterValues;

private int fetchSize;

public HibernatePagingItemReader() {
setName(ClassUtils.getShortName(HibernatePagingItemReader.class));
}


/**
* The parameter values to apply to a query (map of name:value).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ public class IbatisPagingItemReader<T> extends AbstractPagingItemReader<T> {

private DataSource dataSource;

public IbatisPagingItemReader() {
setName(ClassUtils.getShortName(IbatisPagingItemReader.class));
}

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ public class JdbcCursorItemReader<T> extends AbstractCursorItemReader<T> {

RowMapper rowMapper;

public JdbcCursorItemReader() {
super();
setName(ClassUtils.getShortName(JdbcCursorItemReader.class));
}

/**
* Set the RowMapper to be used for all calls to read().
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> impleme

private int fetchSize = VALUE_NOT_SET;

public JdbcPagingItemReader() {
setName(ClassUtils.getShortName(JdbcPagingItemReader.class));
}

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ public class JpaPagingItemReader<T> extends AbstractPagingItemReader<T> {

private boolean transacted = true;//default value

public JpaPagingItemReader() {
setName(ClassUtils.getShortName(JpaPagingItemReader.class));
}

/**
* Create a query using an appropriate query provider (entityManager OR
* queryProvider).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ public class StoredProcedureItemReader<T> extends AbstractCursorItemReader<T> {

private int refCursorPosition = 0;

public StoredProcedureItemReader() {
super();
setName(ClassUtils.getShortName(StoredProcedureItemReader.class));
}

/**
* Set the RowMapper to be used for all calls to read().
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea

private BufferedReaderFactory bufferedReaderFactory = new DefaultBufferedReaderFactory();

public FlatFileItemReader() {
setName(ClassUtils.getShortName(FlatFileItemReader.class));
}

/**
* In strict mode the reader will throw an exception on
* {@link #open(org.springframework.batch.item.ExecutionContext)} if the input resource does not exist.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ public class FlatFileItemWriter<T> extends AbstractItemStreamItemWriter<T> imple

private boolean append = false;

public FlatFileItemWriter() {
this.setExecutionContextName(ClassUtils.getShortName(FlatFileItemWriter.class));
}

/**
* Assert that mandatory properties (lineAggregator) are set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.springframework.batch.item.support.AbstractItemStreamItemReader;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* Reads items from multiple resources sequentially - resource list is given by {@link #setResources(Resource[])}, the
Expand Down Expand Up @@ -83,10 +82,6 @@ public int compare(Resource r1, Resource r2) {

};

public MultiResourceItemReader() {
this.setExecutionContextName(ClassUtils.getShortName(MultiResourceItemReader.class));
}

/**
* Reads the next item, jumping to next resource if necessary.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import java.io.File;
import java.io.IOException;
import java.util.List;

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.support.AbstractItemStreamItemWriter;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* Wraps a {@link ResourceAwareItemWriterItemStream} and creates a new output
Expand Down Expand Up @@ -63,10 +63,6 @@ public class MultiResourceItemWriter<T> extends AbstractItemStreamItemWriter<T>

private boolean opened = false;

public MultiResourceItemWriter() {
this.setExecutionContextName(ClassUtils.getShortName(MultiResourceItemWriter.class));
}

@Override
public void write(List<? extends T> items) throws Exception {
if (!opened) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ public class ResourcesItemReader extends AbstractItemStreamItemReader<Resource>

private AtomicInteger counter = new AtomicInteger(0);

public ResourcesItemReader() {
/*
* Initialize the name for the key in the execution context.
*/
this.setExecutionContextName(getClass().getName());
}

/**
* The resources to serve up as items. Hint: use a pattern to configure.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe

private boolean strict = true;

public StaxEventItemReader() {
setName(ClassUtils.getShortName(StaxEventItemReader.class));
}

/**
* In strict mode the reader will throw an exception on
* {@link #open(org.springframework.batch.item.ExecutionContext)} if the input resource does not exist.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -156,10 +155,6 @@ public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T> impl
// List holding the QName of elements that were opened in the header callback, but not closed
private List<QName> unclosedHeaderCallbackElements = Collections.EMPTY_LIST;

public StaxEventItemWriter() {
setExecutionContextName(ClassUtils.getShortName(StaxEventItemWriter.class));
}

/**
* Set output file.
*
Expand Down
Loading