public NotificationMonitor(Notifications type, int serverPort, BtcdClient client) { LOG.info("** NotificationMonitor(): launching new '{}' notification monitor (port: '{}', " + "RPC-capable: '{}')", type.name(), serverPort, ((client == null) ? "no" : "yes")); this.type = type; this.serverPort = serverPort; this.client = client; }
private void deactivate() { LOG.info(">> deactivate(..): attempting to shut down '{}' notification monitor (port: '{}', " + "RPC-capable: '{}')", type.name(), serverSocket.getLocalPort(), ((client == null) ? "no" : "yes")); isActive = false; try { serverSocket.close(); } catch (IOException e) { LOG.warn("-- deactivate(..): failed to close server socket (monitor: '{}', port: '{}'), " + "message was: '{}'", type.name(), serverSocket.getLocalPort(), e.getMessage()); } workerPool.shutdown(); }
public NotificationWorker(Socket socket, BtcdClient client) { LOG.debug("** NotificationWorker(): launching new '{}' notification worker (RPC-capable: " + "'{}')", getType().name(), ((client == null) ? "no" : "yes")); this.socket = socket; this.client = client; }
public boolean checkMonitorStates(Map<Notifications, Future<Void>> futures) { boolean isAllActive = true; for (Notifications notificationType : Notifications.values()) { Future<Void> monitorHandle = futures.get(notificationType); if ((monitorHandle == null) || (monitorHandle.isDone())) { LOG.warn("-- checkMonitorStates(..): no active '{}' notification monitor detected " + "(reason: {})", notificationType.name(), ((monitorHandle == null) ? "failure to launch" : "task terminated prematurely")); isAllActive = false; } } return isAllActive; } }
private String getUniqueName() { return "NotificationMonitor[" + StringUtils.capitalize(type.name().toLowerCase()) + "]-" + StringUtils.random(4, Constants.DECIMAL_DIGITS); } }
private void activate() { Thread.currentThread().setName(getUniqueName()); isActive = true; try { serverSocket = new ServerSocket(serverPort); serverSocket.setSoTimeout(IDLE_SOCKET_TIMEOUT); } catch (IOException e) { try { serverSocket = new ServerSocket(0); serverSocket.setSoTimeout(IDLE_SOCKET_TIMEOUT); LOG.warn("-- activate(..): failed to create server socket (monitor: '{}', port: " + "'{}'), reverting to unused port '{}'", type.name(), serverPort, serverSocket.getLocalPort()); } catch (IOException e1) { throw new NotificationHandlerException(Errors.IO_SERVERSOCKET_UNINITIALIZED, e); } } workerPool = new ThreadPoolExecutor(WORKER_MIN_COUNT, WORKER_MAX_COUNT, IDLE_WORKER_TIMEOUT, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(TASK_QUEUE_LENGTH)); }
@Override public void run() { activate(); LOG.info("-- run(..): started listening for '{}' notifications on port '{}'", type.name(), serverSocket.getLocalPort()); while (isActive) { try { Socket socket = serverSocket.accept(); NotificationWorker worker = NotificationWorkerFactory.createWorker(type, socket, client); worker.addObserver(this); workerPool.submit(worker); LOG.trace("-- run(..): total no. of '{}' notifications received: '{}', task queue " + "occupancy: '{}/{}'", type.name(), workerPool.getTaskCount(), workerPool.getQueue().size(), TASK_QUEUE_LENGTH); } catch (SocketTimeoutException e) { LOG.trace("-- run(..): polling '{}' notification monitor for interrupts (socket idle " + "for {}ms)", type.name(), IDLE_SOCKET_TIMEOUT); } catch (IOException e) { Thread.currentThread().interrupt(); throw new NotificationHandlerException(Errors.IO_SOCKET_UNINITIALIZED, e); } finally { if (Thread.interrupted()) { deactivate(); } } } }
@Override public synchronized void update(Observable worker, Object result) { LOG.info(">> update(..): worker finished, informing listener(s) of new '{}' notification: " + "'{}'", type.name(), result); worker.deleteObserver(this); setChanged(); notifyObservers(result); }
LOG.debug("-- run(..): received new '{}' notification as (raw): '{}'", getType().name(), notification); Object relatedEntity = getRelatedEntity(notification); try { LOG.debug("-- run(..): attempting to recycle old '{}' notification worker (RPC-" + "capable: '{}')", getType().name(), ((client == null) ? "no" : "yes")); socket.close(); } catch (IOException e) { LOG.warn("<< run(..): failed to close socket (worker: '{}', port: '{}'), message " + "was: '{}'", getType().name(), socket.getLocalPort(), e.getMessage());