This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Refactor LockingRoutePolicy out of periodic delete #53
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
lightblue/src/main/java/org/esbtools/eventhandler/lightblue/locking/LockingRoutePolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.esbtools.eventhandler.lightblue.locking; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import org.apache.camel.Exchange; | ||
import org.apache.camel.Route; | ||
import org.apache.camel.support.RoutePolicySupport; | ||
|
||
public class LockingRoutePolicy extends RoutePolicySupport { | ||
|
||
private final String identifier; | ||
|
||
private final LockStrategy lockStrategy; | ||
|
||
private @Nullable LockedResource<String> lock; | ||
|
||
public LockingRoutePolicy(String identifier, LockStrategy lockStrategy) { | ||
this.identifier = identifier; | ||
this.lockStrategy = lockStrategy; | ||
} | ||
|
||
@Override | ||
public void onStop(Route route) { | ||
releaseLock(); | ||
} | ||
|
||
@Override | ||
public void onSuspend(Route route) { | ||
releaseLock(); | ||
} | ||
|
||
@Override | ||
public synchronized void onExchangeBegin(Route route, Exchange exchange) { | ||
if (lock != null) { | ||
try { | ||
lock.ensureAcquiredOrThrow("Lost lock"); | ||
return; | ||
} catch (LostLockException e) { | ||
log.warn("Lost lock w id: " + identifier + ", trying to reacquire...", e); | ||
lock = null; | ||
} | ||
} | ||
|
||
try { | ||
lock = lockStrategy.tryAcquire(identifier); | ||
} catch (LockNotAvailableException e) { | ||
log.debug("Lock not available, assuming " + | ||
"another thread is holding lock w/ id: " + identifier, e); | ||
exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE); | ||
} | ||
} | ||
|
||
private synchronized void releaseLock() { | ||
if (lock == null) return; | ||
|
||
try { | ||
lock.close(); | ||
} catch (IOException e) { | ||
log.warn("IOException trying to release lock w/ identifier " + identifier, e); | ||
} | ||
|
||
lock = null; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nitpicky, but it would be more consistent with lock strategy interface if
identifier
was namedresourceId
, perhaps I even had this wrong originally.