Spring Batch, le launcher renvoie null
Bonjour,
J'essaye d’intégrer Spring batch dans un projet client, qui repose sur spring boot, spring security, mvc et jpa pour la persistance, par contre j'y arrive toujours pas,
Mon code est comme ceci :
la classe de configuration de spring batch
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
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.explore.JobExplorer;
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
import org.springframework.batch.core.job.SimpleJob;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.jta.WebSphereUowTransactionManager;
import fr.cc.suivireco.productionmanagement.dataaccess.api.OrganismeEntity;
@Configuration
@EnableBatchProcessing
@EnableScheduling
public class BatchConfiguratioOrganisme implements InfrastructureConfiguration {
private Logger logger = LoggerFactory.getLogger(BatchConfiguratioOrganisme.class);
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
private Environment env;
private SimpleJobLauncher jobLauncher;
private JobExplorerFactoryBean jobExplorer;
public DataSource dataSource() {
try {
InitialContext initialContext = new InitialContext();
return (DataSource) initialContext.lookup(this.env.getProperty("datasource.jndi"));
} catch (NamingException e) {
throw new RuntimeException("JNDI lookup failed.", e);
}
}
@Bean
public Job job() {
return this.jobBuilderFactory.get("job").incrementer(new RunIdIncrementer()).flow(step1()).end().build();
}
@Bean
public Step step1() {
return this.stepBuilderFactory.get("step1").<OrganismeCsv, OrganismeEntity> chunk(5).reader(new Reader())
.processor(new Processor()).writer(new Writer().jdbcBatchItemWriter()).build();
}
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
/**
* {@inheritDoc}
*/
@Bean
public JobRepository getJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(dataSource());
factory.setTransactionManager(getTransactionManager());
factory.afterPropertiesSet();
return factory.getObject();
}
/**
* {@inheritDoc}
*/
public PlatformTransactionManager getTransactionManager() throws Exception {
return new WebSphereUowTransactionManager();
}
@Bean
public JobLauncher getJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(getJobRepository());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
public void launch()
throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, Exception {
Job job = new SimpleJob("job");
Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
parameters.put("timestamp", new JobParameter(new Date().getTime()));
JobParameters jobParameters = new JobParameters(parameters);
// JobExecution firstExecution = getJobRepository().createJobExecution(job.getName(), jobParameters);
getJobLauncher().run(job, jobParameters);
}
@Bean
@DependsOn("dataSource")
public JobExplorerFactoryBean jobExplorer() {
this.jobExplorer = new JobExplorerFactoryBean();
this.jobExplorer.setDataSource(dataSource());
return this.jobExplorer;
}
} |
l'interface implémenté
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
/**
* TODO rboughani This type ...
*
* @author rboughani
*/
public interface InfrastructureConfiguration {
@Bean
public abstract DataSource dataSource();
} |
Le controller de lancement du job
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JobLauncherController {
// @Autowired
// JobLauncher jobLauncher;
@Autowired
Job jobs;
@Autowired
private ApplicationContext context;
@RequestMapping("/launchjob")
public String handle() throws Exception {
Logger logger = LoggerFactory.getLogger(this.getClass());
try {
// JobParameters jobParameters =
// new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();
BatchConfiguratioOrganisme batchConfiguratioOrganisme = new BatchConfiguratioOrganisme();
batchConfiguratioOrganisme.launch();
// ApplicationContext context = new ClassPathXmlApplicationContext("dispatcher-servlet.xml");
// JobLauncher jobLauncher = (JobLauncher) this.context.getBean("getJobLauncher");
// jobLauncher.run(this.jobs, jobParameters);
} catch (Exception e) {
logger.warn(e.getMessage());
}
return "Done";
}
} |
processeur
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.transaction.annotation.Transactional;
import fr.cc.suivireco.productionmanagement.dataaccess.api.OrganismeEntity;
public class Processor implements ItemProcessor<OrganismeCsv, OrganismeEntity> {
private static Logger logger = LoggerFactory.getLogger(Processor.class);
@Override
@Transactional
public OrganismeEntity process(OrganismeCsv organismeCsv) throws Exception {
OrganismeEntity organisme = new OrganismeEntity();
organisme.setCRCAriane(organismeCsv.getOr_crc());
organisme.setLibelle(organismeCsv.getOr_nom());
organisme.setORIDAriane(organismeCsv.getOr_id());
organisme.setDepartement(organismeCsv.getOr_dpt());
organisme.setNatureJuridique(organismeCsv.getOr_nat());
// if (organismeCsv.getOr_typ().contains(" "))
// organisme.setTypeOrganismeId(organismeCsv.getOr_typ());
// else
// organisme.setTypeOrganismeId(null);
organisme.setLoiNotre(Character.toChars(Integer.parseInt(organismeCsv.getOr_crc().toString()))[0]);
logger.error("Convert orgnaisme {} +=| - |=+ {}", organisme.getLibelle(), organisme.getCRCAriane());
return organisme;
}
} |
le lecteur
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.core.io.ClassPathResource;
public class Reader implements ItemReader<OrganismeCsv> {
private int count = 0;
Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public OrganismeCsv read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
FlatFileItemReader<OrganismeCsv> reader = new FlatFileItemReader<OrganismeCsv>();
OrganismeCsv organismeCSV = new OrganismeCsv();
reader.setResource(new ClassPathResource("organismesDelta2016-12-19.csv"));
reader.setLineMapper(new DefaultLineMapper<OrganismeCsv>() {
{
setLineTokenizer(new DelimitedLineTokenizer() {
{
setNames(new String[] { "or_crc", "or_nom", "or_id", "or_dpt", "or_nat", "or_typ" });
setDelimiter("||");
}
});
setFieldSetMapper(new PlayerFieldSetMapper());
}
});
reader.setLinesToSkip(1);
reader.open(new ExecutionContext());
organismeCSV = reader.read();
reader.close();
return organismeCSV;
}
public static class PlayerFieldSetMapper implements FieldSetMapper<OrganismeCsv> {
public OrganismeCsv mapFieldSet(FieldSet fieldSet) {
OrganismeCsv player = new OrganismeCsv();
player.setOr_crc(Long.parseLong(fieldSet.readString(0)));
player.setOr_nom(fieldSet.readString(1));
player.setOr_id(Long.parseLong(fieldSet.readString(2)));
player.setOr_dpt(Long.parseLong(fieldSet.readString(3)));
player.setOr_nat(fieldSet.readString(4));
player.setOr_typ(fieldSet.readString(5));
return player;
}
}
} |
le writer
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
import org.springframework.batch.item.database.JpaItemWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import fr.cc.suivireco.productionmanagement.dataaccess.api.OrganismeEntity;
public class Writer extends JpaItemWriter<OrganismeEntity> implements InitializingBean {
private static final String REQUEST_INSERT_PERSONNE =
"insert into ORGANISME (ID,CRC_ARIANE,OR_ID_ARIANE,LIBELLE,NATURE_JURIDIQUE,DEPARTEMENT,LOI_NOTRE,ID_PRODUCTION,MODIFICATIONCOUNTER,TYPEORGANISME_ID) values (?,?,?,?,?,?,?,?,?,?)";
public Writer() {
}
@Bean
public JpaItemWriter<OrganismeEntity> jdbcBatchItemWriter() {
// Instantiate to get the Entity Manager Factory
BatchConfiguratioOrganisme batchConfiguratioOrganisme = new BatchConfiguratioOrganisme();
JpaItemWriter<OrganismeEntity> itemWriter = new JpaItemWriter<OrganismeEntity>();
// itemWriter.setEntityManagerFactory(batchConfiguratioOrganisme.getTransactionManager().getTransaction());
return itemWriter;
}
} |
l'entite qui correcpond aux fichier csv
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
public class OrganismeCsv {
Long or_crc;
String or_nom;
Long or_id;
Long or_dpt;
String or_nat;
String or_typ;
/**
* @return or_crc
*/
public Long getOr_crc() {
return this.or_crc;
}
/**
* @param or_crc the or_crc to set
*/
public void setOr_crc(Long or_crc) {
this.or_crc = or_crc;
}
/**
* @return or_nom
*/
public String getOr_nom() {
return this.or_nom;
}
/**
* @param or_nom the or_nom to set
*/
public void setOr_nom(String or_nom) {
this.or_nom = or_nom;
}
/**
* @return or_id
*/
public Long getOr_id() {
return this.or_id;
}
/**
* @param or_id the or_id to set
*/
public void setOr_id(Long or_id) {
this.or_id = or_id;
}
/**
* @return or_dpt
*/
public Long getOr_dpt() {
return this.or_dpt;
}
/**
* @param or_dpt the or_dpt to set
*/
public void setOr_dpt(Long or_dpt) {
this.or_dpt = or_dpt;
}
/**
* @return or_nat
*/
public String getOr_nat() {
return this.or_nat;
}
/**
* @param or_nat the or_nat to set
*/
public void setOr_nat(String or_nat) {
this.or_nat = or_nat;
}
/**
* @return or_typ
*/
public String getOr_typ() {
return this.or_typ;
}
/**
* @param or_typ the or_typ to set
*/
public void setOr_typ(String or_typ) {
this.or_typ = or_typ;
}
} |
l'entité persistante
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/**
* Ce type est l'entite permettant de gerer les donnees de referentiel
*
* @author gcartier
*/
@Entity(name = "OrganismeEntity")
@Table(name = "organisme")
public class OrganismeEntity extends AbstractPersistenceEntity implements Organisme {
private static final long serialVersionUID = 1L;
private Long CRCAriane;
private Long ORIDAriane;
private String libelle;
private String natureJuridique;
private TypeOrganismeEntity typeOrganisme;
private Long departement;
private Character loiNotre;
private String idProduction;
/**
* @return cRCAriane
*/
@Column(name = "CRC_ARIANE")
public Long getCRCAriane() {
return this.CRCAriane;
}
/**
* @param cRCAriane the cRCAriane to set
*/
public void setCRCAriane(Long cRCAriane) {
this.CRCAriane = cRCAriane;
}
/**
* @return oRIDAriane
*/
@Column(name = "OR_ID_ARIANE")
public Long getORIDAriane() {
return this.ORIDAriane;
}
/**
* @param oRIDAriane the oRIDAriane to set
*/
public void setORIDAriane(Long oRIDAriane) {
this.ORIDAriane = oRIDAriane;
}
/**
* @return libelle
*/
@Column(name = "LIBELLE")
public String getLibelle() {
return this.libelle;
}
/**
* @param libelle the libelle to set
*/
public void setLibelle(String libelle) {
this.libelle = libelle;
}
/**
* @return natureJuridique
*/
@Column(name = "NATURE_JURIDIQUE")
public String getNatureJuridique() {
return this.natureJuridique;
}
/**
* @param natureJuridique the natureJuridique to set
*/
public void setNatureJuridique(String natureJuridique) {
this.natureJuridique = natureJuridique;
}
/**
* @return type
*/
@ManyToOne(fetch = FetchType.LAZY)
public TypeOrganismeEntity getTypeOrganisme() {
return this.typeOrganisme;
}
/**
* @param type the type to set
*/
public void setTypeOrganisme(TypeOrganismeEntity typeOrganisme) {
this.typeOrganisme = typeOrganisme;
}
@Transient
public Long getTypeOrganismeId() {
if (this.typeOrganisme == null) {
return null;
}
return this.typeOrganisme.getId();
}
/**
* {@inheritDoc}
*/
@Transient
public void setTypeOrganismeId(Long typeorganismeId) {
if (typeorganismeId == null) {
this.typeOrganisme = null;
} else {
TypeOrganismeEntity typeOrganismeEntity = new TypeOrganismeEntity();
typeOrganismeEntity.setId(typeorganismeId);
this.typeOrganisme = typeOrganismeEntity;
}
}
/**
* @return departement
*/
@Column(name = "DEPARTEMENT")
public Long getDepartement() {
return this.departement;
}
/**
* @param departement the departement to set
*/
public void setDepartement(Long departement) {
this.departement = departement;
}
/**
* @return loiNotre
*/
@Column(name = "LOI_NOTRE")
public Character getLoiNotre() {
return this.loiNotre;
}
/**
* @param loiNotre the loiNotre to set
*/
public void setLoiNotre(Character loiNotre) {
this.loiNotre = loiNotre;
}
/**
* @return idProduction
*/
@Column(name = "ID_PRODUCTION")
public String getIdProduction() {
return this.idProduction;
}
/**
* @param idProduction the idProduction to set
*/
public void setIdProduction(String idProduction) {
this.idProduction = idProduction;
}
} |
le textes du fichier csv
Code:
1 2 3 4
|
or_crc||or_nom||or_id||or_dpt||or_nat||or_typ
5||ASS ||6281||22||Association loi de 1913||Association
5||AURAY||7381||56||EPL||communes |
Quand j’exécute le code, j’obtiens un null,
quelqu'un peut il nous filer une idée ou une astuce pour balayer ça ?
Merci à l'avance