Karthick A.S
Karthick A.S

Reputation: 185

Spring batch step won't stop by itself after finishing

I am trying to upload multiple files in SFTP server using Spring batch and spring integration. For that I am using ThreadPoolTaskExecutor for parallel processing.

Execute the file upload in each process But even if all the files are uploaded successfully in SFTP server, still it's not stopping the process, the program always keeps on running state.

Even if I override JobExecutionListener

@Bean
public JobExecutionListener jobExecutionListener(ThreadPoolTaskExecutor executor) {
    return new JobExecutionListener() {
        private ThreadPoolTaskExecutor taskExecutor = executor;
        @Override
        public void beforeJob(JobExecution jobExecution) {

        }

        @Override
        public void afterJob(JobExecution jobExecution) {
            taskExecutor.shutdown();
        }
    };
}

@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor()
{
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.initialize();
    executor.setCorePoolSize(10);
    executor.setMaxPoolSize(10);
    executor.setThreadNamePrefix("quantum-runtime-worker-thread");
    executor.setWaitForTasksToCompleteOnShutdown(true);
    return executor;
}


@Bean
public Step uploadFiles()
{
    return stepBuilderFactory.get(UPLOAD_FILE_STEP_NAME).tasklet(new Tasklet() {

        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
        {
            log.info("Upload tasklet start executing..");

            resources = resourcePatternResolver.getResources(inputFilesPath);

            for (Resource anInputResource : resources)
            {
                log.info("Incoming file <{}> to upload....", anInputResource.getFilename());
                threadPoolTaskExecutor().execute(new Runnable() {
                    @Override
                    public void run()
                    {
                        File zippedFile = null;
                        try
                        {
                            log.info("Uploading file : {}", anInputResource.getFilename());
                            gateway.upload(zippedFile);
                            log.info("{} file uploaded : {}", anInputResource.getFilename(), zippedFile.delete());
                        }
                        catch (Exception e)
                        {
                            log.error("Error occured while uploading a  file : {} and the exception is {}",
                                    anInputResource.getFilename(), e);

                        }
                    }
                });

            }
            System.out.println("=============POINTER NOT COMMING HERE================");
            return RepeatStatus.FINISHED;
        }
    }).build();
}

@Bean
@ServiceActivator(inputChannel = SFTP_CHANNEL_NAME)
public MessageHandler handler()
{
    SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
    handler.setRemoteDirectoryExpression(new LiteralExpression("/"));
    handler.setFileNameGenerator(new FileNameGenerator() {
        @Override
        public String generateFileName(Message<?> message)
        {
            if (message.getPayload() instanceof File)
            {
                return ((File) message.getPayload()).getName();
            }
            else
            {
                throw new IllegalArgumentException("File expected as payload.");
            }
        }
    });
    return handler;
}

@MessagingGateway
@Component
public interface UploadGateway {

    @Gateway(requestChannel = SFTP_CHANNEL_NAME)
    void upload(File file);
}

Upvotes: 1

Views: 2757

Answers (1)

Karthick A.S
Karthick A.S

Reputation: 185

Now I close the context and is successfully program stops.

ConfigurableApplicationContext context = new SpringApplicationBuilder(QuantumFileUploadApplication.class).web(false).run(args);
    context.close();// it works

Upvotes: 2

Related Questions