1

My application is written with Spring, Hibernate (JPA), JBOSS 9.0.0.GA & JBOSS EAP 6.4. In POM.xml I have specified the packaging to WAR.

I have 2 functions which I'd like to automate:

a. CSV reader - Read from CSV file and update table in DB

package com.fwd.pmap.memberInterfaceFile;

/* all imports */

public class CsvReader
{
    public void importInterfaceFile() throws Exception
    {
        // do processing here
    }
}

b. CSV Writer - Read from DB and output to CSV file

package com.fwd.pmap.memberInterfaceFile;

/* all imports */

public class CsvWriter
{
    public void generateInterfaceFile() throws Exception
    {
        // do processing here
    }
}

How can I automate both functions above to run on a specific time every day? For example:

  1. CSV Reader to run daily @ 05:00 AM
  2. CSV Writer to run daily @ 07:00 AM

Project Structure

Maruli
  • 173
  • 1
  • 3
  • 14
  • They should run inside you application? If so take a look at [Quartz Scheduler](https://www.quartz-scheduler.org/).... – khmarbaise Apr 04 '16 at 07:30
  • @khmarbaise I prefer not to write the scheduling within the app, that is why I am exploring whether this is possible through Windows Task Scheduler. The application will eventually be deployed on the server (running EAP as well) and I would like to setup the schedule task from the server itself. – Maruli Apr 04 '16 at 07:33
  • I think it's possible throught Windows task Scheduler to Schedule an execution of a script or an application but to Schedule a function in an application it's not possible. – Hohenheim Apr 04 '16 at 08:17
  • @Hohenheim If that's the case do you think it's possible to convert my project into application? I have edited my original post to include image of my project structure. Hope you can take a quick look. – Maruli Apr 04 '16 at 08:38
  • You could create a servlet, which will execute the functions. But be aware of the security issues, if you simple create a open GET-url. – Christian Kuetbach Apr 04 '16 at 08:40
  • Try to chech [this](http://stackoverflow.com/questions/15783553/run-a-jar-file-using-windows-scheduler) @Maruli – Hohenheim Apr 04 '16 at 08:55

1 Answers1

0

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);
    }
}
Maruli
  • 173
  • 1
  • 3
  • 14