This commit is contained in:
Ruben Bermudez 2019-04-03 02:24:15 +01:00
parent 140574f54e
commit 3861b29b25

View File

@ -29,6 +29,7 @@ import java.nio.charset.StandardCharsets;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT; import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT;
@ -40,7 +41,7 @@ import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT;
public class DefaultBotSession implements BotSession { public class DefaultBotSession implements BotSession {
private static final String LOGTAG = "BOTSESSION"; private static final String LOGTAG = "BOTSESSION";
private volatile boolean running = false; private AtomicBoolean running = new AtomicBoolean(false);
private final ConcurrentLinkedDeque<Update> receivedUpdates = new ConcurrentLinkedDeque<>(); private final ConcurrentLinkedDeque<Update> receivedUpdates = new ConcurrentLinkedDeque<>();
private final ObjectMapper objectMapper = new ObjectMapper(); private final ObjectMapper objectMapper = new ObjectMapper();
@ -59,11 +60,11 @@ public class DefaultBotSession implements BotSession {
@Override @Override
public synchronized void start() { public synchronized void start() {
if (running) { if (running.get()) {
throw new IllegalStateException("Session already running"); throw new IllegalStateException("Session already running");
} }
running = true; running.set(true);
lastReceivedUpdate = 0; lastReceivedUpdate = 0;
@ -78,11 +79,11 @@ public class DefaultBotSession implements BotSession {
@Override @Override
public synchronized void stop() { public synchronized void stop() {
if (!running) { if (!running.get()) {
throw new IllegalStateException("Session already stopped"); throw new IllegalStateException("Session already stopped");
} }
running = false; running.set(false);
if (readerThread != null) { if (readerThread != null) {
readerThread.interrupt(); readerThread.interrupt();
@ -126,8 +127,8 @@ public class DefaultBotSession implements BotSession {
} }
@Override @Override
public synchronized boolean isRunning() { public boolean isRunning() {
return running; return running.get();
} }
private class ReaderThread extends Thread implements UpdatesReader { private class ReaderThread extends Thread implements UpdatesReader {
@ -178,9 +179,9 @@ public class DefaultBotSession implements BotSession {
@Override @Override
public void run() { public void run() {
setPriority(Thread.MIN_PRIORITY); setPriority(Thread.MIN_PRIORITY);
while (running) { while (running.get()) {
synchronized (lock) { synchronized (lock) {
if (running) { if (running.get()) {
try { try {
List<Update> updates = updatesSupplier.getUpdates(); List<Update> updates = updatesSupplier.getUpdates();
if (updates.isEmpty()) { if (updates.isEmpty()) {
@ -199,7 +200,7 @@ public class DefaultBotSession implements BotSession {
} }
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
if (!running) { if (!running.get()) {
receivedUpdates.clear(); receivedUpdates.clear();
} }
BotLogger.debug(LOGTAG, e); BotLogger.debug(LOGTAG, e);
@ -211,7 +212,7 @@ public class DefaultBotSession implements BotSession {
lock.wait(exponentialBackOff.nextBackOffMillis()); lock.wait(exponentialBackOff.nextBackOffMillis());
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
if (!running) { if (!running.get()) {
receivedUpdates.clear(); receivedUpdates.clear();
} }
BotLogger.debug(LOGTAG, e); BotLogger.debug(LOGTAG, e);
@ -290,7 +291,7 @@ public class DefaultBotSession implements BotSession {
@Override @Override
public void run() { public void run() {
setPriority(Thread.MIN_PRIORITY); setPriority(Thread.MIN_PRIORITY);
while (running) { while (running.get()) {
try { try {
List<Update> updates = getUpdateList(); List<Update> updates = getUpdateList();
if (updates.isEmpty()) { if (updates.isEmpty()) {