/** * Instantiate a message * @param type {@link MessageType} as a string or a custom message type * @param msgId unique message identifier */ public Message(String type, String msgId) { super(new ZNRecord(msgId)); _record.setSimpleField(Attributes.MSG_TYPE.toString(), type); setMsgId(msgId); setMsgState(MessageState.NEW); _record.setLongField(Attributes.CREATE_TIMESTAMP.toString(), new Date().getTime()); }
/** * Attach a relayed message and its destination participant to this message. * * WARNNING: only content in SimpleFields of relayed message will be carried over and sent, * all contents in either ListFields or MapFields will be ignored. * * @param instance destination participant name * @param message relayed message. */ public void attachRelayMessage(String instance, Message message) { List<String> relayList = _record.getListField(Attributes.RELAY_PARTICIPANTS.name()); if (relayList == null) { relayList = Collections.EMPTY_LIST; } Set<String> relayParticipants = new LinkedHashSet<>(relayList); relayParticipants.add(instance); Map<String, String> messageInfo = message.getRecord().getSimpleFields(); messageInfo.put(Attributes.RELAY_MSG_ID.name(), message.getId()); messageInfo.put(Attributes.MSG_SUBTYPE.name(), MessageType.RELAYED_MESSAGE.name()); messageInfo.put(Attributes.RELAY_FROM.name(), getTgtName()); messageInfo .put(Attributes.EXPIRY_PERIOD.name(), String.valueOf(RELAY_MESSAGE_DEFAULT_EXPIRY)); _record.setMapField(instance, messageInfo); _record.setListField(Attributes.RELAY_PARTICIPANTS.name(), Lists.newArrayList(relayParticipants)); }
private static String instantiateByMessage(String string, Message message) { Matcher matcher = pattern.matcher(string); String result = string; while (matcher.find()) { String var = matcher.group(); result = result.replace(var, message.getAttribute(Message.Attributes.valueOf(var.substring(1, var.length() - 1)))); } return result; }
/** * Attach a relayed message and its destination participant to this message. * * WARNNING: only content in SimpleFields of relayed message will be carried over and sent, * all contents in either ListFields or MapFields will be ignored. * * @param instance destination participant name * @param message relayed message. */ public void attachRelayMessage(String instance, Message message) { List<String> relayList = _record.getListField(Attributes.RELAY_PARTICIPANTS.name()); if (relayList == null) { relayList = Collections.EMPTY_LIST; } Set<String> relayParticipants = new LinkedHashSet<>(relayList); relayParticipants.add(instance); Map<String, String> messageInfo = message.getRecord().getSimpleFields(); messageInfo.put(Attributes.RELAY_MSG_ID.name(), message.getId()); messageInfo.put(Attributes.MSG_SUBTYPE.name(), MessageType.RELAYED_MESSAGE.name()); messageInfo.put(Attributes.RELAY_FROM.name(), getTgtName()); messageInfo .put(Attributes.EXPIRY_PERIOD.name(), String.valueOf(RELAY_MESSAGE_DEFAULT_EXPIRY)); _record.setMapField(instance, messageInfo); _record.setListField(Attributes.RELAY_PARTICIPANTS.name(), Lists.newArrayList(relayParticipants)); }
/** * Get relay message attached for the given instance. * * @param instance * @return null if no message for the instance */ public Message getRelayMessage(String instance) { Map<String, String> messageInfo = _record.getMapField(instance); if (messageInfo != null) { String id = messageInfo.get(Attributes.RELAY_MSG_ID.name()); if (id == null) { id = messageInfo.get(Attributes.MSG_ID.name()); if (id == null) { return null; } } ZNRecord record = new ZNRecord(id); record.setSimpleFields(messageInfo); return new Message(record); } return null; }
/** * Get relay message attached for the given instance. * * @param instance * @return null if no message for the instance */ public Message getRelayMessage(String instance) { Map<String, String> messageInfo = _record.getMapField(instance); if (messageInfo != null) { String id = messageInfo.get(Attributes.RELAY_MSG_ID.name()); if (id == null) { id = messageInfo.get(Attributes.MSG_ID.name()); if (id == null) { return null; } } ZNRecord record = new ZNRecord(id); record.setSimpleFields(messageInfo); return new Message(record); } return null; }
/** * Get all relay messages attached to this message as a map (instance->message). * * @return map of instanceName->message, empty map if none. */ public Map<String, Message> getRelayMessages() { Map<String, Message> relayMessageMap = new HashMap<>(); List<String> relayParticipants = _record.getListField(Attributes.RELAY_PARTICIPANTS.name()); if (relayParticipants != null) { for (String p : relayParticipants) { Message msg = getRelayMessage(p); if (p != null) { relayMessageMap.put(p, msg); } } } return relayMessageMap; }
/** * Get all relay messages attached to this message as a map (instance->message). * * @return map of instanceName->message, empty map if none. */ public Map<String, Message> getRelayMessages() { Map<String, Message> relayMessageMap = new HashMap<>(); List<String> relayParticipants = _record.getListField(Attributes.RELAY_PARTICIPANTS.name()); if (relayParticipants != null) { for (String p : relayParticipants) { Message msg = getRelayMessage(p); if (p != null) { relayMessageMap.put(p, msg); } } } return relayMessageMap; }
public String getRelaySrcHost() { return _record.getSimpleField(Attributes.RELAY_FROM.name()); }
public String getRelaySrcHost() { return _record.getSimpleField(Attributes.RELAY_FROM.name()); }
/** * Whether there are any relay message attached to this message. * * @return */ public boolean hasRelayMessages() { List<String> relayHosts = _record.getListField(Attributes.RELAY_PARTICIPANTS.name()); return (relayHosts != null && relayHosts.size() > 0); }
/** * Set the completion time of previous task associated with this message. * This applies only when this is a relay message, * which specified the completion time of the task running on the participant that sent this relay message. * * @param completionTime */ public void setRelayTime(long completionTime) { _record.setLongField(Attributes.RELAY_TIME.name(), completionTime); }
/** * Get the completion time of previous task associated with this message. * This applies only when this is a relay message, * which specified the completion time of the task running on the participant that sent this relay message. * * @return */ public long getRelayTime() { return _record.getLongField(Attributes.RELAY_TIME.name(), -1); }
/** * Get the "from-state" for transition-related messages * @return state name, or null for other message types */ public String getFromState() { return _record.getSimpleField(Attributes.FROM_STATE.toString()); }
/** * Get the session identifier of the destination node * @return session identifier */ public String getTgtSessionId() { return _record.getSimpleField(Attributes.TGT_SESSION_ID.toString()); }
/** * Get the expiry period (in milliseconds) * * @return */ public long getExpiryPeriod() { return _record.getLongField(Attributes.EXPIRY_PERIOD.name(), -1); }
/** * Add or change a message attribute * @param attr {@link Attributes} attribute name * @param val attribute value */ public void setAttribute(Attributes attr, String val) { _record.setSimpleField(attr.toString(), val); }
/** * Get the state model factory associated with this message * @return the name of the factory */ public String getStateModelFactoryName() { return _record.getSimpleField(Attributes.STATE_MODEL_FACTORY_NAME.toString()); }
/** * Set the results of message execution * @param resultMap map of result property and value pairs */ public void setResultMap(Map<String, String> resultMap) { _record.setMapField(Attributes.MESSAGE_RESULT.toString(), resultMap); }
/** * Get the number of times to retry message handling on timeouts * @return maximum number of retries */ public int getRetryCount() { return _record.getIntField(Attributes.RETRY_COUNT.toString(), 0); }