From b204ad4ee15cc4937cb99de9823747e86dc01d10 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 5 Oct 2020 21:31:27 +0300 Subject: [PATCH] Store default exception handlers as static map in Java example. GitOrigin-RevId: 5745859a70250d104a21edad473b655967060609 --- example/java/org/drinkless/tdlib/Client.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/example/java/org/drinkless/tdlib/Client.java b/example/java/org/drinkless/tdlib/Client.java index 9050e467b..986d5b3c7 100644 --- a/example/java/org/drinkless/tdlib/Client.java +++ b/example/java/org/drinkless/tdlib/Client.java @@ -103,15 +103,6 @@ public final class Client implements Runnable { return nativeClientExecute(query); } - /** - * Replaces default exception handler to be invoked on exceptions thrown from updateHandler and all other ResultHandler. - * - * @param defaultExceptionHandler Default exception handler. If null Exceptions are ignored. - */ - public void setDefaultExceptionHandler(Client.ExceptionHandler defaultExceptionHandler) { - this.defaultExceptionHandler = defaultExceptionHandler; - } - /** * Overridden method from Runnable, do not call it directly. */ @@ -156,6 +147,7 @@ public final class Client implements Runnable { receiveQueries(300.0); } updateHandlers.remove(nativeClientId); + defaultExceptionHandlers.remove(nativeClientId); destroyNativeClient(nativeClientId); } finally { writeLock.unlock(); @@ -170,13 +162,12 @@ public final class Client implements Runnable { private volatile boolean isClientDestroyed = false; private final long nativeClientId; + private static final ConcurrentHashMap defaultExceptionHandlers = new ConcurrentHashMap(); private static final ConcurrentHashMap updateHandlers = new ConcurrentHashMap(); private final ConcurrentHashMap handlers = new ConcurrentHashMap(); private final AtomicLong currentQueryId = new AtomicLong(); - private volatile ExceptionHandler defaultExceptionHandler = null; - private static final int MAX_EVENTS = 1000; private final long[] eventIds = new long[MAX_EVENTS]; private final TdApi.Object[] events = new TdApi.Object[MAX_EVENTS]; @@ -194,7 +185,9 @@ public final class Client implements Runnable { private Client(ResultHandler updateHandler, ExceptionHandler updateExceptionHandler, ExceptionHandler defaultExceptionHandler) { nativeClientId = createNativeClient(); updateHandlers.put(nativeClientId, new Handler(updateHandler, updateExceptionHandler)); - this.defaultExceptionHandler = defaultExceptionHandler; + if (defaultExceptionHandler != null) { + defaultExceptionHandlers.put(nativeClientId, defaultExceptionHandler); + } } @Override @@ -235,7 +228,7 @@ public final class Client implements Runnable { resultHandler.onResult(object); } catch (Throwable cause) { if (exceptionHandler == null) { - exceptionHandler = defaultExceptionHandler; + exceptionHandler = defaultExceptionHandlers.get(nativeClientId); } if (exceptionHandler != null) { try {