Example usage for org.springframework.batch.core.launch.support JobRegistryBackgroundJobRunner register

List of usage examples for org.springframework.batch.core.launch.support JobRegistryBackgroundJobRunner register

Introduction

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

Prototype

private void register(String[] paths) throws DuplicateJobException, IOException 

Source Link

Usage

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

/**
 * Supply a list of application context locations, starting with the parent
 * context, and followed by the children. The parent must contain a
 * {@link JobRegistry} and the child contexts are expected to contain
 * {@link Job} definitions, each of which will be registered wit the
 * registry./*from   w  ww .j  a va 2  s  .com*/
 *
 * Example usage:
 *
 * <pre>
 * $ java -classpath ... JobRegistryBackgroundJobRunner job-registry-context.xml job1.xml job2.xml ...
 * </pre>
 *
 * The child contexts are created only when needed though the
 * {@link JobFactory} interface (but the XML is validated on startup by
 * using it to create a {@link BeanFactory} which is then discarded).
 *
 * The parent context is created in a separate thread, and the program will
 * pause for input in an infinite loop until the user hits any key.
 *
 * @param args the context locations to use (first one is for parent)
 * @throws Exception if anything goes wrong with the context creation
 */
public static void main(String... args) throws Exception {

    Assert.state(args.length >= 1, "At least one argument (the parent context path) must be provided.");

    final JobRegistryBackgroundJobRunner launcher = new JobRegistryBackgroundJobRunner(args[0]);
    errors.clear();

    logger.info("Starting job registry in parent context from XML at: [" + args[0] + "]");

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                launcher.run();
            } catch (RuntimeException e) {
                errors.add(e);
                throw e;
            }
        }
    }).start();

    logger.info("Waiting for parent context to start.");
    while (launcher.parentContext == null && errors.isEmpty()) {
        Thread.sleep(100L);
    }

    synchronized (errors) {
        if (!errors.isEmpty()) {
            logger.info(errors.size() + " errors detected on startup of parent context.  Rethrowing.");
            throw errors.get(0);
        }
    }
    errors.clear();

    // Paths to individual job configurations.
    final String[] paths = new String[args.length - 1];
    System.arraycopy(args, 1, paths, 0, paths.length);

    logger.info("Parent context started.  Registering jobs from paths: " + Arrays.asList(paths));
    launcher.register(paths);

    if (System.getProperty(EMBEDDED) != null) {
        launcher.destroy();
        return;
    }

    synchronized (JobRegistryBackgroundJobRunner.class) {
        System.out.println(
                "Started application.  Interrupt (CTRL-C) or call JobRegistryBackgroundJobRunner.stop() to exit.");
        JobRegistryBackgroundJobRunner.class.wait();
    }
    launcher.destroy();

}