-
Notifications
You must be signed in to change notification settings - Fork 26
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
SLING-12394 improve ResourceResolver handling #39
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,36 +141,31 @@ public void run() { | |
} | ||
|
||
private void scan() { | ||
final ResourceResolver resolver = configuration.createResourceResolver(); | ||
if ( resolver != null ) { | ||
Comment on lines
-144
to
-145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we have a null check. Should we handle the exception here instead? |
||
try { | ||
logger.debug("Scanning for scheduled jobs..."); | ||
final String path = this.configuration.getScheduledJobsPath(false); | ||
final Resource startResource = resolver.getResource(path); | ||
if ( startResource != null ) { | ||
final Map<String, Holder> newScheduledJobs = new HashMap<String, Holder>(); | ||
synchronized ( this.scheduledJobs ) { | ||
for(final Resource rsrc : startResource.getChildren()) { | ||
if ( !isRunning.get() ) { | ||
break; | ||
} | ||
handleAddOrUpdate(newScheduledJobs, rsrc); | ||
try (final ResourceResolver resolver = configuration.createResourceResolver();) { | ||
logger.debug("Scanning for scheduled jobs..."); | ||
final String path = this.configuration.getScheduledJobsPath(false); | ||
final Resource startResource = resolver.getResource(path); | ||
if ( startResource != null ) { | ||
final Map<String, Holder> newScheduledJobs = new HashMap<String, Holder>(); | ||
synchronized ( this.scheduledJobs ) { | ||
for(final Resource rsrc : startResource.getChildren()) { | ||
if ( !isRunning.get() ) { | ||
break; | ||
} | ||
if ( isRunning.get() ) { | ||
for(final Holder h : this.scheduledJobs.values()) { | ||
if ( h.info != null ) { | ||
this.jobScheduler.unscheduleJob(h.info); | ||
} | ||
handleAddOrUpdate(newScheduledJobs, rsrc); | ||
} | ||
if ( isRunning.get() ) { | ||
for(final Holder h : this.scheduledJobs.values()) { | ||
if ( h.info != null ) { | ||
this.jobScheduler.unscheduleJob(h.info); | ||
} | ||
this.scheduledJobs.clear(); | ||
this.scheduledJobs.putAll(newScheduledJobs); | ||
} | ||
this.scheduledJobs.clear(); | ||
this.scheduledJobs.putAll(newScheduledJobs); | ||
} | ||
} | ||
logger.debug("Finished scanning for scheduled jobs..."); | ||
} finally { | ||
resolver.close(); | ||
} | ||
logger.debug("Finished scanning for scheduled jobs..."); | ||
} | ||
} | ||
|
||
|
@@ -235,8 +230,7 @@ private Map<String, Object> writeScheduledJob(final String jobTopic, | |
final boolean suspend, | ||
final List<ScheduleInfoImpl> scheduleInfos) | ||
throws PersistenceException { | ||
final ResourceResolver resolver = this.configuration.createResourceResolver(); | ||
try { | ||
try (final ResourceResolver resolver = this.configuration.createResourceResolver();) { | ||
// create properties | ||
final Map<String, Object> properties = new HashMap<String, Object>(); | ||
|
||
|
@@ -286,8 +280,6 @@ private Map<String, Object> writeScheduledJob(final String jobTopic, | |
properties.put(ResourceHelper.PROPERTY_SCHEDULE_INFO, scheduleInfos); | ||
|
||
return properties; | ||
} finally { | ||
resolver.close(); | ||
} | ||
} | ||
|
||
|
@@ -331,23 +323,18 @@ public void run() { | |
} | ||
} | ||
if ( !updateJobs.isEmpty() && isRunning.get() ) { | ||
ResourceResolver resolver = configuration.createResourceResolver(); | ||
if ( resolver != null ) { | ||
Comment on lines
-334
to
-335
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we have a null check. Should we handle the exception here instead? |
||
try { | ||
for(final Map.Entry<String, Holder> entry : updateJobs.entrySet()) { | ||
final String path = configuration.getScheduledJobsPath(true) + entry.getKey(); | ||
final Resource rsrc = resolver.getResource(path); | ||
if ( !isRunning.get() ) { | ||
break; | ||
} | ||
if ( rsrc != null ) { | ||
synchronized ( scheduledJobs ) { | ||
handleAddOrUpdate(scheduledJobs, rsrc); | ||
} | ||
try (ResourceResolver resolver = configuration.createResourceResolver();) { | ||
for(final Map.Entry<String, Holder> entry : updateJobs.entrySet()) { | ||
final String path = configuration.getScheduledJobsPath(true) + entry.getKey(); | ||
final Resource rsrc = resolver.getResource(path); | ||
if ( !isRunning.get() ) { | ||
break; | ||
} | ||
if ( rsrc != null ) { | ||
synchronized ( scheduledJobs ) { | ||
handleAddOrUpdate(scheduledJobs, rsrc); | ||
} | ||
} | ||
} finally { | ||
resolver.close(); | ||
} | ||
} | ||
} | ||
|
@@ -387,17 +374,12 @@ public void handleAddUpdate(final String path) { | |
@Override | ||
public void run() { | ||
if ( isRunning.get() ) { | ||
final ResourceResolver resolver = configuration.createResourceResolver(); | ||
if ( resolver != null ) { | ||
Comment on lines
-390
to
-391
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we have a null check. Should we handle the exception here instead? |
||
try { | ||
final Resource rsrc = resolver.getResource(path); | ||
if ( rsrc != null ) { | ||
synchronized ( scheduledJobs ) { | ||
handleAddOrUpdate(scheduledJobs, rsrc); | ||
} | ||
try (final ResourceResolver resolver = configuration.createResourceResolver();) { | ||
final Resource rsrc = resolver.getResource(path); | ||
if ( rsrc != null ) { | ||
synchronized ( scheduledJobs ) { | ||
handleAddOrUpdate(scheduledJobs, rsrc); | ||
} | ||
} finally { | ||
resolver.close(); | ||
} | ||
} | ||
} | ||
|
@@ -462,8 +444,8 @@ private void handleAddOrUpdate(final Map<String, Holder> newScheduledJobs, final | |
public void remove(final ScheduledJobInfoImpl info) { | ||
final String scheduleKey = ResourceHelper.filterName(info.getName()); | ||
|
||
final ResourceResolver resolver = configuration.createResourceResolver(); | ||
try { | ||
|
||
try (final ResourceResolver resolver = configuration.createResourceResolver();) { | ||
final StringBuilder sb = new StringBuilder(configuration.getScheduledJobsPath(true)); | ||
sb.append(scheduleKey); | ||
final String path = sb.toString(); | ||
|
@@ -476,8 +458,6 @@ public void remove(final ScheduledJobInfoImpl info) { | |
} catch (final PersistenceException pe) { | ||
// we ignore the exception if removing fails | ||
ignoreException(pe); | ||
} finally { | ||
resolver.close(); | ||
} | ||
|
||
synchronized ( this.scheduledJobs ) { | ||
|
@@ -490,8 +470,7 @@ public void remove(final ScheduledJobInfoImpl info) { | |
|
||
public void updateSchedule(final String scheduleName, final Collection<ScheduleInfo> scheduleInfo) { | ||
|
||
final ResourceResolver resolver = configuration.createResourceResolver(); | ||
try { | ||
try (final ResourceResolver resolver = configuration.createResourceResolver();) { | ||
final String scheduleKey = ResourceHelper.filterName(scheduleName); | ||
|
||
final StringBuilder sb = new StringBuilder(configuration.getScheduledJobsPath(true)); | ||
|
@@ -527,8 +506,6 @@ public void updateSchedule(final String scheduleName, final Collection<ScheduleI | |
logger.warn("Unable to update scheduled job " + scheduleName, pe); | ||
} | ||
} | ||
} finally { | ||
resolver.close(); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC this change will result in exceptions rather than silent swallows if the factory is null. That might its own consequences. What is the reasoning behind this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @stefan-egli's concern. There are quite a few places where we had a null check before.
@joerghoh I suggest to throw a
NullPointerException
(or something more specific) instead of aRuntimeException
and to catch and log this exception in the places where we used to have a null check.