Skip to content

Instantly share code, notes, and snippets.

@reikje
Created January 19, 2012 18:16
Show Gist options
  • Select an option

  • Save reikje/1641604 to your computer and use it in GitHub Desktop.

Select an option

Save reikje/1641604 to your computer and use it in GitHub Desktop.
Maybe I should have used a Lock here
build [pool-4-thread-9] [] DEBUG c.p.f.s.x.r.ChangeAwareDynamicProperty:41 - Handling non-existent version 10.0-MULTI_THREADED
build [pool-4-thread-7] [] DEBUG c.p.f.s.x.r.ChangeAwareDynamicProperty:41 - Handling non-existent version 10.0-MULTI_THREADED
public class ChangeAwareDynamicProperty extends DynamicProperty {
// current value of property. May be null, if the value has never been fetched
private volatile String currentVersion;
private final ConcurrentMap<String, Long> changedVersion = new ConcurrentHashMap<String, Long>();
@Override
public String toString() {
final String version = super.toString();
if (this.currentVersion == null) {
this.currentVersion = version;
} else if (!this.currentVersion.equals(version)) { // change of version in the underlaying source detected
final Long alreadyExisting = this.changedVersion.putIfAbsent(version, System.currentTimeMillis()); // another thread might have already picked up that new version
if (alreadyExisting == null) {
System.out.println(String.format("Handling non-existent version %s", version));
if (!wasVersionHandledBefore(version)) { // if this is a brand new version
try {
doCostlyOperationThatInvolvesNetwork();
this.currentVersion = version;
} catch (Exception e) {
throw new IllegalStateException(String.format("Unable to introduce new version %s using DynamicProperty. Do it manually.", version), e);
}
}
this.changedVersion.remove(version);
}
}
return this.currentVersion;
}
protected boolean wasVersionHandledBefore(final String version) {
// some check here
}
protected void doCostlyOperationThatInvolvesNetwork() {
// may take a few seconds
}
}
public class ChangeAwareDynamicProperty extends DynamicProperty {
// current value of property. May be null, if the value has never been fetched
private volatile String currentVersion;
private final ConcurrentMap<String, Long> changedVersion = new ConcurrentHashMap<String, Long>();
@Override
public String toString() {
final String version = super.toString();
if (this.currentVersion == null) {
this.currentVersion = version;
} else if (!this.currentVersion.equals(version)) { // change of version in the underlaying source detected
final Long alreadyExisting = this.changedVersion.putIfAbsent(version, System.currentTimeMillis()); // another thread might have already picked up that new version
if (alreadyExisting == null) {
System.out.println(String.format("Handling non-existent version %s", version));
if (!wasVersionHandledBefore(version)) { // if this is a brand new version
try {
doCostlyOperationThatInvolvesNetwork();
this.changedVersion.remove(this.currentVersion); // cleanup previous value
this.currentVersion = version;
} catch (Exception e) {
throw new IllegalStateException(String.format("Unable to introduce new version %s using DynamicProperty. Do it manually.", version), e);
}
}
}
}
return this.currentVersion;
}
protected boolean wasVersionHandledBefore(final String version) {
// some check here
}
protected void doCostlyOperationThatInvolvesNetwork() {
// may take a few seconds
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment