Example usage for org.springframework.batch.core.configuration.support ApplicationContextFactory createApplicationContext

List of usage examples for org.springframework.batch.core.configuration.support ApplicationContextFactory createApplicationContext

Introduction

In this page you can find the example usage for org.springframework.batch.core.configuration.support ApplicationContextFactory createApplicationContext.

Prototype

ConfigurableApplicationContext createApplicationContext();

Source Link

Usage

From source file:org.springframework.batch.core.configuration.support.DefaultJobLoader.java

@SuppressWarnings("resource")
private Collection<Job> doLoad(ApplicationContextFactory factory, boolean unregister)
        throws DuplicateJobException {

    Collection<String> jobNamesBefore = jobRegistry.getJobNames();
    ConfigurableApplicationContext context = factory.createApplicationContext();
    Collection<String> jobNamesAfter = jobRegistry.getJobNames();
    // Try to detect auto-registration (e.g. through a bean post processor)
    boolean autoRegistrationDetected = jobNamesAfter.size() > jobNamesBefore.size();

    Collection<String> jobsRegistered = new HashSet<String>();
    if (autoRegistrationDetected) {
        for (String name : jobNamesAfter) {
            if (!jobNamesBefore.contains(name)) {
                jobsRegistered.add(name);
            }//from w  w w.j  a  v  a2s . c o  m
        }
    }

    contexts.put(factory, context);
    String[] names = context.getBeanNamesForType(Job.class);

    for (String name : names) {

        if (!autoRegistrationDetected) {

            Job job = (Job) context.getBean(name);
            String jobName = job.getName();

            // On reload try to unregister first
            if (unregister) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "Unregistering job: " + jobName + " from context: " + context.getDisplayName());
                }
                doUnregister(jobName);
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Registering job: " + jobName + " from context: " + context.getDisplayName());
            }
            doRegister(context, job);
            jobsRegistered.add(jobName);
        }

    }

    Collection<Job> result = new ArrayList<Job>();
    for (String name : jobsRegistered) {
        try {
            result.add(jobRegistry.getJob(name));
        } catch (NoSuchJobException e) {
            // should not happen;
            throw new IllegalStateException("Could not retrieve job that was should have been registered", e);
        }

    }

    contextToJobNames.put(context, jobsRegistered);

    return result;

}