Skip to content

Commit

Permalink
JNG-5995 Retry logic for setup
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcsakany committed Nov 28, 2024
1 parent dc59cb5 commit 3ce041b
Showing 1 changed file with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

import static java.util.stream.Collectors.joining;

Expand Down Expand Up @@ -114,6 +115,31 @@ public WrappedEmfModelContext(

public WrappedEmfModelContext() {
}

private void retry(Runnable executeable) {
synchronized (resource) {
int cnt = 0;
boolean success = false;
ConcurrentModificationException exception = null;
while (cnt < 10 && !success) {
try {
executeable.run();
success = true;
} catch (ConcurrentModificationException e) {
try {
Thread.sleep(new Random().nextInt(10) + 10);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
exception = e;
}
}
if (!success && exception != null) {
throw exception;
}
}

}
@Override
public IModel load(Logger log, ResourceSet resourceSet, ModelRepository repository, Map<String, URI> uris, Map<URI, URI> uriConverterMap) throws EolModelLoadingException, ModelValidationException {
emfModel = new InMemoryEmfModel(name, resource, resource.getResourceSet().getPackageRegistry().values().stream().map(o -> (EPackage) o).toList()) {
Expand All @@ -128,27 +154,17 @@ public Object getCacheKeyForType(String type) throws EolModelElementTypeNotFound

@Override
synchronized public void setupContainmentChangeListeners() {
synchronized (resource) {
int cnt = 0;
boolean success = false;
ConcurrentModificationException exception = null;
while (cnt < 10 && !success) {
try {
super.setupContainmentChangeListeners();
success = true;
} catch (ConcurrentModificationException e) {
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
exception = e;
}
}
if (!success && exception != null) {
throw exception;
}
}
retry(() -> super.setupContainmentChangeListeners());
}

@Override
protected void removeContentsAdapter() {
retry(() -> super.removeContentsAdapter());
}

@Override
protected void addContentsAdapter() {
retry(() -> super.addContentsAdapter());
}
};
emfModel.setName(name);
Expand Down

0 comments on commit 3ce041b

Please sign in to comment.