/** * Creates a route if a message is needed by a profile. * @param message The message that needs routed. * @param profile The profile that may need the message. * @return A MessageRoute if the message is needed by the profile. */ private Optional<MessageRoute> routeToProfile(JSONObject message, ProfileConfig profile, Clock clock) { Optional<MessageRoute> route = Optional.empty(); // allow the profile to access the fields defined within the message @SuppressWarnings("unchecked") final Map<String, Object> state = (Map<String, Object>) message; try { // is this message needed by this profile? if (executor.execute(profile.getOnlyif(), state, Boolean.class)) { // what time is is? could be either system or event time Optional<Long> timestamp = clock.currentTimeMillis(message); if(timestamp.isPresent()) { // what is the name of the entity in this message? String entity = executor.execute(profile.getForeach(), state, String.class); route = Optional.of(new MessageRoute(profile, entity, message, timestamp.get())); } } } catch(Throwable e) { // log an error and move on. ignore bad profiles. String msg = format("error while executing profile; profile='%s', error='%s'", profile.getProfile(), e.getMessage()); LOG.error(msg, e); } return route; }
/** * Handles the processing of a single tuple. * * @param input The tuple containing a telemetry message. */ private void handleMessage(Tuple input) { // crack open the tuple JSONObject message = getField(MESSAGE_TUPLE_FIELD, input, JSONObject.class); ProfileConfig definition = getField(PROFILE_TUPLE_FIELD, input, ProfileConfig.class); String entity = getField(ENTITY_TUPLE_FIELD, input, String.class); Long timestamp = getField(TIMESTAMP_TUPLE_FIELD, input, Long.class); // keep track of time activeFlushSignal.update(timestamp); // distribute the message MessageRoute route = new MessageRoute(definition, entity, message, timestamp); synchronized (messageDistributor) { messageDistributor.distribute(route, getStellarContext()); } LOG.debug("Message distributed: profile={}, entity={}, timestamp={}", definition.getProfile(), entity, timestamp); }