mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-25 15:52:06 +01:00
Make executorService blocking in AbstractXMPPConnection
Otherwise AbstractXMPPConnection.processPacket() will throw a RejectedExecutionException if the underlying queue is full. Fixes SMACK-702.
This commit is contained in:
parent
cc758b8f59
commit
81fb1ed93c
2 changed files with 69 additions and 6 deletions
|
@ -27,14 +27,12 @@ import java.util.LinkedList;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
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.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
@ -73,6 +71,7 @@ import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
|
|||
import org.jivesoftware.smack.parsing.UnparsablePacket;
|
||||
import org.jivesoftware.smack.provider.ExtensionElementProvider;
|
||||
import org.jivesoftware.smack.provider.ProviderManager;
|
||||
import org.jivesoftware.smack.util.BoundedThreadPoolExecutor;
|
||||
import org.jivesoftware.smack.util.DNSUtil;
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
|
@ -237,8 +236,8 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
|
|||
* important that we use a <b>single threaded ExecutorService</b> in order to guarantee that the
|
||||
* PacketListeners are invoked in the same order the stanzas arrived.
|
||||
*/
|
||||
private final ThreadPoolExecutor executorService = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
|
||||
new ArrayBlockingQueue<Runnable>(100), new SmackExecutorThreadFactory(connectionCounterValue, "Incoming Processor"));
|
||||
private final BoundedThreadPoolExecutor executorService = new BoundedThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
|
||||
100, new SmackExecutorThreadFactory(connectionCounterValue, "Incoming Processor"));
|
||||
|
||||
/**
|
||||
* This scheduled thread pool executor is used to remove pending callbacks.
|
||||
|
@ -976,12 +975,13 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
|
|||
* they are a match with the filter.
|
||||
*
|
||||
* @param packet the stanza(/packet) to process.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
protected void processPacket(Stanza packet) {
|
||||
protected void processPacket(Stanza packet) throws InterruptedException {
|
||||
assert(packet != null);
|
||||
lastStanzaReceived = System.currentTimeMillis();
|
||||
// Deliver the incoming packet to listeners.
|
||||
executorService.submit(new ListenerNotification(packet));
|
||||
executorService.executeBlocking(new ListenerNotification(packet));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2015 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.ArrayBlockingQueue;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class BoundedThreadPoolExecutor extends ThreadPoolExecutor {
|
||||
|
||||
private final Semaphore semaphore;
|
||||
|
||||
public BoundedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
|
||||
TimeUnit unit, int bound, ThreadFactory threadFactory) {
|
||||
// One could think that the array blocking queue bound should be "bound - 1" because the bound protected by the
|
||||
// Semaphore also includes the "slot" in the worker thread executing the Runnable. But using that as bound could
|
||||
// actually cause a RejectedExecutionException as the queue could fill up while the worker thread remains
|
||||
// unscheduled and is thus not removing any entries.
|
||||
super(corePoolSize, maximumPoolSize, keepAliveTime,
|
||||
unit, new ArrayBlockingQueue<Runnable>(bound), threadFactory);
|
||||
semaphore = new Semaphore(bound);
|
||||
}
|
||||
|
||||
public void executeBlocking(final Runnable command) throws InterruptedException {
|
||||
semaphore.acquire();
|
||||
try {
|
||||
execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
command.run();
|
||||
} finally {
|
||||
semaphore.release();
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
semaphore.release();
|
||||
if (e instanceof RejectedExecutionException) {
|
||||
throw (RejectedExecutionException) e;
|
||||
} else {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue