From a87e8c63842d96c192dd7fbb5ed11ecb67835744 Mon Sep 17 00:00:00 2001 From: Christian Pape Date: Tue, 19 Nov 2024 11:24:44 +0100 Subject: [PATCH] Parallel add and delete --- .../de/hsfulda/informatik/AccountSource.java | 56 +++++++++++-------- .../java/de/hsfulda/informatik/LdapSync.java | 4 +- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/hsfulda/informatik/AccountSource.java b/src/main/java/de/hsfulda/informatik/AccountSource.java index 55bbda8..f70ba25 100644 --- a/src/main/java/de/hsfulda/informatik/AccountSource.java +++ b/src/main/java/de/hsfulda/informatik/AccountSource.java @@ -12,11 +12,14 @@ import com.unboundid.ldap.sdk.SearchScope; import com.unboundid.ldap.sdk.extensions.StartTLSExtendedRequest; import com.unboundid.util.ssl.SSLUtil; import com.unboundid.util.ssl.TrustAllTrustManager; +import sun.nio.ch.ThreadPool; import java.security.GeneralSecurityException; import java.util.Collections; import java.util.List; import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.stream.Collectors; public class AccountSource { @@ -24,6 +27,7 @@ public class AccountSource { final List searchResultEntryList; final Set users; final String baseDN; + final ExecutorService executorService = Executors.newFixedThreadPool(24); AccountSource(final String baseDN, final List searchResultEntryList) { this.baseDN = baseDN; @@ -65,37 +69,45 @@ public class AccountSource { public void add(final List usersToBeAdded) { for (final Entry entry : usersToBeAdded) { System.out.print("-> Füge Benutzer " + entry.getDN() + "..."); - final LDAPResult ldapResult; - try { - ldapResult = connection.add(entry); - - if (ldapResult.getResultCode().intValue() == 0) { - System.out.print("Ok\n"); - } else { - System.out.print("Fehler (" + ldapResult.getResultCode() + ")\n"); + executorService.submit(new Runnable() { + @Override + public void run() { + final LDAPResult ldapResult; + try { + ldapResult = connection.add(entry); + if (ldapResult.getResultCode().intValue() == 0) { + System.out.print("Ok\n"); + } else { + System.out.print("Fehler (" + ldapResult.getResultCode() + ")\n"); + } + } catch (LDAPException e) { + System.out.print("Fehler (" + e.getMessage() + ")\n"); + } } - } catch (LDAPException e) { - System.out.print("Fehler (" + e.getMessage() + ")\n"); - } + }); } } public void del(final Set usersToBeDeleted) { for (final String user : usersToBeDeleted) { System.out.print("-> Lösche Benutzer cn=" + user + "," + this.baseDN + "..."); - final LDAPResult ldapResult; - - try { - ldapResult = connection.delete("cn=" + user + "," + this.baseDN); + executorService.submit(new Runnable() { + @Override + public void run() { + final LDAPResult ldapResult; + try { + ldapResult = connection.delete("cn=" + user + "," + AccountSource.this.baseDN); - if (ldapResult.getResultCode().intValue() == 0) { - System.out.print("Ok\n"); - } else { - System.out.print("Fehler (" + ldapResult.getResultCode() + ")\n"); + if (ldapResult.getResultCode().intValue() == 0) { + System.out.print("Ok\n"); + } else { + System.out.print("Fehler (" + ldapResult.getResultCode() + ")\n"); + } + } catch (LDAPException e) { + System.out.print("Fehler (" + e.getMessage() + ")\n"); + } } - } catch (LDAPException e) { - System.out.print("Fehler (" + e.getMessage() + ")\n"); - } + }); } } } diff --git a/src/main/java/de/hsfulda/informatik/LdapSync.java b/src/main/java/de/hsfulda/informatik/LdapSync.java index 0dc0013..efab050 100644 --- a/src/main/java/de/hsfulda/informatik/LdapSync.java +++ b/src/main/java/de/hsfulda/informatik/LdapSync.java @@ -49,7 +49,7 @@ public class LdapSync { new String[]{"cn", "givenname", "sn", "uid"} ); - System.out.print(remote.getUsers().size() + "entrie(s) received\nAbfrage der Benutzer im OpenLDAP..."); + System.out.print(remote.getUsers().size() + " entrie(s) received\nAbfrage der Benutzer im OpenLDAP..."); // lade Daten des lokalen Systems final AccountSource local = new AccountSource( @@ -62,7 +62,7 @@ public class LdapSync { new String[]{} ); - System.out.print(remote.getUsers().size() + "entrie(s) received\n"); + System.out.print(remote.getUsers().size() + " entrie(s) received\n"); sync(remote, local); }