@Override public ShardedMessageEnvelope setDittoHeaders(final DittoHeaders dittoHeaders) { return of(id, type, message, dittoHeaders); }
/** * Returns a new {@code ShardedMessageEnvelope} for the specified {@code id} and {@code message}. * * @param id the identifier. * @param type the type of the message. * @param message the message. * @param dittoHeaders the command headers. * @return the ShardedMessageEnvelope. */ public static ShardedMessageEnvelope of(final String id, final String type, final JsonObject message, final DittoHeaders dittoHeaders) { return new ShardedMessageEnvelope(id, type, message, dittoHeaders); }
private Jsonifiable createJsonifiableFrom(final ShardedMessageEnvelope messageEnvelope) { final String type = messageEnvelope.getType(); final BiFunction<JsonObject, DittoHeaders, Jsonifiable> mappingFunction = mappingStrategies.get(type); if (null == mappingFunction) { final String pattern = "No strategy found to map type {0} to a Jsonifiable!"; throw new IllegalStateException(MessageFormat.format(pattern, type)); } final JsonObject payload = messageEnvelope.getMessage(); final DittoHeaders dittoHeaders = messageEnvelope.getDittoHeaders(); return mappingStrategies.get(type).apply(payload, dittoHeaders); }
@Override public Object entityMessage(final Object message) { final Object entity; if (message instanceof JsonObject) { // message was sent from another cluster node and therefor is serialized as json final ShardedMessageEnvelope shardedMessageEnvelope = ShardedMessageEnvelope.fromJson((JsonObject) message); entity = createJsonifiableFrom(shardedMessageEnvelope); } else if (message instanceof ShardedMessageEnvelope) { // message was sent from the same cluster node entity = createJsonifiableFrom((ShardedMessageEnvelope) message); } else { entity = message; } return entity; }
@Override public String entityId(final Object message) { if (message instanceof ShardedMessageEnvelope) { return ((ShardedMessageEnvelope) message).getId(); } else if (message instanceof WithId) { return ((WithId) message).getId(); } else if (message instanceof ShardRegion.StartEntity) { return ((ShardRegion.StartEntity) message).entityId(); } return null; }
@Override public Object entityMessage(final Object message) { final Object entity; if (message instanceof JsonObject) { // message was sent from another cluster node and therefor is serialized as json final ShardedMessageEnvelope shardedMessageEnvelope = ShardedMessageEnvelope.fromJson((JsonObject) message); entity = createJsonifiableFrom(shardedMessageEnvelope); } else if (message instanceof ShardedMessageEnvelope) { // message was sent from the same cluster node entity = createJsonifiableFrom((ShardedMessageEnvelope) message); } else { entity = message; } return entity; }
@Override public String entityId(final Object message) { if (message instanceof ShardedMessageEnvelope) { return ((ShardedMessageEnvelope) message).getId(); } else if (message instanceof WithId) { return ((WithId) message).getId(); } else if (message instanceof ShardRegion.StartEntity) { return ((ShardRegion.StartEntity) message).entityId(); } return null; }
@Override public ShardedMessageEnvelope setDittoHeaders(final DittoHeaders dittoHeaders) { return of(id, type, message, dittoHeaders); }
private Jsonifiable createJsonifiableFrom(final ShardedMessageEnvelope messageEnvelope) { final String type = messageEnvelope.getType(); final BiFunction<JsonObject, DittoHeaders, Jsonifiable> mappingFunction = mappingStrategies.get(type); if (null == mappingFunction) { final String pattern = "No strategy found to map type {0} to a Jsonifiable!"; throw new IllegalStateException(MessageFormat.format(pattern, type)); } final JsonObject payload = messageEnvelope.getMessage(); final DittoHeaders dittoHeaders = messageEnvelope.getDittoHeaders(); return mappingStrategies.get(type).apply(payload, dittoHeaders); }
/** * Returns a new instance of {@code MappingStrategiesBuilder}. * * @return the instance. */ public static MappingStrategiesBuilder newInstance() { final MappingStrategiesBuilder builder = new MappingStrategiesBuilder(); // add the commonly known types: builder.add(DittoHeaders.class, jsonObject -> DittoHeaders.newBuilder(jsonObject).build()); builder.add(ShardedMessageEnvelope.class, jsonObject -> ShardedMessageEnvelope.fromJson(jsonObject)); // do not replace with lambda! builder.add(SimpleCommand.class, jsonObject -> SimpleCommand.fromJson(jsonObject)); // do not replace with lambda! builder.add(SimpleCommandResponse.class, jsonObject -> SimpleCommandResponse.fromJson(jsonObject)); // do not replace with lambda! builder.add(StatusInfo.class, jsonObject -> StatusInfo.fromJson(jsonObject)); // do not replace with lambda! builder.add(StreamAck.class, StreamAck::fromJson); return builder; }
/** * Returns a new {@code ShardedMessageEnvelope} for the specified {@code id} and {@code message}. * * @param id the identifier. * @param type the type of the message. * @param message the message. * @param dittoHeaders the command headers. * @return the ShardedMessageEnvelope. */ public static ShardedMessageEnvelope of(final String id, final String type, final JsonObject message, final DittoHeaders dittoHeaders) { return new ShardedMessageEnvelope(id, type, message, dittoHeaders); }
private <M> void forwardToShardRegion(final M message, final Function<M, String> getId, final Function<M, String> getType, final Function<M, JsonObject> toJson, final Function<M, DittoHeaders> getDittoHeaders) { final String id = getId.apply(message); log.debug("Forwarding incoming {} to shard region of {}", message.getClass().getSimpleName(), id); final String type = getType.apply(message); final JsonObject jsonObject = toJson.apply(message); final DittoHeaders dittoHeaders = getDittoHeaders.apply(message); final ShardedMessageEnvelope messageEnvelope = ShardedMessageEnvelope.of(id, type, jsonObject, dittoHeaders); shardRegion.forward(messageEnvelope, context()); }
/** * Returns a new instance of {@code MappingStrategiesBuilder}. * * @return the instance. */ public static MappingStrategiesBuilder newInstance() { final MappingStrategiesBuilder builder = new MappingStrategiesBuilder(); // add the commonly known types: builder.add(DittoHeaders.class, jsonObject -> DittoHeaders.newBuilder(jsonObject).build()); builder.add(ShardedMessageEnvelope.class, jsonObject -> ShardedMessageEnvelope.fromJson(jsonObject)); // do not replace with lambda! builder.add(SimpleCommand.class, jsonObject -> SimpleCommand.fromJson(jsonObject)); // do not replace with lambda! builder.add(SimpleCommandResponse.class, jsonObject -> SimpleCommandResponse.fromJson(jsonObject)); // do not replace with lambda! builder.add(StatusInfo.class, jsonObject -> StatusInfo.fromJson(jsonObject)); // do not replace with lambda! builder.add(StreamAck.class, StreamAck::fromJson); return builder; }
private <M> void forwardToShardRegion(final M message, final Function<M, String> getId, final Function<M, String> getType, final Function<M, JsonObject> toJson, final Function<M, DittoHeaders> getDittoHeaders) { final String id = getId.apply(message); log.debug("Forwarding incoming {} to shard region of {}", message.getClass().getSimpleName(), id); final String type = getType.apply(message); final JsonObject jsonObject = toJson.apply(message); final DittoHeaders dittoHeaders = getDittoHeaders.apply(message); final ShardedMessageEnvelope messageEnvelope = ShardedMessageEnvelope.of(id, type, jsonObject, dittoHeaders); final ActorRef sender = getSender(); final ActorRef deadLetters = getContext().getSystem().deadLetters(); namespaceBlockingBehavior .block(messageEnvelope) .thenAccept(m -> shardRegion.tell(m, sender)) .exceptionally(throwable -> { if (!Objects.equals(sender, deadLetters)) { // Only acknowledge IdentifiableStreamingMessage. No other messages should be acknowledged. if (message instanceof IdentifiableStreamingMessage) { final StreamAck streamAck = StreamAck.success(((IdentifiableStreamingMessage) message).asIdentifierString()); sender.tell(streamAck, getSelf()); } } return null; }); }
private static ShardedMessageEnvelope createEnvelope(final EntityId entityId, final Signal<?> signal) { return ShardedMessageEnvelope.of( entityId.toString(), signal.getType(), signal.toJson(signal.getImplementedSchemaVersion(), FieldType.regularOrSpecial()), signal.getDittoHeaders()); } }
/** * Returns a new {@code ShardedMessageEnvelope} parsed from the specified {@code jsonObject}. * * @param jsonObject the JSON object. * @return the ShardedMessageEnvelope. */ public static ShardedMessageEnvelope fromJson(final JsonObject jsonObject) { final String extractedId = jsonObject.getValueOrThrow(JSON_ID); final String extractedType = jsonObject.getValueOrThrow(JSON_TYPE); final JsonObject extractedMessage = jsonObject.getValueOrThrow(JSON_MESSAGE); final JsonObject jsonDittoHeaders = jsonObject.getValueOrThrow(JSON_DITTO_HEADERS); final DittoHeaders extractedDittoHeaders = DittoHeaders.newBuilder(jsonDittoHeaders).build(); return of(extractedId, extractedType, extractedMessage, extractedDittoHeaders); }
/** * Returns a new {@code ShardedMessageEnvelope} parsed from the specified {@code jsonObject}. * * @param jsonObject the JSON object. * @return the ShardedMessageEnvelope. */ public static ShardedMessageEnvelope fromJson(final JsonObject jsonObject) { final String extractedId = jsonObject.getValueOrThrow(JSON_ID); final String extractedType = jsonObject.getValueOrThrow(JSON_TYPE); final JsonObject extractedMessage = jsonObject.getValueOrThrow(JSON_MESSAGE); final JsonObject jsonDittoHeaders = jsonObject.getValueOrThrow(JSON_DITTO_HEADERS); final DittoHeaders extractedDittoHeaders = DittoHeaders.newBuilder(jsonDittoHeaders).build(); return of(extractedId, extractedType, extractedMessage, extractedDittoHeaders); }
private void retrieveThing() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .correlationId("thingUpdater-sudoRetrieveThing-" + UUID.randomUUID()) .build(); final SudoRetrieveThing sudoRetrieveThingCmd = SudoRetrieveThing.withOriginalSchemaVersion(thingId, dittoHeaders); final String cmdType = sudoRetrieveThingCmd.getType(); final JsonSchemaVersion implementedSchemaVersion = sudoRetrieveThingCmd.getImplementedSchemaVersion(); final Predicate<JsonField> regularOrSpecialFields = FieldType.regularOrSpecial(); final JsonObject cmdJsonObject = sudoRetrieveThingCmd.toJson(implementedSchemaVersion, regularOrSpecialFields); final Object messageEnvelope = ShardedMessageEnvelope.of(thingId, cmdType, cmdJsonObject, dittoHeaders); // Send a message directly to the Things Shard Region. thingsShardRegion.tell(messageEnvelope, getSelf()); }
private void retrieveThing() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .correlationId("thingUpdater-sudoRetrieveThing-" + UUID.randomUUID()) .build(); final SudoRetrieveThing sudoRetrieveThingCmd = SudoRetrieveThing.withOriginalSchemaVersion(thingId, dittoHeaders); final String cmdType = sudoRetrieveThingCmd.getType(); final JsonSchemaVersion implementedSchemaVersion = sudoRetrieveThingCmd.getImplementedSchemaVersion(); final Predicate<JsonField> regularOrSpecialFields = FieldType.regularOrSpecial(); final JsonObject cmdJsonObject = sudoRetrieveThingCmd.toJson(implementedSchemaVersion, regularOrSpecialFields); final Object messageEnvelope = ShardedMessageEnvelope.of(thingId, cmdType, cmdJsonObject, dittoHeaders); // Send a message directly to the Things Shard Region. thingsShardRegion.tell(messageEnvelope, getSelf()); }