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 1d3ed2d71..6ca816092 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java @@ -32,7 +32,7 @@ import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; @@ -297,6 +297,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { */ protected AbstractXMPPConnection(ConnectionConfiguration configuration) { config = configuration; + removeCallbacksService.setKeepAliveTime(30, TimeUnit.SECONDS); } protected ConnectionConfiguration getConfiguration() { @@ -1376,7 +1377,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { streamFeatures.put(key, feature); } - private final ScheduledExecutorService removeCallbacksService = new ScheduledThreadPoolExecutor(1, + private final ScheduledThreadPoolExecutor removeCallbacksService = new ScheduledThreadPoolExecutor(1, new SmackExecutorThreadFactory(connectionCounterValue, "Remove Callbacks")); @Override @@ -1550,4 +1551,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { cachedExecutorService.execute(runnable); } + protected final ScheduledFuture schedule(Runnable runnable, long delay, TimeUnit unit) { + return removeCallbacksService.schedule(runnable, delay, unit); + } } diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java index 334259254..a8d3c934f 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java @@ -121,6 +121,7 @@ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; @@ -1534,11 +1535,19 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { * @return the previous listener for this stanza ID or null. * @throws StreamManagementNotEnabledException if Stream Management is not enabled. */ - public PacketListener addStanzaIdAcknowledgedListener(String id, PacketListener listener) throws StreamManagementNotEnabledException { + public PacketListener addStanzaIdAcknowledgedListener(final String id, PacketListener listener) throws StreamManagementNotEnabledException { // Prevent users from adding callbacks that will never get removed if (!smWasEnabledAtLeastOnce) { throw new StreamManagementException.StreamManagementNotEnabledException(); } + // Remove the listener after max. 12 hours + final int removeAfterSeconds = Math.min(getMaxSmResumptionTime() + 60, 12 * 60 * 60); + schedule(new Runnable() { + @Override + public void run() { + stanzaIdAcknowledgedListeners.remove(id); + } + }, removeAfterSeconds, TimeUnit.SECONDS); return stanzaIdAcknowledgedListeners.put(id, listener); }