Skip to content

Instantly share code, notes, and snippets.

@betray32
Created August 2, 2019 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betray32/d241d2d7b223d34b2374cfd0bb476177 to your computer and use it in GitHub Desktop.
Save betray32/d241d2d7b223d34b2374cfd0bb476177 to your computer and use it in GitHub Desktop.
Ejemplo con secciones del batch todo en el mismo archivo
package cl.poc.scheduler.batch;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.StoredProcedureItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.SqlParameter;
import cl.poc.scheduler.bean.ListadoMaestroNotificaciones;
import cl.poc.scheduler.dao.ListadoMaestroNotificacionesMapper;
import cl.poc.scheduler.properties.CustomYMLFile;
import net.bytebuddy.utility.RandomString;
import oracle.jdbc.OracleTypes;
/**
* Spring batch de notificaciones
*
* @author ccontrerasc
*
*/
@Configuration
@EnableBatchProcessing
public class JobNotificaciones {
/**
* LOG
*/
private static final Log log = LogFactory.getLog(JobNotificaciones.class);
/******************
* Factories
*/
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
private JobCompletionNotificationListener postListener;
/*******************
* DI
*/
@Autowired
private CustomYMLFile properties;
@Autowired
DataSource dataSource;
/***************************************/
/**
* JOB
*
* Punto de entrada para comenzar la ejecucion del batch
*
* @param listener
* @param evaluarData
* @param ejecutarAccion
* @return
*/
@Bean
@Scope("singleton")
public Job executeBatchProcessing(@Qualifier("stepNotification") Step stepNotification) {
return jobBuilderFactory.get(RandomString.make(15)
.toUpperCase()).listener(postListener)
.preventRestart()
.incrementer(new RunIdIncrementer())
.start(stepNotification)
.build();
}
/**
* STEP
*
* Pago PAC
*
* @return
*/
@Bean
public Step stepNotification(
ItemReader<ListadoMaestroNotificaciones> reader,
ItemProcessor<ListadoMaestroNotificaciones, ListadoMaestroNotificaciones> processor,
ItemWriter<ListadoMaestroNotificaciones> writer) {
return stepBuilderFactory.get("stepNotification")
.<ListadoMaestroNotificaciones, ListadoMaestroNotificaciones>chunk(10)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}
/**************************************************************/
/**
* Lee los registros que contengan el estado indicado
*
* @return
*/
@Bean
public ItemReader<ListadoMaestroNotificaciones> read() {
try {
log.info("[READ] Executing...");
StoredProcedureItemReader<ListadoMaestroNotificaciones> reader = new StoredProcedureItemReader<ListadoMaestroNotificaciones>();
reader.setDataSource(dataSource);
// Procedure
reader.setProcedureName(properties.getProcedures().getObtieneMaestro());
SqlParameter[] parameters = { new SqlParameter("P_ESTADO", OracleTypes.VARCHAR), new SqlParameter("OUT_ESTADO", OracleTypes.CURSOR) };
/*
* Le seteo al reader los parametros ya definidos. Ademas le digo que Mapper necesito para
* procesarlo como un objeto y le indico en que posicion esta el cursor
*/
reader.setParameters(parameters);
reader.setRowMapper(new ListadoMaestroNotificacionesMapper());
reader.setRefCursorPosition(3);
return reader;
} catch (Exception e) {
log.error("Error al obtener lista de clientes a procesar, ERROR > ", e);
}
return null;
}
/*********************************************************/
@Bean
public ItemProcessor<ListadoMaestroNotificaciones, ListadoMaestroNotificaciones> processor() {
ItemProcessor<ListadoMaestroNotificaciones, ListadoMaestroNotificaciones> processor = new ItemProcessor<ListadoMaestroNotificaciones, ListadoMaestroNotificaciones>() {
@Override
public ListadoMaestroNotificaciones process(ListadoMaestroNotificaciones item) throws Exception {
log.info("-----------------------------");
log.info("[PROCESS PAYMENT] Executing...");
log.info("-----------------------------");
return null;
}
};
return processor;
}
/*********************************************************/
@Bean
public ItemWriter<ListadoMaestroNotificaciones> writer() {
ItemWriter<ListadoMaestroNotificaciones> itemWriter = new ItemWriter<ListadoMaestroNotificaciones>() {
@Override
public void write(List<? extends ListadoMaestroNotificaciones> items) throws Exception {
log.info("-----------------------------");
log.info("[WRITER PAYMENT] Executing...");
log.info("-----------------------------");
}
};
return itemWriter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment