1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 05:52:06 +01:00

Move SmackExecutorThreadFactory out of

AbstractXMPPConnection. And use it in PingManager.
This commit is contained in:
Florian Schmaus 2014-12-19 12:53:16 +01:00
parent eb48d02673
commit 18ac83cf8c
3 changed files with 45 additions and 42 deletions

View file

@ -34,7 +34,6 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@ -73,6 +72,7 @@ import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.rosterstore.RosterStore;
import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.SmackExecutorThreadFactory;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.dns.HostAddress;
import org.jxmpp.util.XmppStringUtils;
@ -238,29 +238,6 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
// @formatter:on
);
/**
* SmackExecutorThreadFactory is a *static* inner class of XMPPConnection. Note that we must not
* use anonymous classes in order to prevent threads from leaking.
*/
private static final class SmackExecutorThreadFactory implements ThreadFactory {
private final int connectionCounterValue;
private final String name;
private int count = 0;
private SmackExecutorThreadFactory(int connectionCounterValue, String name) {
this.connectionCounterValue = connectionCounterValue;
this.name = name;
}
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable);
thread.setName("Smack Executor - " + name + ' ' + count++ + " (" + connectionCounterValue + ")");
thread.setDaemon(true);
return thread;
}
}
private Roster roster;
/**

View file

@ -0,0 +1,42 @@
/**
*
* Copyright 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
import java.util.concurrent.ThreadFactory;
/**
* SmackExecutorThreadFactory creates daemon threads with a particular name. Note that you should
* not use anonymous inner classes for thread factories in order to prevent threads from leaking.
*/
public final class SmackExecutorThreadFactory implements ThreadFactory {
private final int connectionCounterValue;
private final String name;
private int count = 0;
public SmackExecutorThreadFactory(int connectionCounterValue, String name) {
this.connectionCounterValue = connectionCounterValue;
this.name = name;
}
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable);
thread.setName("Smack Executor - " + name + ' ' + count++ + " (" + connectionCounterValue + ")");
thread.setDaemon(true);
return thread;
}
}

View file

@ -24,7 +24,6 @@ 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;
@ -45,6 +44,7 @@ import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.SmackExecutorThreadFactory;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.ping.packet.Ping;
@ -106,22 +106,6 @@ public class PingManager extends Manager {
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.
*/
@ -132,7 +116,7 @@ public class PingManager extends Manager {
private PingManager(XMPPConnection connection) {
super(connection);
executorService = new ScheduledThreadPoolExecutor(1,
new PingExecutorThreadFactory(connection.getConnectionCounter()));
new SmackExecutorThreadFactory(connection.getConnectionCounter(), "Ping"));
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
sdm.addFeature(Ping.NAMESPACE);
INSTANCES.put(connection, this);