spring-batch StagingItemWriter 源码

  • 2022-08-16
  • 浏览 (246)

spring-batch StagingItemWriter 代码

文件路径:/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/StagingItemWriter.java

/*
 * Copyright 2006-2019 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.sample.common;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.ListIterator;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ItemWriter;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.SerializationUtils;

/**
 * Database {@link ItemWriter} implementing the process indicator pattern.
 */
public class StagingItemWriter<T> extends JdbcDaoSupport implements StepExecutionListener, ItemWriter<T> {

	protected static final String NEW = "N";

	protected static final String DONE = "Y";

	private DataFieldMaxValueIncrementer incrementer;

	private StepExecution stepExecution;

	/**
	 * Check mandatory properties.
	 *
	 * @see org.springframework.dao.support.DaoSupport#initDao()
	 */
	@Override
	protected void initDao() throws Exception {
		super.initDao();
		Assert.notNull(incrementer, "DataFieldMaxValueIncrementer is required - set the incrementer property in the "
				+ ClassUtils.getShortName(StagingItemWriter.class));
	}

	/**
	 * Setter for the key generator for the staging table.
	 * @param incrementer the {@link DataFieldMaxValueIncrementer} to set
	 */
	public void setIncrementer(DataFieldMaxValueIncrementer incrementer) {
		this.incrementer = incrementer;
	}

	/**
	 * Serialize the item to the staging table, and add a NEW processed flag.
	 *
	 * @see ItemWriter#write(java.util.List)
	 */
	@Override
	public void write(final List<? extends T> items) {
		final ListIterator<? extends T> itemIterator = items.listIterator();

		getJdbcTemplate().batchUpdate("INSERT into BATCH_STAGING (ID, JOB_ID, VALUE, PROCESSED) values (?,?,?,?)",
				new BatchPreparedStatementSetter() {
					@Override
					public int getBatchSize() {
						return items.size();
					}

					@Override
					public void setValues(PreparedStatement ps, int i) throws SQLException {
						Assert.state(itemIterator.nextIndex() == i,
								"Item ordering must be preserved in batch sql update");

						ps.setLong(1, incrementer.nextLongValue());
						ps.setLong(2, stepExecution.getJobExecution().getJobId());
						ps.setBytes(3, SerializationUtils.serialize(itemIterator.next()));
						ps.setString(4, NEW);
					}
				});
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.springframework.batch.core.domain.StepListener#afterStep(StepExecution )
	 */
	@Nullable
	@Override
	public ExitStatus afterStep(StepExecution stepExecution) {
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.springframework.batch.core.domain.StepListener#beforeStep(org.
	 * springframework.batch.core.domain.StepExecution)
	 */
	@Override
	public void beforeStep(StepExecution stepExecution) {
		this.stepExecution = stepExecution;
	}

}

相关信息

spring-batch 源码目录

相关文章

spring-batch ColumnRangePartitioner 源码

spring-batch InfiniteLoopReader 源码

spring-batch InfiniteLoopWriter 源码

spring-batch LogAdvice 源码

spring-batch OutputFileListener 源码

spring-batch OutputFileNameListener 源码

spring-batch ProcessIndicatorItemWrapper 源码

spring-batch StagingItemListener 源码

spring-batch StagingItemProcessor 源码

spring-batch StagingItemReader 源码

0  赞