public void addSubscription(final Topic topic, final NotificationParams params) { // If listener is already subscribe to topic, override its notification params subscriptions.put(topic.getTopic(), params); }
private boolean unsubscribeMonitor() { boolean unsubscriptionDone = true; try { LOGGER.debug("Removing subscription from topic {}", monitorTopic.getTopic()); listenerContainer.removeMessageListener(monitorListener, monitorTopic); } catch (final RedisInvalidSubscriptionException rise) { LOGGER.error("An error occurred while unsubscribing from topic {}", monitorTopic.getTopic(), rise); unsubscriptionDone = false; } return unsubscriptionDone; }
private boolean subscribeMonitor() { boolean subscriptionDone = true; try { LOGGER.debug("Adding subscription to topic {}", monitorTopic.getTopic()); listenerContainer.addMessageListener(monitorListener, monitorTopic); } catch (final RedisInvalidSubscriptionException rise) { LOGGER.error("An error occurred while subscribing to topic {}", monitorTopic.getTopic(), rise); subscriptionDone = false; } return subscriptionDone; }
public void publishPing() { redisTemplate.convertAndSend(monitorTopic.getTopic(), "PING SUBSCRIPTION"); lock.lock(); try { countPendingEvents++; } finally { lock.unlock(); } }
private void removeSubscription(final Subscription subscription) { final Topic topic = ChannelUtils.getChannel(subscription); LOGGER.debug("Removing subscription to channel {} for listener {} ", topic.getTopic(), subscription.getSourceEntityId()); final MessageListenerImpl listener = listeners.get(subscription.getSourceEntityId()); if (listener != null) { listenerContainer.removeMessageListener(listener, topic); listener.removeSubscription(topic); } // Finally, the subscription to the topic is removed from Redis jedisTemplate.hDel(keysBuilder.getSubscriptionKey(subscription.getSourceEntityId()), topic.getTopic()); LOGGER.debug("Removed subscription from listener {} to channel {}", subscription.getSourceEntityId(), topic.getTopic()); }
private void activateSubscription(final String listenerName, final Topic topic, final NotificationParams notificationParams) { MessageListenerImpl listener = listeners.get(listenerName); if (listener == null) { listener = addNewListener(listenerName); } LOGGER.info("Subscribing listener {} to channel {}", listener.getName(), topic.getTopic()); listenerContainer.addMessageListener(listener, topic); listener.addSubscription(topic, notificationParams); }
private void publish(final AlarmInputMessage message) { LOGGER.debug("Publish alarm event message [{}] associated with alert [{}]", message.getMessage(), message.getAlertId()); final Topic topic = ChannelUtils.buildTopic(PubSubChannelPrefix.alarm, message.getAlertId()); jedisTemplate.publish(topic.getTopic(), PublishMessageUtils.buildContentToPublish(message, topic)); }
private void publish(final OrderInputMessage message) { LOGGER.debug("Publish order event [{}] related to provider [{}] and sensor [{}]", message.getOrder(), message.getProviderId(), message.getSensorId()); final Topic topic = ChannelUtils.buildTopic(PubSubChannelPrefix.order, message.getProviderId(), message.getSensorId()); jedisTemplate.publish(topic.getTopic(), PublishMessageUtils.buildContentToPublish(message, topic)); LOGGER.debug("Order published"); } }
private void publishSensorData(final Observation data) { final Topic topic = ChannelUtils.buildTopic(PubSubChannelPrefix.data, data.getProvider(), data.getSensor()); jedisTemplate.publish(topic.getTopic(), PublishMessageUtils.buildContentToPublish(data, topic)); }
@Override public void subscribe(final Subscription subscription) { // The first step is to validate that the resource to which the subscription refers exists in // Sentilo. Otherwise an error is thrown checkTargetResourceState(subscription); // Al subscribirse, no sólo se debe habilitar el listener correspondiente, sino que tb se debe // persistir en Redis la subscripcion para la entidad de turno. De esta manera se podrán // iniciar los listeners asociados a las subscripciones ya existentes cuando se arranque // este modulo. // Estos registros en Redis serán del tipo Hash y habrá uno para cada entidad. // Es decir, para cada entidad que este subscrita a algun canal tendremos en Redis // una Hash con key igual a subs:<entityId> y N entradas <field, value> donde: // - field: cada campo de la hash corresponderá a una subscripcion , por lo que el nombre del // campo identificará el canal al cual se está subscrito <event_type>/element_id, donde // element_id es el identificador del recurso al cual se esta subscrito. // - value: el value del campo contiene la informacion necesaria para realizar la notificacion // via HTTP Callback (estos datos son el endpoint, la secretKey a utilizar, politica de // reintentos, ...) final Topic topic = ChannelUtils.getChannel(subscription); // Habilitamos listener activateSubscription(subscription.getSourceEntityId(), topic, subscription.getNotificationParams()); // Persistimos en Redis la subscripcion jedisTemplate.hSet(keysBuilder.getSubscriptionKey(subscription.getSourceEntityId()), topic.getTopic(), converter.marshal(subscription.getNotificationParams())); LOGGER.info("Listener {} subscribed to channel {}", subscription.getSourceEntityId(), topic.getTopic()); }
public static String buildContentToPublish(final OrderInputMessage message, final Topic topic) { final Long timestamp = System.currentTimeMillis(); final EventMessage event = new EventMessage(); event.setProvider(message.getProviderId()); event.setSensor(message.getSensorId()); event.setMessage(message.getOrder()); event.setTimestamp(DateUtils.timestampToString(timestamp)); event.setTime(timestamp); event.setType(EventType.ORDER.name()); event.setTopic(topic.getTopic()); setCustomsFields(event); return converter.marshal(event); }
private void publishGhostSensorAlarm(final Observation data) { final String ghostSensorKey = data.getProvider() + "." + data.getSensor(); if (ghostSensors.get(ghostSensorKey) == null) { final String ghost_message_template = "Detected ghost sensor %s belonging to provider %s"; final Topic topic = ChannelUtils.buildTopic(PubSubChannelPrefix.alarm, data.getProvider(), data.getSensor()); final AlarmInputMessage aim = new AlarmInputMessage(); aim.setProviderId(data.getProvider()); aim.setSensorId(data.getSensor()); aim.setAlertType("INTERNAL"); aim.setAlertId(SentiloConstants.GHOST_SENSOR_ALERT); aim.setSender(SentiloConstants.GHOST_SENSOR_SENDER); aim.setMessage(String.format(ghost_message_template, data.getSensor(), data.getProvider())); jedisTemplate.publish(topic.getTopic(), PublishMessageUtils.buildContentToPublish(aim, topic)); ghostSensors.put(ghostSensorKey, ghost_message_template); LOGGER.info("Published new ghost sensor alarm related to sensor [{}] from provider [{}]", data.getSensor(), data.getProvider()); } }
public static String buildContentToPublish(final AlarmInputMessage message, final Topic topic) { final Long timestamp = System.currentTimeMillis(); final EventMessage event = new EventMessage(); event.setAlert(message.getAlertId()); event.setAlertType(message.getAlertType()); event.setProvider(message.getProviderId()); event.setSensor(message.getSensorId()); event.setMessage(message.getMessage()); event.setTimestamp(DateUtils.timestampToString(timestamp)); event.setTime(timestamp); event.setType(EventType.ALARM.name()); event.setTopic(topic.getTopic()); event.setPublisher(message.getSender()); setCustomsFields(event); return converter.marshal(event); }
public static String buildContentToPublish(final Observation message, final Topic topic) { final EventMessage event = new EventMessage(); event.setProvider(message.getProvider()); event.setSensor(message.getSensor()); event.setMessage(message.getValue()); event.setTimestamp(DateUtils.timestampToString(message.getTimestamp())); event.setTime(message.getTimestamp()); event.setLocation(message.getLocation()); event.setType(EventType.DATA.name()); event.setTopic(topic.getTopic()); setCustomsFields(event); return converter.marshal(event); }