Darshan
Darshan

Reputation: 3

How to run Spring batch app using CommandLineJobRunner (spring + hibernate and/or war deployment)

I need to create batch jobs using Spring Batch.

Job will access oracle DB then fetch records, process them in tasklet and commit results.

I am planning to use hibernate with spring to deal with data. Jobs will be executed via AutoSys. I am using CommandLineJobRunner as entry point.

(Extra info - I am using DynamicWebProject converted to Gradle, STS, Spring 4.0, Hibernate 5.0, NO Spring Boot)

I have few queries/doubts about this entire application. They are more towards environment/deployment.

  1. Do I need to deploy this whole app as a war in Tomcat(or any server) to instantiate all beans(spring and hibernate)?
  2. If yes, how can I start jobs using CommandLineJobRunner ?
  3. If no, I will have to manually instantiate beans in main method using ClassPathXmlApplicationContext. In this case how should I execute jobs ? Do I need to create jar(is this mandatory) ?
  4. How can I test these jobs on command line ? Do I need to pass jars(spring , hibernate etc dependencies) while using CommandLineJobRunner to execute jobs ?

I am new to batch jobs and all your comments would be of great help.

Thanks

Upvotes: 0

Views: 2185

Answers (1)

Niraj Sonawane
Niraj Sonawane

Reputation: 11115

  • No server is needed for spring batch applications.
  • You can launch job using jobLauncher bean . below is sample code.

    public class MyJobLauncher {

    public static void main(String[] args) {
        GenericApplicationContext context = new AnnotationConfigApplicationContext(MyBatchConfiguration.class);
        JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean("myJobName");//this is bean name of your job 
        JobExecution execution = jobLauncher.run(job, jobParameters);
    }
    

    }

You will need to create jar. Also all other jar that are needed are also required. You can use maven maven assembly plugin for this.

Upvotes: 1

Related Questions