Example usage for org.springframework.batch.core.launch.support CommandLineJobRunner start

List of usage examples for org.springframework.batch.core.launch.support CommandLineJobRunner start

Introduction

In this page you can find the example usage for org.springframework.batch.core.launch.support CommandLineJobRunner start.

Prototype

@SuppressWarnings("resource")
    int start(String jobPath, String jobIdentifier, String[] parameters, Set<String> opts) 

Source Link

Usage

From source file:org.springframework.batch.core.launch.support.CommandLineJobRunner.java

/**
 * Launch a batch job using a {@link CommandLineJobRunner}. Creates a new
 * Spring context for the job execution, and uses a common parent for all
 * such contexts. No exception are thrown from this method, rather
 * exceptions are logged and an integer returned through the exit status in
 * a {@link JvmSystemExiter} (which can be overridden by defining one in the
 * Spring context).<br>//  ww w  .  j a  v  a2s  . com
 * Parameters can be provided in the form key=value, and will be converted
 * using the injected {@link JobParametersConverter}.
 *
 * @param args
 * <ul>
 * <li>-restart: (optional) if the job has failed or stopped and the most
 * should be restarted. If specified then the jobIdentifier parameter can be
 * interpreted either as the name of the job or the id of the job execution
 * that failed.</li>
 * <li>-next: (optional) if the job has a {@link JobParametersIncrementer}
 * that can be used to launch the next in a sequence</li>
 * <li>jobPath: the xml application context containing a {@link Job}
 * <li>jobIdentifier: the bean id of the job or id of the failed execution
 * in the case of a restart.
 * <li>jobParameters: 0 to many parameters that will be used to launch a
 * job.
 * </ul>
 * <p>
 * The options (<code>-restart, -next</code>) can occur anywhere in the
 * command line.
 * </p>
 */
public static void main(String[] args) throws Exception {

    CommandLineJobRunner command = new CommandLineJobRunner();

    List<String> newargs = new ArrayList<String>(Arrays.asList(args));

    try {
        if (System.in.available() > 0) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String line = " ";
            while (line != null) {
                if (!line.startsWith("#") && StringUtils.hasText(line)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Stdin arg: " + line);
                    }
                    newargs.add(line);
                }
                line = reader.readLine();
            }
        }
    } catch (IOException e) {
        logger.warn("Could not access stdin (maybe a platform limitation)");
        if (logger.isDebugEnabled()) {
            logger.debug("Exception details", e);
        }
    }

    Set<String> opts = new HashSet<String>();
    List<String> params = new ArrayList<String>();

    int count = 0;
    String jobPath = null;
    String jobIdentifier = null;

    for (String arg : newargs) {
        if (VALID_OPTS.contains(arg)) {
            opts.add(arg);
        } else {
            switch (count) {
            case 0:
                jobPath = arg;
                break;
            case 1:
                jobIdentifier = arg;
                break;
            default:
                params.add(arg);
                break;
            }
            count++;
        }
    }

    if (jobPath == null || jobIdentifier == null) {
        String message = "At least 2 arguments are required: JobPath/JobClass and jobIdentifier.";
        logger.error(message);
        CommandLineJobRunner.message = message;
        command.exit(1);
        return;
    }

    String[] parameters = params.toArray(new String[params.size()]);

    int result = command.start(jobPath, jobIdentifier, parameters, opts);
    command.exit(result);
}