Finally decided to use Spring Scheduling as it does not involve lots of coding as well as XML.
This is the bean class where I schedule 2 jobs to run at 5AM & 6AM daily:
package com.fwd.pmap.scheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.fwd.pmap.memberInterfaceFile.CsvReader;
import com.fwd.pmap.memberInterfaceFile.CsvWriter;;
@Component
public class MyBean {
@Scheduled(cron="0 0 5 * * *")
public void importInterfaceFile()
{
CsvReader reader = new CsvReader();
try {
reader.importInterfaceFile();
} catch (Exception e) {
e.printStackTrace();
}
}
@Scheduled(cron="0 0 6 * * *")
public void generateInterfaceFile()
{
CsvWriter writer = new CsvWriter();
try {
writer.generateInterfaceFile();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here's the config class:
package com.fwd.pmap.scheduler;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import com.fwd.pmap.scheduler.MyBean;
@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {
@Bean
public MyBean bean() {
return new MyBean();
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(4);
}
}
And the main class to execute the above:
package main;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import com.fwd.pmap.scheduler.SchedulerConfig;
public class Main {
static Logger LOGGER = LoggerFactory.getLogger(Main.class);
@SuppressWarnings({ "unused", "resource" })
public static void main(String[] args) {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(SchedulerConfig.class);
}
}