1

Main application:-

@SpringBootApplication
@Configuration
public class Application {      
    public static void main(String[] args) {      
        new SpringApplicationBuilder()
          .web(false)
          .sources(Application.class)
          .run(args);
    }
}

FilePollingIntegrationFlow class:-

@Configuration
class FilePollingIntegrationFlow {

    @Autowired
    private ApplicationContext applicationContext;
    //It does the file polling part:-       

    @Bean
    public IntegrationFlow inboundFileIntegration(@Value("${inbound.file.poller.fixed.delay}") long period,
        @Value("${inbound.file.poller.max.messages.per.poll}") int maxMessagesPerPoll,
        TaskExecutor taskExecutor,
        MessageSource < File > fileReadingMessageSource) {
        // JobLaunchingGateway jobLaunchingGateway
        return IntegrationFlows.from(fileReadingMessageSource,
                c - > c.poller(Pollers.fixedDelay(period)
                    .taskExecutor(taskExecutor)
                    .maxMessagesPerPoll(maxMessagesPerPoll)))
            .transform(Transformers.fileToString())
            .channel(ApplicationConfiguration.INBOUND_CHANNEL)
            .channel(MessageChannels.queue("fileReadingResultChannel"))
            .get();
    }

    @Bean
    TaskExecutor taskExecutor(@Value("${inbound.file.poller.thread.pool.size}") int poolSize) {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(poolSize);
        return taskExecutor;
    }

    @Bean
    public FileReadingMessageSource fileReadingMessageSource(@Value("${inbound.filename.regex}") String regex, @Value("${inbound.directory.location}") String directory) {
        FileReadingMessageSource source = new FileReadingMessageSource();
        source.setDirectory(new File(directory));
        source.setAutoCreateDirectory(true);
        CompositeFileListFilter < File > filter = new CompositeFileListFilter < > (
            Arrays.asList(new AcceptOnceFileListFilter < File > (),
                new RegexPatternFileListFilter(regex))
        );
        source.setFilter(filter);
        return source;
    }
}

How to trigger a thread to check the system time with a fixed time in a day(say 5pm est), if sysyem time equals fixed time then System.exit(1) in a spring integration project using java dsl and spring boot.

I am not able to figure

  1. how to exit from Integrationflows and move to another program or method.
  2. Run a separate thread or on top of the same thread which does polling to achieve my functionality.

Please advice.

madteapot
  • 2,208
  • 2
  • 19
  • 32
Prerna
  • 111
  • 1
  • 16
  • Your question is unclear. And looks like this file polling is misleading. Nobody can help you if you don't provide the clear requirements. – Artem Bilan Jan 25 '18 at 19:44
  • Requirement is:- I need to poll for files in a given directory , any time a new file comes in the directory a message is received(logged), once a message is logged, a spring batch job is triggered , also in a separate thread it keeps checking the system time with the a fixed time(say 5pm), once the system time is greater then the fixed time , the program exits. – Prerna Jan 25 '18 at 19:50
  • Good, but still bad. The file polling isn't related to the exit task. See Gary's answer. – Artem Bilan Jan 25 '18 at 19:55
  • can you please look at my integration flow and tell me what mistake i am doing ? what is the role of .channel(MessageChannels.queue("fileReadingResultChannel")). – Prerna Jan 25 '18 at 20:33
  • No, I don't say that your logic in the app is wrong. That is definitely good and may be like you show it. IMO your mistake that checking system time isn't related to the file polling. The error in the properly formed question here on SO. We don't know your app and therefore misleading info confuses us. Not sure why you don't proceed with Gary's answer. – Artem Bilan Jan 25 '18 at 20:39
  • Thanks Artem , proceeded with Gary's answer, it helped – Prerna Jan 25 '18 at 20:48

1 Answers1

0

Seems a bit brutal to pull the plug regardless of what's in process but...

@Component
public class Foo {

    @Scheduled(cron = "0 0 17 * * *")
    public void killer() {
        System.exit(1);
    }

}

with @EnableScheduling on the Application class.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179