스프링 배치를 사용하여 작성한 일괄 처리의 다양한 조건에 대한 다양한 예외를 추적해야하는 시나리오를 발견했습니다. 예 : 데이터베이스를 읽을 수없는 경우 특정 유형의 예외를 throw하고 데이터베이스를 사용할 수 없다는 메일을 보내고 일괄 처리를 종료하십시오. 테이블을 사용할 수없는 경우 다른 예외를 throw하고 메일을 보내면 테이블을 사용할 수없고 배치를 종료합니다. 그리고 데이터가 SQL 문에 지정된 조건을 충족시키지 않으면 정상적인 작업 종료이므로 아무 것도하지 않습니다. 지금까지 StepExecutionListener를 사용하여 일괄 처리가 모든 레코드를 읽었는지 또는 failureException인지는 알 수 있지만 원하는 방식으로 볼 수는 없습니다. 도움이나 제안 사항이 있습니다.스프링 배치에서 맞춤 예외를 throw하는 방법은 무엇입니까?
내 context.xml에
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<import resource="classpath:context-datasource.xml" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>springbatch.properties</value>
</property>
</bean>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<!-- ItemReader which reads from database and returns the row mapped by
rowMapper -->
<bean id="databaseItemReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="dataSource" />
<property name="sql" value="SELECT * FROM employee1" />
<property name="rowMapper">
<bean class="com.abc.springbatch.jdbc.EmployeeRowMapper" />
</property>
</bean>
<!-- ItemWriter writes a line into output flat file -->
<bean id="databaseItemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource" />
<property name="sql">
<value>
<![CDATA[
insert into actemployee(empId, firstName, lastName,additionalInfo)
values (?, ?, ?, ?)
]]>
</value>
</property>
<property name="itemPreparedStatementSetter">
<bean class="com.abc.springbatch.jdbc.EmployeePreparedStatementSetter" />
</property>
</bean>
<!-- Optional ItemProcessor to perform business logic/filtering on the input
records -->
<bean id="itemProcessor" class="com.abc.springbatch.EmployeeItemProcessor">
<property name="validator" ref="validator" />
</bean>
<!-- Step will need a transaction manager -->
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="recordSkipListener" class="com.abc.springbatch.RecordSkipListener" />
<bean id="customItemReadListener" class="com.abc.springbatch.CustomItemReadListener" />
<bean id="stepExecutionListener" class="com.abc.springbatch.BatchStepExecutionListner">
<constructor-arg ref="mailSender" />
<constructor-arg ref="preConfiguredMessage" />
</bean>
<!-- Actual Job -->
<batch:job id="employeeToActiveEmployee">
<batch:step id="step1">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="databaseItemReader" writer="databaseItemWriter"
processor="itemProcessor" commit-interval="10" skip-limit="500" retry-limit="5">
<batch:listeners>
<batch:listener ref="customItemReadListener"/>
</batch:listeners>
<!-- Retry included here to retry for specified times in case the following exception occurs -->
<batch:retryable-exception-classes>
<batch:include
class="org.springframework.dao.DeadlockLoserDataAccessException" />
</batch:retryable-exception-classes>
<batch:skippable-exception-classes>
<batch:include class="javax.validation.ValidationException" />
</batch:skippable-exception-classes>
</batch:chunk>
</batch:tasklet>
<batch:listeners>
<batch:listener ref="recordSkipListener" />
<batch:listener ref="stepExecutionListener" />
</batch:listeners>
</batch:step>
</batch:job>
<!-- Email API bean configuarion -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${constant.order.mailHost.response}" />
<property name="port" value="${constant.order.mailPort.response}" />
<property name="username" value="${constant.order.mailUsername.response}" />
<property name="password" value="XXXXXX" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">false</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
<bean id="preConfiguredMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="[email protected]" />
<property name="to" value="[email protected]" />
<property name="subject" value="Skipped Records" />
</bean>
</beans>
<!-- Email API bean configuarion -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${constant.order.mailHost.response}" />
<property name="port" value="${constant.order.mailPort.response}" />
<property name="username" value="${constant.order.mailUsername.response}" />
<property name="password" value="XXXXXX" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">false</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
<bean id="preConfiguredMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="[email protected]" />
<property name="to" value="[email protected]" />
<property name="subject" value="Skipped Records" />
</bean>
StepExecutionListener.java
public class BatchStepExecutionListner implements StepExecutionListener {
private JavaMailSender mailSender;
private SimpleMailMessage simpleMailMessage;
public BatchStepExecutionListner(JavaMailSender mailSender, SimpleMailMessage preConfiguredMessage) {
// TODO Auto-generated constructor stub
this.mailSender = mailSender;
this.simpleMailMessage = preConfiguredMessage;
}
@Override
public void beforeStep(StepExecution stepExecution) {
// TODO Auto-generated method stub
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
// TODO Auto-generated method stub
stepExecution.getReadCount();
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(simpleMailMessage.getFrom());
helper.setTo(simpleMailMessage.getTo());
helper.setSubject(simpleMailMessage.getSubject());
helper.setText("These are the skipped records");
FileSystemResource file = new FileSystemResource("filename.txt");
helper.addAttachment(file.getFilename(), file);
} catch (MessagingException e) {
throw new MailParseException(e);
}
//mailSender.send(message);
return null;
}
}
감사
Google에서 평가하고 개선 할 수 있도록 사용하려고 시도한 코드를 포함시킬 수 있습니까? :-) – Sometowngeek
@Sometowngeek 코드를 넣었습니다. 나는 그것이 도움이되기를 바랍니다. 감사합니다 –
질문을 이해하려고합니다 ... 데이터베이스 서버가 다운되었거나 데이터베이스 스키마의 일부를 사용할 수 없는지 묻는 질문을합니까? – Sometowngeek