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.LoadBalancingMethod;
import configuration.SymConfig;
import configuration.SymLoadBalancedConfig;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
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 static final int MAX_BACKOFF_TIME = 5 * 60; // five minutes
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();
// if the reuseDatafeedID config isn't set (null), we assume its default value as true
if (botClient.getConfig().getReuseDatafeedID() == null || this.botClient.getConfig().getReuseDatafeedID()) {
try {
File file = new File("." + File.separator + "datafeed.id");
if (file.isDirectory()) {
file = new File("." + File.separator + "datafeed.id" + File.separator + "datafeed.id");
}
Path datafeedIdPath = Paths.get(file.getPath());
String[] persistedDatafeed = Files.readAllLines(datafeedIdPath).get(0).split("@");
datafeedId = persistedDatafeed[0];
if (client.getConfig() instanceof SymLoadBalancedConfig) {
SymLoadBalancedConfig lbConfig = (SymLoadBalancedConfig) client.getConfig();
String[] agentHostPort = persistedDatafeed[1].split(":");
if (lbConfig.getLoadBalancing().getMethod() == LoadBalancingMethod.external) {
lbConfig.setActualAgentHost(agentHostPort[0]);
} else {
int previousIndex = lbConfig.getAgentServers().indexOf(agentHostPort[0]);
lbConfig.setCurrentAgentIndex(previousIndex);
}
}
logger.info("Using previous datafeed id: {}", datafeedId);
} catch (IOException e) {
logger.info("No previous datafeed id file");
}
}
while (datafeedId == null) {
try {
datafeedId = datafeedClient.createDatafeed();
resetTimeout();
} catch (Exception 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