Skip to content

Commit

Permalink
Use threads instead of parallel streams.
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Jan 2, 2024
1 parent 23c3acc commit 4ed4c58
Showing 1 changed file with 53 additions and 32 deletions.
85 changes: 53 additions & 32 deletions src/main/java/com/danubetech/libindy/IndyConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,41 +146,62 @@ public synchronized void openIndyConnections(boolean createSubmitterDid, boolean

Map<String, IndyConnection> indyConnections = Collections.synchronizedMap(new LinkedHashMap<>());
List<IndyConnectionException> exceptions = Collections.synchronizedList(new ArrayList<>());
poolConfigFiles.keySet().parallelStream().forEach(network -> {
String poolConfigName = poolConfigNames.get(network);
String poolConfigFile = poolConfigFiles.get(network);
Integer poolVersion = poolVersions.get(network);
Boolean nativeDidIndy = nativeDidIndys.get(network);
Boolean nymAddSignMulti = nymAddSignMultis.get(network);
Boolean nymEditSignMulti = nymEditSignMultis.get(network);
Boolean attribAddSignMulti = attribAddSignMultis.get(network);
Boolean attribEditSignMulti = attribEditSignMultis.get(network);
String walletName = walletNames.get(network);
String submitterDidSeed = submitterDidSeeds.get(network);
Long genesisTimestamp = genesisTimestamps.get(network);

if (poolConfigName == null) exceptions.add(new IndyConnectionException("No 'poolConfigName' for network: " + network));
if (poolConfigFile == null) exceptions.add(new IndyConnectionException("No 'poolConfigFile' for network: " + network));
if (poolVersion == null) exceptions.add(new IndyConnectionException("No 'poolVersion' for network: " + network));
if (nativeDidIndy == null) exceptions.add(new IndyConnectionException("No 'nativeDidIndy' for network: " + network));
if (nymAddSignMulti == null) exceptions.add(new IndyConnectionException("No 'nymAddSignMulti' for network: " + network));
if (nymEditSignMulti == null) exceptions.add(new IndyConnectionException("No 'nymEditSignMulti' for network: " + network));
if (attribAddSignMulti == null) exceptions.add(new IndyConnectionException("No 'attribAddSignMulti' for network: " + network));
if (attribEditSignMulti == null) exceptions.add(new IndyConnectionException("No 'attribEditSignMulti' for network: " + network));
if (walletName == null) exceptions.add(new IndyConnectionException("No 'walletName' for network: " + network));
if (submitterDidSeed == null) exceptions.add(new IndyConnectionException("No 'submitterDidSeed' for network: " + network));
if (poolVersion == null || nativeDidIndy == null || nymAddSignMulti == null || nymEditSignMulti == null || attribAddSignMulti == null || attribEditSignMulti == null || walletName == null || submitterDidSeed == null) return;
IndyConnection indyConnection = new IndyConnection(network, poolConfigName, poolConfigFile, poolVersion, nativeDidIndy, nymAddSignMulti, nymEditSignMulti, attribAddSignMulti, attribEditSignMulti, walletName, submitterDidSeed, genesisTimestamp);
List<Thread> threads = new ArrayList<>(poolConfigFiles.size());

for (String network : poolConfigFiles.keySet()) {
Thread thread = new Thread(() -> {
String poolConfigName = poolConfigNames.get(network);
String poolConfigFile = poolConfigFiles.get(network);
Integer poolVersion = poolVersions.get(network);
Boolean nativeDidIndy = nativeDidIndys.get(network);
Boolean nymAddSignMulti = nymAddSignMultis.get(network);
Boolean nymEditSignMulti = nymEditSignMultis.get(network);
Boolean attribAddSignMulti = attribAddSignMultis.get(network);
Boolean attribEditSignMulti = attribEditSignMultis.get(network);
String walletName = walletNames.get(network);
String submitterDidSeed = submitterDidSeeds.get(network);
Long genesisTimestamp = genesisTimestamps.get(network);

if (poolConfigName == null) exceptions.add(new IndyConnectionException("No 'poolConfigName' for network: " + network));
if (poolConfigFile == null) exceptions.add(new IndyConnectionException("No 'poolConfigFile' for network: " + network));
if (poolVersion == null) exceptions.add(new IndyConnectionException("No 'poolVersion' for network: " + network));
if (nativeDidIndy == null) exceptions.add(new IndyConnectionException("No 'nativeDidIndy' for network: " + network));
if (nymAddSignMulti == null) exceptions.add(new IndyConnectionException("No 'nymAddSignMulti' for network: " + network));
if (nymEditSignMulti == null) exceptions.add(new IndyConnectionException("No 'nymEditSignMulti' for network: " + network));
if (attribAddSignMulti == null) exceptions.add(new IndyConnectionException("No 'attribAddSignMulti' for network: " + network));
if (attribEditSignMulti == null) exceptions.add(new IndyConnectionException("No 'attribEditSignMulti' for network: " + network));
if (walletName == null) exceptions.add(new IndyConnectionException("No 'walletName' for network: " + network));
if (submitterDidSeed == null) exceptions.add(new IndyConnectionException("No 'submitterDidSeed' for network: " + network));
if (poolVersion == null || nativeDidIndy == null || nymAddSignMulti == null || nymEditSignMulti == null || attribAddSignMulti == null || attribEditSignMulti == null || walletName == null || submitterDidSeed == null) return;

IndyConnection indyConnection = new IndyConnection(network, poolConfigName, poolConfigFile, poolVersion, nativeDidIndy, nymAddSignMulti, nymEditSignMulti, attribAddSignMulti, attribEditSignMulti, walletName, submitterDidSeed, genesisTimestamp);
try {
indyConnection.open(createSubmitterDid, retrieveTaa);
} catch (IndyConnectionException ex) {
if (log.isWarnEnabled()) log.warn("Exception while opening Indy connection for network " + network);
exceptions.add(ex);
}

if (log.isInfoEnabled()) log.info("Adding Indy connection for network " + network + ": " + indyConnection);
indyConnections.put(network, indyConnection);
});
threads.add(thread);
thread.start();
try {
indyConnection.open(createSubmitterDid, retrieveTaa);
} catch (IndyConnectionException ex) {
if (log.isWarnEnabled()) log.warn("Exception while opening Indy connection for network " + network);
exceptions.add(ex);
Thread.sleep(500);
} catch (InterruptedException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
if (log.isInfoEnabled()) log.info("Started thread " + thread.getName());
}

if (log.isInfoEnabled()) log.info("Adding Indy connection for network " + network + ": " + indyConnection);
indyConnections.put(network, indyConnection);
});
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}

if (! exceptions.isEmpty()) {
StringBuilder errorMessage = new StringBuilder();
Expand Down

0 comments on commit 4ed4c58

Please sign in to comment.