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.
This commit is contained in:
Florian Schmaus 2014-05-26 17:36:00 +02:00
parent 3647a7fce5
commit 6fd4bb850e
3 changed files with 33 additions and 25 deletions

View File

@ -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;

View File

@ -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.
* <p>
* 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);
}

View File

@ -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<PingFailedListener> pingFailedListeners = Collections
.synchronizedSet(new HashSet<PingFailedListener>());
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();
}
}
}