/** * Publish message on given topic. Message is optional. */ public synchronized void publish(Topic topic, String message) { Preconditions.checkNotNull(topic, "Topic cannot be null"); Set<Subscriber> subscribers = subscriberMap.get(topic); if (subscribers == null) { throw new IllegalArgumentException("cannot publish to unknown topic " + topic + ", existing topics " + subscriberMap.keySet()); } for (Subscriber subscriber : subscribers) { // Faulire of one subscriber to process message delivery should not affect // message delivery to other subscribers, therefore using try-catch. try { subscriber.onMessage(topic, message); } catch (Exception e) { LOGGER.error("Topic " + topic + ", message " + message + ", delivery error", e); } } LOGGER.info("Topic " + topic + ", message " + message + ": " + subscribers.size() + " subscribers called"); }