diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultJobLoader.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultJobLoader.java index b79ab9c07d..5f6115ed57 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultJobLoader.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultJobLoader.java @@ -43,9 +43,11 @@ public class DefaultJobLoader implements JobLoader { private JobRegistry jobRegistry; - private Map contexts = new ConcurrentHashMap(); + private Map contexts = + new ConcurrentHashMap(); - private Map> contextToJobNames = new ConcurrentHashMap>(); + private Map> contextToJobNames = + new ConcurrentHashMap>(); /** * Default constructor useful for declarative configuration. @@ -85,8 +87,10 @@ public void clear() { } for (String jobName : jobRegistry.getJobNames()) { jobRegistry.unregister(jobName); + onUnregisteredJob(jobName); } contexts.clear(); + contextToJobNames.clear(); } public Collection reload(ApplicationContextFactory factory) { @@ -97,15 +101,17 @@ public Collection reload(ApplicationContextFactory factory) { for (String name : contextToJobNames.get(context)) { logger.debug("Unregistering job: " + name + " from context: " + context.getDisplayName()); jobRegistry.unregister(name); + onUnregisteredJob(name); } context.close(); + contextToJobNames.remove(context); } try { return doLoad(factory, true); } catch (DuplicateJobException e) { - throw new IllegalStateException("Found duplicte job in reload (it should have been unregistered " + throw new IllegalStateException("Found duplicate job in reload (it should have been unregistered " + "if it was previously registered in this loader)", e); } } @@ -139,6 +145,7 @@ private Collection doLoad(ApplicationContextFactory factory, boolean unregi if (!autoRegistrationDetected) { Job job = (Job) context.getBean(name); + beforeJobRegistration(context, job); String jobName = job.getName(); // On reload try to unregister first @@ -151,7 +158,7 @@ private Collection doLoad(ApplicationContextFactory factory, boolean unregi JobFactory jobFactory = new ReferenceJobFactory(job); jobRegistry.register(jobFactory); jobsRegistered.add(jobName); - + onRegisteredJob(context, job); } } @@ -174,4 +181,52 @@ private Collection doLoad(ApplicationContextFactory factory, boolean unregi } -} + // Callback methods + + /** + * Invoked when a {@link Job} is about to be registered. + * + * @param context the context where the job is defined + * @param job the job that is about to be registered + */ + protected void beforeJobRegistration(ConfigurableApplicationContext context, Job job) { + } + + /** + * Invoked when a {@link Job} has been registered. + * + * @param context the context where the job is defined + * @param job the job that has been registered + */ + protected void onRegisteredJob(ConfigurableApplicationContext context, Job job) { + } + + /** + * Invoked when a {@link Job} is unregistered. + * + * @param jobName the name of the job that was unregistered + */ + protected void onUnregisteredJob(String jobName) { + } + + // Useful for unit testing purposes + + /** + * Returns the map associating context factories to the contexts they have generated. + * + * @return a map from contract factories to Spring contexts + */ + protected Map getContexts() { + return contexts; + } + + /** + * Returns the map associating application contexts to all the job names they contained. + * + * @return a map from Spring contexts to job names + */ + protected Map> getContextToJobNames() { + return contextToJobNames; + } + +} \ No newline at end of file