private static boolean isMessageReplicated(Message message) { MessageAnnotations annotations = message.getMessageAnnotations(); return annotations != null && annotations.getValue().containsKey(replicated); } }
/** * Safe way to access message annotations which will check internal structure and * either return the annotation if it exists or null if the annotation or any annotations * are present. * * @param key * the String key to use to lookup an annotation. * @param message * the AMQP message object that is being examined. * * @return the given annotation value or null if not present in the message. */ public static Object getMessageAnnotation(String key, Message message) { if (message != null && message.getMessageAnnotations() != null) { Map<Symbol, Object> annotations = message.getMessageAnnotations().getValue(); return annotations.get(AmqpMessageSupport.getSymbol(key)); } return null; }
/** * Safe way to access message annotations which will check internal structure and * either return the annotation if it exists or null if the annotation or any annotations * are present. * * @param key * the String key to use to lookup an annotation. * @param message * the AMQP message object that is being examined. * * @return the given annotation value or null if not present in the message. */ public static Object getMessageAnnotation(String key, Message message) { if (message != null && message.getMessageAnnotations() != null) { Map<Symbol, Object> annotations = message.getMessageAnnotations().getValue(); return annotations.get(AmqpMessageSupport.getSymbol(key)); } return null; }
/** * Safe way to access message annotations which will check internal structure and either * return the annotation if it exists or null if the annotation or any annotations are * present. * * @param key * the String key to use to lookup an annotation. * @param message * the AMQP message object that is being examined. * * @return the given annotation value or null if not present in the message. */ public static Object getMessageAnnotation(String key, Message message) { if (message != null && message.getMessageAnnotations() != null) { Map<Symbol, Object> annotations = message.getMessageAnnotations().getValue(); return annotations.get(AMQPMessageSupport.getSymbol(key)); } return null; }
/** * Safe way to access message annotations which will check internal structure and either * return the annotation if it exists or null if the annotation or any annotations are * present. * * @param key * the String key to use to lookup an annotation. * @param message * the AMQP message object that is being examined. * * @return the given annotation value or null if not present in the message. */ public static Object getMessageAnnotation(String key, Message message) { if (message != null && message.getMessageAnnotations() != null) { Map<Symbol, Object> annotations = message.getMessageAnnotations().getValue(); return annotations.get(AMQPMessageSupport.getSymbol(key)); } return null; }
Message toAmqpMessage(final String partitionKey) { final Message amqpMessage = this.toAmqpMessage(); final MessageAnnotations messageAnnotations = (amqpMessage.getMessageAnnotations() == null) ? new MessageAnnotations(new HashMap<>()) : amqpMessage.getMessageAnnotations(); messageAnnotations.getValue().put(AmqpConstants.PARTITION_KEY, partitionKey); amqpMessage.setMessageAnnotations(messageAnnotations); return amqpMessage; }
/** * Returns the value to which the specified key is mapped in the message annotations, or {@code null} if the message * annotations contain no mapping for the key. * * @param <T> the expected type of the property to read. * @param msg the message that contains the annotations. * @param key the name of the symbol to return a value for. * @param type the expected type of the value. * @return the annotation's value or {@code null} if no such annotation exists or its value is not of the expected * type. */ @SuppressWarnings("unchecked") public static <T> T getAnnotation(final Message msg, final String key, final Class<T> type) { final MessageAnnotations annotations = msg.getMessageAnnotations(); if (annotations == null) { return null; } else { final Object value = annotations.getValue().get(Symbol.getSymbol(key)); if (type.isInstance(value)) { return (T) value; } else { return null; } } }
/** * Adds a value for a symbol to an AMQP 1.0 message's <em>annotations</em>. * * @param msg the message to add the symbol to. * @param key the name of the symbol to add a value for. * @param value the value to add. */ public static void addAnnotation(final Message msg, final String key, final Object value) { MessageAnnotations annotations = msg.getMessageAnnotations(); if (annotations == null) { annotations = new MessageAnnotations(new HashMap<>()); msg.setMessageAnnotations(annotations); } annotations.getValue().put(Symbol.getSymbol(key), value); }
/** * Returns the value to which the specified key is mapped in the message annotations, or {@code null} if the message * annotations contain no mapping for the key. * * @param <T> the expected type of the property to read. * @param msg the message that contains the annotations. * @param key the name of the symbol to return a value for. * @param type the expected type of the value. * @return the annotation's value or {@code null} if no such annotation exists or its value is not of the expected * type. */ @SuppressWarnings("unchecked") public static <T> T getAnnotation(final Message msg, final String key, final Class<T> type) { final MessageAnnotations annotations = msg.getMessageAnnotations(); if (annotations == null) { return null; } else { final Object value = annotations.getValue().get(Symbol.getSymbol(key)); if (type.isInstance(value)) { return (T) value; } else { return null; } } }
@Override public Map<Symbol, UnknownDescribedType> getFilter(final Message lastReceivedMessage) { String expression; if (lastReceivedMessage != null) { String lastReceivedOffset = lastReceivedMessage.getMessageAnnotations().getValue().get(AmqpConstants.OFFSET).toString(); expression = String.format(AmqpConstants.AMQP_ANNOTATION_FORMAT, AmqpConstants.OFFSET_ANNOTATION_NAME, StringUtil.EMPTY, lastReceivedOffset); } else { expression = this.eventPosition.getExpression(); } if (TRACE_LOGGER.isInfoEnabled()) { String logReceivePath = ""; if (this.internalReceiver == null) { // During startup, internalReceiver is still null. Need to handle this special case when logging during startup // or the reactor thread crashes with NPE when calling internalReceiver.getReceivePath() and no receiving occurs. logReceivePath = "receiverPath[RECEIVER IS NULL]"; } else { logReceivePath = "receiverPath[" + this.internalReceiver.getReceivePath() + "]"; } TRACE_LOGGER.info(String.format("%s, action[createReceiveLink], %s", logReceivePath, this.eventPosition)); } return Collections.singletonMap(AmqpConstants.STRING_FILTER, new UnknownDescribedType(AmqpConstants.STRING_FILTER, expression)); }
/** * Adds a value for a symbol to an AMQP 1.0 message's <em>annotations</em>. * * @param msg the message to add the symbol to. * @param key the name of the symbol to add a value for. * @param value the value to add. */ public static void addAnnotation(final Message msg, final String key, final Object value) { MessageAnnotations annotations = msg.getMessageAnnotations(); if (annotations == null) { annotations = new MessageAnnotations(new HashMap<>()); msg.setMessageAnnotations(annotations); } annotations.getValue().put(Symbol.getSymbol(key), value); }
MessageAnnotations messageAnnotations = message.getMessageAnnotations();
/** * Creates a new AmqpMessage that wraps the information necessary to handle * an incoming delivery. * * @param receiver the AmqpReceiver that received this message. * @param message the Proton message that was received. * @param delivery the Delivery instance that produced this message. */ @SuppressWarnings("unchecked") public AmqpMessage(AmqpReceiver receiver, Message message, Delivery delivery) { this.receiver = receiver; this.message = message; this.delivery = delivery; if (message.getMessageAnnotations() != null) { messageAnnotationsMap = message.getMessageAnnotations().getValue(); } if (message.getApplicationProperties() != null) { applicationPropertiesMap = message.getApplicationProperties().getValue(); } if (message.getDeliveryAnnotations() != null) { deliveryAnnotationsMap = message.getDeliveryAnnotations().getValue(); } }
private void forwardMessage(ProtonSender protonSender, ProtonReceiver protonReceiver, ProtonDelivery sourceDelivery, Message message) { MessageAnnotations annotations = message.getMessageAnnotations(); if (annotations == null) { annotations = new MessageAnnotations(Collections.singletonMap(replicated, true)); } else { annotations.getValue().put(replicated, true); } message.setMessageAnnotations(annotations); protonSender.send(message, protonDelivery -> { sourceDelivery.disposition(protonDelivery.getRemoteState(), protonDelivery.remotelySettled()); protonReceiver.flow(protonSender.getCredit() - protonReceiver.getCredit()); }); }
public static int getDataSerializedSize(Message amqpMessage) { if (amqpMessage == null) { return 0; } int payloadSize = getPayloadSize(amqpMessage); // EventData - accepts only PartitionKey - which is a String & stuffed into MessageAnnotation final MessageAnnotations messageAnnotations = amqpMessage.getMessageAnnotations(); final ApplicationProperties applicationProperties = amqpMessage.getApplicationProperties(); int annotationsSize = 0; int applicationPropertiesSize = 0; if (messageAnnotations != null) { for (Symbol value : messageAnnotations.getValue().keySet()) { annotationsSize += sizeof(value); } for (Object value : messageAnnotations.getValue().values()) { annotationsSize += sizeof(value); } } if (applicationProperties != null) { for (Object value : applicationProperties.getValue().keySet()) { applicationPropertiesSize += sizeof(value); } for (Object value : applicationProperties.getValue().values()) { applicationPropertiesSize += sizeof(value); } } return annotationsSize + applicationPropertiesSize + payloadSize; }
batchMessage.setMessageAnnotations(firstMessage.getMessageAnnotations());
@Test public void testJSON_to_AMQP_WithNoMessageAnnotations() { JsonObject jsonObject = new JsonObject(); Message protonMsg = translator.convertToAmqpMessage(jsonObject); assertNotNull("Expected converted msg", protonMsg); assertNull("expected converted msg to have no message annotations section", protonMsg.getMessageAnnotations()); }
public static int getDataSerializedSize(Message amqpMessage) { if (amqpMessage == null) { return 0; } int payloadSize = getPayloadSize(amqpMessage); // EventData - accepts only PartitionKey - which is a String & stuffed into MessageAnnotation MessageAnnotations messageAnnotations = amqpMessage.getMessageAnnotations(); ApplicationProperties applicationProperties = amqpMessage.getApplicationProperties(); int annotationsSize = 0; int applicationPropertiesSize = 0; if (messageAnnotations != null) { annotationsSize += Util.sizeof(messageAnnotations.getValue()); } if (applicationProperties != null) { applicationPropertiesSize += Util.sizeof(applicationProperties.getValue()); } return annotationsSize + applicationPropertiesSize + payloadSize; }
assertNotNull("Expected converted msg", protonMsg); MessageAnnotations ma = protonMsg.getMessageAnnotations(); assertNotNull("message annotations section not present", ma);
@Test public void testJSON_to_AMQP_VerifyMessageAnnotations() { String testAnnKeyNameA = "testAnnKeyA"; String testAnnKeyNameB = "testAnnKeyB"; Symbol testAnnKeyA = Symbol.valueOf(testAnnKeyNameA); String testAnnValueA = "testAnnValueA"; Symbol testAnnKeyB = Symbol.valueOf(testAnnKeyNameB); String testAnnValueB = "testAnnValueB"; JsonObject jsonAppProps = new JsonObject(); jsonAppProps.put(testAnnKeyNameA, testAnnValueA); jsonAppProps.put(testAnnKeyNameB, testAnnValueB); JsonObject jsonObject = new JsonObject(); jsonObject.put(AmqpConstants.MESSAGE_ANNOTATIONS, jsonAppProps); Message protonMsg = translator.convertToAmqpMessage(jsonObject); assertNotNull("Expected converted msg", protonMsg); MessageAnnotations ma = protonMsg.getMessageAnnotations(); assertNotNull("message annotations section not present", ma); Map<Symbol, Object> annotations = ma.getValue(); assertNotNull("message annotations map not present", ma); assertTrue("expected key to be present", annotations.containsKey(testAnnKeyA)); assertEquals("expected value to be equal", testAnnValueA, annotations.get(testAnnKeyA)); assertTrue("expected key to be present", annotations.containsKey(testAnnKeyB)); assertEquals("expected value to be equal", testAnnValueB, annotations.get(testAnnKeyB)); assertEquals("unexpected number of props", 2, annotations.size()); }