/** * Constructor to reconstruct a Message using existing data. Note that no correlation data * from a UnitOfWork is attached when using this constructor. If you're constructing a new * Message, use {@link #GenericMessage(Object, Map)} instead * * @param identifier The identifier of the Message * @param declaredPayloadType The declared type of message payload * @param payload The payload for the message * @param metaData The meta data for the message */ public GenericMessage(String identifier, Class<T> declaredPayloadType, T payload, Map<String, ?> metaData) { super(identifier); this.metaData = MetaData.from(metaData); this.payload = payload; this.payloadType = declaredPayloadType; }
/** * Creates a MetaData instances with a single entry, with the given {@code key} and * given {@code value}. * * @param key The key for the entry * @param value The value of the entry * @return a MetaData instance with a single entry */ public static MetaData with(String key, Object value) { return MetaData.from(Collections.singletonMap(key, value)); }
@Override public GrpcBackedResponseMessage<R> withMetaData(Map<String, ?> metaData) { return new GrpcBackedResponseMessage<>(queryResponse, messageSerializer, serializedPayload, exception, () -> MetaData.from(metaData)); }
/** * Returns a MetaData instance containing a subset of the {@code keys} in this instance. * Keys for which there is no assigned value are ignored.< * * @param keys The keys of the entries to remove * @return a MetaData instance containing the given {@code keys} if these were already present */ public MetaData subset(String... keys) { return MetaData.from(Stream.of(keys).filter(this::containsKey).collect(new MetaDataCollector(this::get))); }
@Override public QueryMessage<Q, R> withMetaData(Map<String, ?> metaData) { return new GrpcBackedQueryMessage<>(query, messageSerializer, serializedPayload, serializedResponseType, () -> MetaData.from(metaData)); }
@Override public MetaData get() { if (metaData.isEmpty()){ if (map.isEmpty()) { metaData.add(emptyInstance()); } else { metaData.add(from(map.entrySet().stream().collect(toMap(Map.Entry::getKey, e -> convert(e.getValue()))))); } } return metaData.get(0); }
/** * Convert the given map of MetaDataValues to a Map containing the Java representations of each MetaDataValue. * <p> * See {@link #convertToMetaDataValue(Object)} for details about the mapping. * * @param metaDataMap a Map containing MetaDataValue representations of each MetaData key * @return a map containing the same keys, referencing to the Java representation of each corresponding value in * the given {@code metaDataMap} */ public MetaData convert(Map<String, MetaDataValue> metaDataMap) { if (metaDataMap.isEmpty()) { return MetaData.emptyInstance(); } Map<String, Object> metaData = new HashMap<>(metaDataMap.size()); metaDataMap.forEach((k, v) -> metaData.put(k, convertFromMetaDataValue(v))); return MetaData.from(metaData); }
/** * Returns a MetaData instance containing values of {@code this}, combined with the given * {@code additionalEntries}. If any entries have identical keys, the values from the * {@code additionalEntries} will take precedence. * * @param additionalEntries The additional entries for the new MetaData * @return a MetaData instance containing values of {@code this}, combined with the given * {@code additionalEntries} */ public MetaData mergedWith(Map<String, ?> additionalEntries) { if (additionalEntries.isEmpty()) { return this; } if (isEmpty()) { return MetaData.from(additionalEntries); } Map<String, Object> merged = new HashMap<>(values); merged.putAll(additionalEntries); return new MetaData(merged); }
@Override public MetaData getCorrelationData() { if (correlationDataProviders.isEmpty()) { return MetaData.emptyInstance(); } Map<String, Object> result = new HashMap<>(); for (CorrelationDataProvider correlationDataProvider : correlationDataProviders) { final Map<String, ?> extraData = correlationDataProvider.correlationDataFor(getMessage()); if (extraData != null) { result.putAll(extraData); } } return MetaData.from(result); }
private MetaData deserializeMetaData(Map<String, MetaDataValue> metaDataMap) { if (metaDataMap.isEmpty()) { return MetaData.emptyInstance(); } GrpcMetaDataConverter grpcMetaDataConverter = new GrpcMetaDataConverter(serializer); return MetaData.from(grpcMetaDataConverter.convert(metaDataMap)); } }
@Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { if (!reader.hasMoreChildren()) { return MetaData.emptyInstance(); } Map<String, Object> contents = new HashMap<>(); populateMap(reader, context, contents); if (contents.isEmpty()) { return MetaData.emptyInstance(); } else { return MetaData.from(contents); } } }
@Override public Message<T> withMetaData(Map<String, ?> metaData) { if (getMetaData().equals(metaData)) { return this; } return withMetaData(MetaData.from(metaData)); }
@SuppressWarnings("unchecked") @Override public MetaData deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonDeserializer<Object> deserializer = ctxt.findRootValueDeserializer( ctxt.getTypeFactory().constructMapType(Map.class, String.class, Object.class)); return MetaData.from((Map) deserializer.deserialize(jp, ctxt, new HashMap<>())); } }
private Object readResolve() { return new org.axonframework.eventhandling.GenericEventMessage<>(getIdentifier(), getPayload(), MetaData.from(getMetaData().getValues()), Instant.ofEpochMilli(timestamp.getMillis())); } }
/** * Constructs a Message for the given {@code payload} and {@code meta data}. The given {@code metaData} is * merged with the MetaData from the correlation data of the current unit of work, if present. * * @param declaredPayloadType The declared type of message payload * @param payload The payload for the message * @param metaData The meta data for the message */ public GenericMessage(Class<T> declaredPayloadType, T payload, Map<String, ?> metaData) { this(IdentifierFactory.getInstance().generateIdentifier(), declaredPayloadType, payload, CurrentUnitOfWork.correlationData().mergedWith(MetaData.from(metaData))); }
/** * Creates a MetaData instances with a single entry, with the given {@code key} and * given {@code value}. * * @param key The key for the entry * @param value The value of the entry * @return a MetaData instance with a single entry */ public static MetaData with(String key, Object value) { return MetaData.from(Collections.singletonMap(key, value)); }
/** * Returns a MetaData instance containing a subset of the {@code keys} in this instance. * Keys for which there is no assigned value are ignored.< * * @param keys The keys of the entries to remove * @return a MetaData instance containing the given {@code keys} if these were already present */ public MetaData subset(String... keys) { return MetaData.from(Stream.of(keys).filter(this::containsKey).collect(new MetaDataCollector(this::get))); }
/** * Returns a MetaData instance containing a subset of the {@code keys} in this instance. * Keys for which there is no assigned value are ignored.< * * @param keys The keys of the entries to remove * @return a MetaData instance containing the given {@code keys} if these were already present */ public MetaData subset(String... keys) { return MetaData.from(Stream.of(keys).filter(this::containsKey).collect(new MetaDataCollector(this::get))); }
@Override public Message<T> withMetaData(Map<String, ?> metaData) { if (getMetaData().equals(metaData)) { return this; } return withMetaData(MetaData.from(metaData)); }
/** * Constructs a Message for the given {@code payload} and {@code meta data}. The given {@code metaData} is * merged with the MetaData from the correlation data of the current unit of work, if present. * * @param declaredPayloadType The declared type of message payload * @param payload The payload for the message * @param metaData The meta data for the message */ public GenericMessage(Class<T> declaredPayloadType, T payload, Map<String, ?> metaData) { this(IdentifierFactory.getInstance().generateIdentifier(), declaredPayloadType, payload, CurrentUnitOfWork.correlationData().mergedWith(MetaData.from(metaData))); }