/** * If there is no {@link JobLoader} then try and create one from existing * bean definitions. */ private void maybeCreateJobLoader() { if (jobLoader != null) { return; } String[] names = parentContext.getBeanNamesForType(JobLoader.class); if (names.length == 0) { if (parentContext.containsBean("jobLoader")) { jobLoader = parentContext.getBean("jobLoader", JobLoader.class); return; } if (jobRegistry != null) { jobLoader = new DefaultJobLoader(jobRegistry); return; } } jobLoader = parentContext.getBean(names[0], JobLoader.class); return; }
@Override public Collection<Job> reload(ApplicationContextFactory factory) { // If the same factory is loaded twice the context can be closed if (contexts.containsKey(factory)) { ConfigurableApplicationContext context = contexts.get(factory); for (String name : contextToJobNames.get(context)) { if (logger.isDebugEnabled()) { logger.debug("Unregistering job: " + name + " from context: " + context.getDisplayName()); } doUnregister(name); } context.close(); } try { return doLoad(factory, true); } catch (DuplicateJobException e) { throw new IllegalStateException("Found duplicate job in reload (it should have been unregistered " + "if it was previously registered in this loader)", e); } }
logger.debug("Unregistering job: " + jobName + " from context: " + context.getDisplayName()); doUnregister(jobName); logger.debug("Registering job: " + jobName + " from context: " + context.getDisplayName()); doRegister(context, job); jobsRegistered.add(jobName);
@Test public void createWithBothRegistries() { final DefaultJobLoader loader = new DefaultJobLoader(); loader.setJobRegistry(jobRegistry); loader.setStepRegistry(stepRegistry); loader.afterPropertiesSet(); }
@Test public void createWithOnlyJobRegistry() { final DefaultJobLoader loader = new DefaultJobLoader(); loader.setJobRegistry(jobRegistry); loader.afterPropertiesSet(); }
@Override public Collection<Job> load(ApplicationContextFactory factory) throws DuplicateJobException { return doLoad(factory, false); }
@Before public void setUp() { DefaultJobLoader jobLoader = new DefaultJobLoader(); jobLoader.setJobRegistry(registry); registrar.setJobLoader(jobLoader); }
/** * Unregister all the jobs and close all the contexts created by this * loader. * * @see JobLoader#clear() */ @Override public void clear() { for (ConfigurableApplicationContext context : contexts.values()) { if (context.isActive()) { context.close(); } } for (String jobName : jobRegistry.getJobNames()) { doUnregister(jobName); } contexts.clear(); contextToJobNames.clear(); }
/** * Registers the specified {@link Job} defined in the specified {@link ConfigurableApplicationContext}. * <br> * Makes sure to update the {@link StepRegistry} if it is available. * * @param context the context in which the job is defined * @param job the job to register * @throws DuplicateJobException if that job is already registered */ private void doRegister(ConfigurableApplicationContext context, Job job) throws DuplicateJobException { final JobFactory jobFactory = new ReferenceJobFactory(job); jobRegistry.register(jobFactory); if (stepRegistry != null) { if (!(job instanceof StepLocator)) { throw new UnsupportedOperationException("Cannot locate steps from a Job that is not a StepLocator: job=" + job.getName() + " does not implement StepLocator"); } stepRegistry.register(job.getName(), getSteps((StepLocator) job, context)); } }
@Override public Collection<Job> load(ApplicationContextFactory factory) throws DuplicateJobException { return doLoad(factory, false); }
/** * Unregister all the jobs and close all the contexts created by this * loader. * * @see JobLoader#clear() */ @Override public void clear() { for (ConfigurableApplicationContext context : contexts.values()) { if (context.isActive()) { context.close(); } } for (String jobName : jobRegistry.getJobNames()) { doUnregister(jobName); } contexts.clear(); contextToJobNames.clear(); }
/** * Registers the specified {@link Job} defined in the specified {@link ConfigurableApplicationContext}. * <br> * Makes sure to update the {@link StepRegistry} if it is available. * * @param context the context in which the job is defined * @param job the job to register * @throws DuplicateJobException if that job is already registered */ private void doRegister(ConfigurableApplicationContext context, Job job) throws DuplicateJobException { final JobFactory jobFactory = new ReferenceJobFactory(job); jobRegistry.register(jobFactory); if (stepRegistry != null) { if (!(job instanceof StepLocator)) { throw new UnsupportedOperationException("Cannot locate steps from a Job that is not a StepLocator: job=" + job.getName() + " does not implement StepLocator"); } stepRegistry.register(job.getName(), getSteps((StepLocator) job, context)); } }
@Bean public AutomaticJobRegistrar jobRegistrar() throws Exception { registrar.setJobLoader(new DefaultJobLoader(jobRegistry())); for (ApplicationContextFactory factory : context.getBeansOfType(ApplicationContextFactory.class).values()) { registrar.addApplicationContextFactory(factory); } return registrar; }
@Override public Collection<Job> reload(ApplicationContextFactory factory) { // If the same factory is loaded twice the context can be closed if (contexts.containsKey(factory)) { ConfigurableApplicationContext context = contexts.get(factory); for (String name : contextToJobNames.get(context)) { logger.debug("Unregistering job: " + name + " from context: " + context.getDisplayName()); doUnregister(name); } context.close(); } try { return doLoad(factory, true); } catch (DuplicateJobException e) { throw new IllegalStateException("Found duplicate job in reload (it should have been unregistered " + "if it was previously registered in this loader)", e); } }
doUnregister(jobName); doRegister(context, job); jobsRegistered.add(jobName);
@Override public Collection<Job> load(ApplicationContextFactory factory) throws DuplicateJobException { return doLoad(factory, false); }
/** * Unregister all the jobs and close all the contexts created by this * loader. * * @see JobLoader#clear() */ @Override public void clear() { for (ConfigurableApplicationContext context : contexts.values()) { if (context.isActive()) { context.close(); } } for (String jobName : jobRegistry.getJobNames()) { doUnregister(jobName); } contexts.clear(); }
/** * Registers the specified {@link Job} defined in the specified {@link ConfigurableApplicationContext}. * <br> * Makes sure to update the {@link StepRegistry} if it is available. * * @param context the context in which the job is defined * @param job the job to register * @throws DuplicateJobException if that job is already registered */ private void doRegister(ConfigurableApplicationContext context, Job job) throws DuplicateJobException { final JobFactory jobFactory = new ReferenceJobFactory(job); jobRegistry.register(jobFactory); if (stepRegistry != null) { if (!(job instanceof StepLocator)) { throw new UnsupportedOperationException("Cannot locate steps from a Job that is not a StepLocator: job=" + job.getName() + " does not implement StepLocator"); } stepRegistry.register(job.getName(), getSteps((StepLocator) job, context)); } }
@Test public void testNoStepRegistryAvailable() throws DuplicateJobException { final JobLoader loader = new DefaultJobLoader(jobRegistry); GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource("job-context-with-steps.xml", getClass())); loader.load(factory); // No step registry available so just registering the jobs assertEquals(2, jobRegistry.getJobNames().size()); }
@Override public Collection<Job> reload(ApplicationContextFactory factory) { // If the same factory is loaded twice the context can be closed if (contexts.containsKey(factory)) { ConfigurableApplicationContext context = contexts.get(factory); for (String name : contextToJobNames.get(context)) { if (logger.isDebugEnabled()) { logger.debug("Unregistering job: " + name + " from context: " + context.getDisplayName()); } doUnregister(name); } context.close(); } try { return doLoad(factory, true); } catch (DuplicateJobException e) { throw new IllegalStateException("Found duplicate job in reload (it should have been unregistered " + "if it was previously registered in this loader)", e); } }