@Bean public AutomaticJobRegistrar jobRegistrar() throws Exception { registrar.setJobLoader(new DefaultJobLoader(jobRegistry())); for (ApplicationContextFactory factory : context.getBeansOfType(ApplicationContextFactory.class).values()) { registrar.addApplicationContextFactory(factory); } return registrar; }
@Test public void testClear() throws Exception { Resource[] jobPaths = new Resource[] { new ClassPathResource( "org/springframework/batch/core/launch/support/2jobs.xml") }; setUpApplicationContextFactories(jobPaths, null); registrar.start(); assertEquals(2, registry.getJobNames().size()); registrar.stop(); assertEquals(0, registry.getJobNames().size()); }
@Test public void testStartStopRunning() throws Exception { Resource[] jobPaths = new Resource[] { new ClassPathResource( "org/springframework/batch/core/launch/support/2jobs.xml") }; setUpApplicationContextFactories(jobPaths, null); registrar.start(); assertTrue(registrar.isRunning()); registrar.start(); assertEquals(2, registry.getJobNames().size()); registrar.stop(); assertFalse(registrar.isRunning()); }
@PostConstruct public void initialize() { registrar.addApplicationContextFactory(new GenericApplicationContextFactory(TestConfiguration.class)); registrar.addApplicationContextFactory(new GenericApplicationContextFactory(VanillaConfiguration.class)); }
@Bean public AutomaticJobRegistrar jobRegistrar(JobRegistry jobRegistry) throws Exception { AutomaticJobRegistrar registrar = new AutomaticJobRegistrar(); registrar.setJobLoader(new DefaultJobLoader(jobRegistry)); for (ApplicationContextFactory factory : applicationContext.getBeansOfType(ApplicationContextFactory.class).values()) { registrar.addApplicationContextFactory(factory); } return registrar; }
@Override public void stop(Runnable callback) { stop(); callback.run(); }
@Test public void testChildContextOverridesBeanPostProcessor() throws Exception { Resource[] jobPaths = new Resource[] { new ClassPathResource( "org/springframework/batch/core/launch/support/2jobs.xml") }; @SuppressWarnings("resource") ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "/org/springframework/batch/core/launch/support/test-environment-with-registry-and-auto-register.xml"); registrar.setApplicationContext(applicationContext); setUpApplicationContextFactories(jobPaths, applicationContext); registrar.start(); assertEquals(2, registry.getJobNames().size()); }
@Before public void setUp() { DefaultJobLoader jobLoader = new DefaultJobLoader(); jobLoader.setJobRegistry(registry); registrar.setJobLoader(jobLoader); }
@PostConstruct public void initialize() throws Exception { // Default order for the AutomaticJobRegistrar is Ordered.LOWEST_PRECEDENCE. Since we want to register // listeners after the jobs are registered through the AutomaticJobRegistrar, we need to decrement its // order value by one. The creation of the AutomaticJobRegistrar bean is hidden deep in the automatic // batch configuration, so we unfortunately have to do it here. automaticJobRegistrar.setOrder(Ordered.LOWEST_PRECEDENCE - 1); addApplicationContextFactories(automaticJobRegistrar); }
@Test public void testErrorInContext() throws Exception { Resource[] jobPaths = new Resource[] { new ClassPathResource("org/springframework/batch/core/launch/support/2jobs.xml"), new ClassPathResource("org/springframework/batch/core/launch/support/error.xml") }; setUpApplicationContextFactories(jobPaths, null); try { registrar.start(); fail("Expected BeanCreationException"); } catch (BeanCreationException e) { } }
protected void registerJobsFromXml(AutomaticJobRegistrar automaticJobRegistrar) throws IOException { // Add all XML-Configurations to the AutomaticJobRegistrar ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); Resource[] xmlConfigurations = resourcePatternResolver.getResources(batchConfig.getConfig().getPathXml()); for (Resource resource : xmlConfigurations) { LOGGER.info("Register jobs from {}", resource); automaticJobRegistrar.addApplicationContextFactory(new GenericApplicationContextFactory(resource)); } }
@Test public void testStartStopRunningWithCallback() throws Exception { Runnable callback = Mockito.mock(Runnable.class); Resource[] jobPaths = new Resource[] { new ClassPathResource( "org/springframework/batch/core/launch/support/2jobs.xml") }; setUpApplicationContextFactories(jobPaths, null); registrar.start(); assertTrue(registrar.isRunning()); registrar.start(); assertEquals(2, registry.getJobNames().size()); registrar.stop(callback); assertFalse(registrar.isRunning()); assertEquals(0, registry.getJobNames().size()); Mockito.verify(callback, Mockito.times(1)).run(); }
@Override public void stop(Runnable callback) { stop(); callback.run(); }
@Test public void testNoJobFound() throws Exception { Resource[] jobPaths = new Resource[] { new ClassPathResource( "org/springframework/batch/core/launch/support/test-environment.xml") }; @SuppressWarnings("resource") GenericApplicationContext applicationContext = new GenericApplicationContext(); applicationContext.refresh(); setUpApplicationContextFactories(jobPaths, applicationContext); registrar.setApplicationContext(applicationContext); registrar.start(); }
@PostConstruct public void initialize() throws Exception { // Default order for the AutomaticJobRegistrar is Ordered.LOWEST_PRECEDENCE. Since we want to register // listeners after the jobs are registered through the AutomaticJobRegistrar, we need to decrement its // order value by one. The creation of the AutomaticJobRegistrar bean is hidden deep in the automatic // batch configuration, so we unfortunately have to do it here. automaticJobRegistrar.setOrder(Ordered.LOWEST_PRECEDENCE - 1); addApplicationContextFactories(automaticJobRegistrar); }
@Bean public AutomaticJobRegistrar jobRegistrar() throws Exception { registrar.setJobLoader(new DefaultJobLoader(jobRegistry())); for (ApplicationContextFactory factory : context.getBeansOfType(ApplicationContextFactory.class).values()) { registrar.addApplicationContextFactory(factory); } return registrar; }
protected void registerJobsFromXml(AutomaticJobRegistrar automaticJobRegistrar) throws IOException { // Add all XML-Configurations to the AutomaticJobRegistrar ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); Resource[] xmlConfigurations = resourcePatternResolver.getResources(batchConfig.getConfig().getPathXml()); for (Resource resource : xmlConfigurations) { LOGGER.info("Register jobs from {}", resource); automaticJobRegistrar.addApplicationContextFactory(new GenericApplicationContextFactory(resource)); } }
/** * Creates all the application contexts required and set up job registry * entries with all the instances of {@link Job} found therein. Also closes * the contexts when the enclosing context is closed. * * @see InitializingBean#afterPropertiesSet() */ public final void onApplicationEvent(ApplicationEvent event) { // TODO: With Spring 3 a SmartLifecycle is started automatically if (event.getSource() == applicationContext) { if (event instanceof ContextRefreshedEvent) { start(); } else if (event instanceof ContextClosedEvent) { stop(); } } }
@Override public void stop(Runnable callback) { stop(); callback.run(); }