我正在使用Spring批处理和Spring数据jpa从日志文件中读取行并将其插入数据库中。但是它没有触发在logLineWriterService中写入方法,因此我无法弄清原因。贝娄是我到目前为止所做的。有人可以帮我吗?
public class Parser {
public static void main(String[] args) {
final ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
LogLineService service = (LogLineService) appContext.getBean(LogLineService.class);
try {
service.saveFileToDB("D:\\Other\\Suranga\\Remote\\Java_MySQL_Test\\access.log");
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Configuration
@EnableBatchProcessing
@EnableTransactionManagement
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public LogLineDao logLineDao;
@Bean
public ResourcelessTransactionManager resourcelessTransactionManager(){
ResourcelessTransactionManager transactionManager = new ResourcelessTransactionManager();
return transactionManager;
}
@Bean
protected JobRepository jobRepository(ResourcelessTransactionManager resourcelessTransactionManager) throws Exception{
MapJobRepositoryFactoryBean jobRepository = new MapJobRepositoryFactoryBean();
jobRepository.setTransactionManager(resourcelessTransactionManager);
return (JobRepository)jobRepository.getObject();
}
@Bean
public SimpleJobLauncher jobLauncher(JobRepository jobRepository) {
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setJobRepository(jobRepository);
return simpleJobLauncher;
}
@Bean
@Scope(value = "step", proxyMode = ScopedProxyMode.TARGET_CLASS)
public FlatFileItemReader<LogLine> fileReader(@Value("#{jobParameters[fullPathFileName]}") String pathToFile) {
FlatFileItemReader<LogLine> reader = new FlatFileItemReader<LogLine>();
reader.setResource(new FileSystemResource(new File(pathToFile)));
reader.setLineMapper(createLogLineMapper());
return reader;
}
/**
*
* @return
*/
private LineMapper<LogLine> createLogLineMapper() {
DefaultLineMapper<LogLine> logLineMapper = new DefaultLineMapper<LogLine>();
LineTokenizer studentLineTokenizer = createStudentLineTokenizer();
logLineMapper.setLineTokenizer(studentLineTokenizer);
FieldSetMapper<LogLine> studentInformationMapper = createLogLineInformationMapper();
logLineMapper.setFieldSetMapper(studentInformationMapper);
return logLineMapper;
}
/**
* @return
*/
private FieldSetMapper<LogLine> createLogLineInformationMapper() {
BeanWrapperFieldSetMapper<LogLine> studentInformationMapper = new BeanWrapperFieldSetMapper<LogLine>();
studentInformationMapper.setTargetType(LogLine.class);
return studentInformationMapper;
}
/**
* @return
*/
private LineTokenizer createStudentLineTokenizer() {
DelimitedLineTokenizer studentLineTokenizer = new DelimitedLineTokenizer();
studentLineTokenizer.setDelimiter("|");
studentLineTokenizer.setNames(new String[]{"date", "ip", "method", "status", "description"});
return studentLineTokenizer;
}
@Bean
public LogLineProcessorService processor() {
return new LogLineProcessorService();
}
@Bean
public ItemWriter<LogLine> writer() throws Exception {
return new LogLineWriterService();
}
@Bean
public Job importUserJob(@Qualifier("fileReader") ItemReader<LogLine> fileReader) throws Exception{
return jobBuilderFactory.get("importUserJob")
.incrementer(new RunIdIncrementer())
.flow(step1(fileReader))
.end()
.build();
}
@Bean
public Step step1(@Qualifier("fileReader") ItemReader<LogLine> fileReader) throws Exception{
return stepBuilderFactory.get("step1")
.<LogLine, LogLine> chunk(1)
.reader(fileReader)
.processor(processor())
.writer(writer())
.build();
}
}
public interface LogLineDao extends CrudRepository<LogLine,Long> {
}
@Service
public class LogLineProcessorService implements ItemProcessor<LogLine, LogLine>{
public LogLine process(LogLine logLine) throws Exception {
System.out.println("Inside process....");
return logLine;
}
}
@Service
public class LogLineService {
@Autowired private JobLauncher jobLauncher;
@Autowired private Job importUserJob;
public void saveFileToDB(String filePath) throws Exception, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException{
try{
JobExecution jobExecution = jobLauncher.run(importUserJob, new JobParametersBuilder().addString("fullPathFileName", filePath).toJobParameters());
System.out.println(jobExecution.getStatus());
System.out.println(jobExecution.getFailureExceptions());
}catch(Throwable t){
t.printStackTrace();
}
}
}
@Transactional
@Service
public class LogLineWriterService implements ItemWriter<LogLine>{
@Autowired
private LogLineDao logLineDao;
public void write(List<? extends LogLine> items) throws Exception {
logLineDao.save(items);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<description>Example configuration to get you started.</description>
<context:component-scan base-package="com.ef.parser" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:config.properties</value>
</list>
</property>
</bean>
<bean id="log4jInitialization"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>classpath:META-INF/spring/log4j.xml</value>
</list>
</property>
</bean>
<bean id="mainDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${db.driver}" />
<property name="jdbcUrl" value="${db.url}" />
<property name="user" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maxPoolSize" value="10" />
<property name="minPoolSize" value="5" />
<property name="maxStatements" value="100" />
<property name="testConnectionOnCheckout" value="true" />
</bean>
<!-- SPRING - JPA -->
<jpa:repositories base-package="com.ef.parser.dao" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="mainDataSource" />
<property name="packagesToScan" value="com.ef.parser.domain" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
<property name="jpaProperties">
<value>
hibernate.cache.use_second_level_cache = true
hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory
hibernate.cache.use_query_cache = true
hibernate.generate_statistics = true
</value>
</property>
</bean>
</beans>