1
私は基本的にスプリングバッチが初めてです。私の最初のステップでは、SFTPサーバーからファイルを取得し、それをJSchを使ってローカルディレクトリに転送しています。 2番目のステップでは、ローカルディレクトリのファイルを読み込み、自分のDBに内容を書き込んでいます。問題はsaveWithdrawalTransaction(STEP2)が実行される前記インスタンスが存在するということではないgetRemoteFile(STEP1)スプリングバッチ条件ステップ、適切に実行されていません
設定クラス:
@Qualifier("writer")
@Autowired
JdbcBatchItemWriter<TopUpTransaction> writer;
@Qualifier("reader")
@Autowired
FlatFileItemReader<TopUpTransaction> reader;
@Bean(name="importWithdrawalTransactionJob")
public Job importWithdrawalTransactionJob() throws Exception {
return jobBuilderFactory.get("importWithdrawalTransactionJob")
.incrementer(new RunIdIncrementer())
.start(getRemoteFile()).on(ExitStatus.FAILED.getExitCode()).end()
.next(saveWithdrawalTransaction()).end()
.build();
}
@Bean
public GetRemoteFileTasklet sFtpGetRemoteFilesTasklet() {
return new GetRemoteFileTasklet();
}
@Bean
public Step getRemoteFile() throws Exception {
return stepBuilderFactory.get("getRemoteFile")
.tasklet(sFtpGetRemoteFilesTasklet())
.build();
}
@Bean
public Step saveWithdrawalTransaction() throws Exception {
return stepBuilderFactory.get("saveWithdrawalTransaction")
.<TopUpTransaction, TopUpTransaction> chunk(1)
.reader(reader)
.processor(processor())
.writer(writer)
.build();
}
リーダー:
@Bean
public FlatFileItemReader<TopUpTransaction> reader() throws Exception {
File folder = new File(csvFilePath);
File filesToProcess[] = folder.listFiles();
FlatFileItemReader<TopUpTransaction> reader = new FlatFileItemReader<TopUpTransaction>();
if (filesToProcess.length > 0) {
for (File file : filesToProcess) {
if (file.isFile()) {
System.out.println(file.getName());
reader.setResource(new FileSystemResource(file.getAbsolutePath()));
reader.setLineMapper(new DefaultLineMapper<TopUpTransaction>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[] { "lineIdentifier","transactionDate", "beneAccNum", "beneAccName", "senderName", "senderInfo",
"creditedAmountWithCurrency", "originalAmountWithCurrency", "netAmountWithCurrency",
"charges", "otherInfo1", "otherInfo2",
"ubTransactionId"});
setDelimiter("|");
}});
setFieldSetMapper(new CustomTopUpMapper());
}});
reader.setRecordSeparatorPolicy(new CustomReaderPolicy());
System.out.println("end read");
file.renameTo(new File(file.getName() + "-processed"));
}
}
脚本:
@Bean(name="writer")
public JdbcBatchItemWriter<TopUpTransaction> writer() {
StringBuffer insertWithdrawalTransaction = new StringBuffer();
insertWithdrawalTransaction.append("INSERT INTO top_up_transactions (customer_id,txn_date, bene_acc_num, bene_acc_name,");
insertWithdrawalTransaction.append("sender_name, sender_info, credited_amount, credited_currency,");
insertWithdrawalTransaction.append("original_amount, original_currency,");
insertWithdrawalTransaction.append("net_amount, net_currency, charges, other_info_1, other_info_2,");
insertWithdrawalTransaction.append("ubp_txn_id,status) ");
insertWithdrawalTransaction.append("VALUES (:customerId, :transactionDate,:beneAccNum,");
insertWithdrawalTransaction.append(":beneAccName,:senderName,:senderInfo,:creditedAmount,:creditedAmountCurrency,");
insertWithdrawalTransaction.append(":originalAmount,:originalAmountCurrency,");
insertWithdrawalTransaction.append(":netAmount,:netAmountCurrency,:charges,:otherInfo1,:otherInfo2,");
insertWithdrawalTransaction.append(":ubTransactionId,:status)");
System.out.println("start write");
JdbcBatchItemWriter<TopUpTransaction> writer = new JdbcBatchItemWriter<TopUpTransaction>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<TopUpTransaction>());
writer.setSql(insertWithdrawalTransaction.toString());
writer.setDataSource(dataSource);
System.out.println("end write");
return writer;
}
タスクレット:
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
{
Session session = null;
Channel channel = null;
//deleteLocalFiles();
System.out.println("=====start tasklet=====");
try {
JSch ssh = new JSch();
session = ssh.getSession("username", "host", 22);
session.setPassword("password");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
sftp.get("*.rpt", "src/main/resources/files/");
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
} finally {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
私はどのようにこれについて移動するアイデアを持っていない...私がしようとしているものは、私はどこからでも見つけました。ありがとうございました!
を言及することができますか? –
あなたの春のバッチ構成でstep1が失敗した場合、それはステップ2.ifには到達しません。最初のステップが失敗し、ファイルが利用できないので、ステップ2とステップ3が何をしているのですか? –
私の目標はstep1が失敗した場合です。 step1が完了したら、バッチは続行されます。私もこの試みを試みました.start(getRemoteFile())。on( "COMPLETED")。(saveWithdrawalTransaction())に同じ問題が残っていました@LucaBassoRicci –