Skip to content

BATCH-1763: listener callback for Job loader #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ public class DefaultJobLoader implements JobLoader {

private JobRegistry jobRegistry;

private Map<ApplicationContextFactory, ConfigurableApplicationContext> contexts = new ConcurrentHashMap<ApplicationContextFactory, ConfigurableApplicationContext>();
private Map<ApplicationContextFactory, ConfigurableApplicationContext> contexts =
new ConcurrentHashMap<ApplicationContextFactory, ConfigurableApplicationContext>();

private Map<ConfigurableApplicationContext, Collection<String>> contextToJobNames = new ConcurrentHashMap<ConfigurableApplicationContext, Collection<String>>();
private Map<ConfigurableApplicationContext, Collection<String>> contextToJobNames =
new ConcurrentHashMap<ConfigurableApplicationContext, Collection<String>>();

/**
* Default constructor useful for declarative configuration.
Expand Down Expand Up @@ -85,8 +87,10 @@ public void clear() {
}
for (String jobName : jobRegistry.getJobNames()) {
jobRegistry.unregister(jobName);
onUnregisteredJob(jobName);
}
contexts.clear();
contextToJobNames.clear();
}

public Collection<Job> reload(ApplicationContextFactory factory) {
Expand All @@ -97,15 +101,17 @@ public Collection<Job> 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);
}
}
Expand Down Expand Up @@ -139,6 +145,7 @@ private Collection<Job> 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
Expand All @@ -151,7 +158,7 @@ private Collection<Job> doLoad(ApplicationContextFactory factory, boolean unregi
JobFactory jobFactory = new ReferenceJobFactory(job);
jobRegistry.register(jobFactory);
jobsRegistered.add(jobName);

onRegisteredJob(context, job);
}

}
Expand All @@ -174,4 +181,52 @@ private Collection<Job> 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<ApplicationContextFactory, ConfigurableApplicationContext> 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<ConfigurableApplicationContext, Collection<String>> getContextToJobNames() {
return contextToJobNames;
}

}