com.japp.Main.java Source code

Java tutorial

Introduction

Here is the source code for com.japp.Main.java

Source

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.japp;

import java.util.Scanner;

import com.japp.utils.SpringIntegrationUtils;
import org.slf4j.Logger;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Starts the Spring Context and will initialize the Spring Integration routes.
 *
 * @author n
 * @since 1.0
 *
 */
public final class Main {

    private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(Main.class);

    private Main() {
    }

    /**
     * Load the Spring Integration Application Context
     *
     * @param args - command line arguments
     */
    public static void main(final String... args) {

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("\n========================================================="
                    + "\n                                                         "
                    + "\n       SFTP-File Transfer POC                 "
                    + "\n                                                         "
                    + "\n=========================================================");
        }

        final AbstractApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:/launch-context.xml");

        context.registerShutdownHook();

        SpringIntegrationUtils.displayDirectories(context);

        final Scanner scanner = new Scanner(System.in);

        JobLauncher jobLauncher = context.getBean("jobLauncher", JobLauncher.class);
        Job job = context.getBean("job1", Job.class);

        try {
            jobLauncher.run(job, new JobParameters());
        } catch (JobExecutionAlreadyRunningException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } catch (JobRestartException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } catch (JobInstanceAlreadyCompleteException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } catch (JobParametersInvalidException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("\n========================================================="
                    + "\n                                                         "
                    + "\n    Please press 'q + Enter' to quit the application.    "
                    + "\n                                                         "
                    + "\n=========================================================");
        }

        while (!scanner.hasNext("q")) {
            //Do nothing unless user presses 'q' to quit.
        }

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Exiting application...bye.");
        }

        System.exit(0);

    }
}