From 6fd4bb850e0b98ad9bfe26b03950fec50b70a8ff Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 26 May 2014 17:36:00 +0200 Subject: [PATCH] Remove schedule() from XMPPConnection interface The idea that we abstract the scheduling of tasks on Android over this method turned out to be unnecessary. schedule() was also not really part of the *public* XMPPConnection API, so it's good that it's gone. --- .../smack/AbstractXMPPConnection.java | 7 ---- .../jivesoftware/smack/XMPPConnection.java | 17 ---------- .../jivesoftware/smackx/ping/PingManager.java | 34 ++++++++++++++++++- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java index 486e7bc0e..13c7d14b9 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java @@ -26,10 +26,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -1039,11 +1037,6 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { } } - @Override - public ScheduledFuture schedule(Runnable command, long delay, TimeUnit unit) { - return executorService.schedule(command, delay, unit); - } - @Override public int getConnectionCounter() { return connectionCounterValue; diff --git a/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java index 6d9e75ad1..a0987a112 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java @@ -16,9 +16,6 @@ */ package org.jivesoftware.smack; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.filter.IQReplyFilter; @@ -352,18 +349,4 @@ public interface XMPPConnection { * @return the currently active {@link FromMode} */ public FromMode getFromMode(); - - /** - * Schedule a Runnable related to this connection. - *

- * It is possible that the connection is blocked if the Runnable takes a considerably long - * amount to complete. So either make sure that it always finishes within a reasonably fast or - * use your own ScheduledExecutorService. - * - * @param command - * @param delay - * @param unit - * @return the ScheduldedFuture - */ - public ScheduledFuture schedule(Runnable command, long delay, TimeUnit unit); } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java index 6d0af7c2f..40813d4b1 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java @@ -21,7 +21,10 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.WeakHashMap; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; @@ -107,6 +110,24 @@ public class PingManager extends Manager { private final Set pingFailedListeners = Collections .synchronizedSet(new HashSet()); + private final ScheduledExecutorService executorService; + + private static class PingExecutorThreadFactory implements ThreadFactory { + private final int connectionCounterValue; + + public PingExecutorThreadFactory(int connectionCounterValue) { + this.connectionCounterValue = connectionCounterValue; + } + + @Override + public Thread newThread(Runnable runnable) { + Thread thread = new Thread(runnable, "Smack Scheduled Ping Executor Service (" + + connectionCounterValue + ")"); + thread.setDaemon(true); + return thread; + } + + } /** * The interval in seconds between pings are send to the users server. */ @@ -121,6 +142,8 @@ public class PingManager extends Manager { private PingManager(XMPPConnection connection) { super(connection); + executorService = new ScheduledThreadPoolExecutor(1, + new PingExecutorThreadFactory(connection.getConnectionCounter())); ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection); sdm.addFeature(PingManager.NAMESPACE); INSTANCES.put(connection, this); @@ -309,7 +332,7 @@ public class PingManager extends Manager { int nextPingIn = pingInterval - delta; LOGGER.fine("Scheduling ServerPingTask in " + nextPingIn + " seconds (pingInterval=" + pingInterval + ", delta=" + delta + ")"); - nextAutomaticPing = connection().schedule(pingServerRunnable, pingInterval, TimeUnit.SECONDS); + nextAutomaticPing = executorService.schedule(pingServerRunnable, pingInterval, TimeUnit.SECONDS); } } @@ -386,4 +409,13 @@ public class PingManager extends Manager { } } }; + + @Override + protected void finalize() throws Throwable { + try { + executorService.shutdown(); + } finally { + super.finalize(); + } + } }