services.DatafeedEventsService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of symphony-api-client-java Show documentation
Show all versions of symphony-api-client-java Show documentation
Symphony API Client provided by Symphony Platform Solutions team
package services;
import clients.SymBotClient;
import clients.symphony.api.DatafeedClient;
import configuration.SymConfig;
import configuration.SymLoadBalancedConfig;
import exceptions.SymClientException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.ws.rs.ProcessingException;
import listeners.*;
import model.DatafeedEvent;
import model.events.MessageSent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DatafeedEventsService {
private final Logger logger = LoggerFactory.getLogger(DatafeedEventsService.class);
private SymBotClient botClient;
private DatafeedClient datafeedClient;
private List roomListeners;
private List imListeners;
private List connectionListeners;
private List elementsListeners;
private String datafeedId = null;
private ExecutorService pool;
private AtomicBoolean stop = new AtomicBoolean();
private static int THREADPOOL_SIZE;
private static int TIMEOUT_NO_OF_SECS;
public DatafeedEventsService(SymBotClient client) {
this.roomListeners = new ArrayList<>();
this.imListeners = new ArrayList<>();
this.connectionListeners = new ArrayList<>();
this.elementsListeners = new ArrayList<>();
this.botClient = client;
this.datafeedClient = this.botClient.getDatafeedClient();
int threadPoolSize = client.getConfig().getDatafeedEventsThreadpoolSize();
THREADPOOL_SIZE = threadPoolSize > 0 ? threadPoolSize : 5;
resetTimeout();
while (datafeedId == null) {
try {
datafeedId = datafeedClient.createDatafeed();
resetTimeout();
} catch (ProcessingException e) {
handleError(e);
}
}
readDatafeed();
stop.set(false);
}
private void resetTimeout() {
int errorTimeout = this.botClient.getConfig().getDatafeedEventsErrorTimeout();
TIMEOUT_NO_OF_SECS = errorTimeout > 0 ? errorTimeout : 30;
}
public void addListeners(DatafeedListener... listeners) {
for (DatafeedListener listener : listeners) {
if (listener instanceof RoomListener) {
addRoomListener((RoomListener) listener);
} else if (listener instanceof IMListener) {
addIMListener((IMListener) listener);
} else if (listener instanceof ConnectionListener) {
addConnectionsListener((ConnectionListener) listener);
} else if (listener instanceof ElementsListener) {
addElementsListener((ElementsListener) listener);
}
}
}
public void removeListeners(DatafeedListener... listeners) {
for (DatafeedListener listener : listeners) {
if (listener instanceof RoomListener) {
removeRoomListener((RoomListener) listener);
} else if (listener instanceof IMListener) {
removeIMListener((IMListener) listener);
} else if (listener instanceof ConnectionListener) {
removeConnectionsListener((ConnectionListener) listener);
} else if (listener instanceof ElementsListener) {
removeElementsListener((ElementsListener) listener);
}
}
}
public void addRoomListener(RoomListener listener) {
roomListeners.add(listener);
}
public void removeRoomListener(RoomListener listener) {
roomListeners.remove(listener);
}
public void addIMListener(IMListener listener) {
imListeners.add(listener);
}
public void removeIMListener(IMListener listener) {
imListeners.remove(listener);
}
public void addConnectionsListener(ConnectionListener listener) {
connectionListeners.add(listener);
}
public void removeConnectionsListener(ConnectionListener listener) {
connectionListeners.remove(listener);
}
public void addElementsListener(ElementsListener listener) {
elementsListeners.add(listener);
}
public void removeElementsListener(ElementsListener listener) {
elementsListeners.remove(listener);
}
public void readDatafeed() {
if (pool != null) {
pool.shutdown();
}
pool = Executors.newFixedThreadPool(THREADPOOL_SIZE);
CompletableFuture.supplyAsync(() -> {
while (!stop.get()) {
CompletableFuture
© 2015 - 2025 Weber Informatics LLC | Privacy Policy