Search in sources :

Example 1 with ThrowableList

use of com.openmeap.util.ThrowableList in project OpenMEAP by OpenMEAP.

the class AbstractClusterServiceMgmtNotifier method notify.

public <E extends Event<T>> void notify(final E event, List<ProcessingEvent> events) throws ClusterNotificationException {
    final ThrowableList exceptions = new ThrowableList();
    onBeforeNotify(event);
    final Map<String, Boolean> urlRequestCompleteStatus = new HashMap<String, Boolean>();
    GlobalSettings globalSettings = modelManager.getGlobalSettings();
    List<ClusterNode> clusterNodes = globalSettings.getClusterNodes();
    for (ClusterNode thisNode : clusterNodes) {
        URL thisUrl = null;
        try {
            thisUrl = new URL(thisNode.getServiceWebUrlPrefix());
        } catch (MalformedURLException e) {
            logger.error("Could not create URL object from " + thisNode.getServiceWebUrlPrefix() + ": {}", e);
            continue;
        }
        if (executorService != null) {
            logger.debug("Making request to {} using the executor.", thisUrl);
            executorService.execute(new Runnable() {

                URL url;

                public void run() {
                    notifyMakeRequest(exceptions, url, urlRequestCompleteStatus, event);
                }

                Runnable setUrl(URL url) {
                    this.url = url;
                    return this;
                }
            }.setUrl(thisUrl));
        } else {
            logger.debug("Making request to {} serially.", thisUrl);
            notifyMakeRequest(exceptions, thisUrl, urlRequestCompleteStatus, event);
        }
    }
    // this set of if-else handles any exceptions in the list accumulated
    if (executorService != null && executorTimeout != null) {
        try {
            // terminate, but make sure nothing is still waiting when we terminate
            if (!executorService.awaitTermination(executorTimeout, TimeUnit.SECONDS)) {
                executorService.shutdownNow();
                List<String> waiting = new ArrayList<String>();
                for (Map.Entry<String, Boolean> completed : urlRequestCompleteStatus.entrySet()) {
                    if (completed.getValue().equals(Boolean.FALSE)) {
                        waiting.add(completed.getKey());
                    }
                }
                logger.error("Blocking timed-out still waiting to notify: {}", StringUtils.join(waiting, ", "));
                throw new ClusterNotificationException(String.format("Blocking timed-out still waiting to notify: %s", StringUtils.join(waiting, ", ")));
            }
        } catch (InterruptedException ie) {
            throw new ClusterNotificationException("The notification thread was interrupted", ie);
        }
    } else if (exceptions.size() > 0) {
        throw new ClusterNotificationException(String.format("The following exceptions were thrown: %s", exceptions.getMessages()));
    }
    onAfterNotify(event);
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) ThrowableList(com.openmeap.util.ThrowableList) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GlobalSettings(com.openmeap.model.dto.GlobalSettings) URL(java.net.URL) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ClusterNode (com.openmeap.model.dto.ClusterNode)1 GlobalSettings (com.openmeap.model.dto.GlobalSettings)1 ThrowableList (com.openmeap.util.ThrowableList)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1