Java Code Examples for akka.actor.AbstractActor#Receive

The following examples show how to use akka.actor.AbstractActor#Receive . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SafeRecovery.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates a Receive for recovery such that exceptions are logged as warnings
 * and not thrown. Recovery messages causing exceptions have no effect.
 *
 * @param log The Akka logger to write warnings to.
 * @param receiveRecover The Receive to wrap around.
 *
 * @return the created Receive.
 */
public static AbstractActor.Receive wrapReceive(
        @Nullable final DiagnosticLoggingAdapter log,
        @Nonnull final AbstractActor.Receive receiveRecover) {
    return ReceiveBuilder.create().matchAny(x -> {
        try {
            receiveRecover.onMessage().apply(x);
        } catch (final Exception error) {
            if (log != null) {
                log.warning("Failed to recover from the following message (it is ignored): {}", x);
            }
        }
    }).build();
}
 
Example 2
Source File: ModifyConfigBehavior.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Injectable behavior to handle {@code ModifyConfig}.
 *
 * @return behavior to handle {@code ModifyConfig}.
 */
default AbstractActor.Receive modifyConfigBehavior() {
    return ReceiveBuilder.create()
            .match(ModifyConfig.class, cmd -> {
                final Config newConfig = setConfig(ConfigFactory.parseString(cmd.getConfig().toString()));
                final JsonObject newConfigJson =
                        JsonObject.of(newConfig.root().render(ConfigRenderOptions.concise()));
                final ModifyConfigResponse response =
                        ModifyConfigResponse.of(newConfigJson, cmd.getDittoHeaders());
                sender().tell(response, self());
            })
            .build();
}
 
Example 3
Source File: AbstractHttpRequestActor.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public AbstractActor.Receive createReceive() {
    return ReceiveBuilder.create()
            .match(Status.Failure.class, failure -> {
                Throwable cause = failure.cause();
                if (cause instanceof JsonRuntimeException) {
                    // wrap JsonRuntimeExceptions
                    cause = new DittoJsonException((RuntimeException) cause);
                }
                if (cause instanceof DittoRuntimeException) {
                    handleDittoRuntimeException((DittoRuntimeException) cause);
                } else if (cause instanceof EntityStreamSizeException) {
                    logger.warning("Got EntityStreamSizeException when a 'Command' was expected which means that" +
                            " the max. allowed http payload size configured in Akka was overstepped in this" +
                            " request.");
                    completeWithResult(
                            HttpResponse.create().withStatus(HttpStatusCode.REQUEST_ENTITY_TOO_LARGE.toInt()));
                } else {
                    logger.error(cause, "Got unknown Status.Failure when a 'Command' was expected.");
                    completeWithResult(
                            HttpResponse.create().withStatus(HttpStatusCode.INTERNAL_SERVER_ERROR.toInt()));
                }
            })
            .match(Whoami.class, this::handleWhoami)
            .match(DittoRuntimeException.class, this::handleDittoRuntimeException)
            .match(ReceiveTimeout.class,
                    receiveTimeout -> handleDittoRuntimeException(GatewayServiceUnavailableException.newBuilder()
                            .dittoHeaders(DittoHeaders.empty())
                            .build()))
            .match(Command.class, command -> !isResponseRequired(command), this::handleCommandWithoutResponse)
            .match(ThingModifyCommand.class, this::handleThingModifyCommand)
            .match(MessageCommand.class, this::handleMessageCommand)
            .match(Command.class, command -> handleCommandWithResponse(command,
                    getResponseAwaitingBehavior(getTimeoutExceptionSupplier(command))))
            .matchAny(m -> {
                logger.warning("Got unknown message, expected a 'Command': {}", m);
                completeWithResult(HttpResponse.create().withStatus(HttpStatusCode.INTERNAL_SERVER_ERROR.toInt()));
            })
            .build();
}
 
Example 4
Source File: BackOffActor.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public AbstractActor.Receive createReceive() {
    return ReceiveBuilder.create()
            .match(BackOffWithAnswer.class, this::backOff)
            .match(BackOffWithSender.class, this::afterBackOff)
            .matchEquals(IsInBackOff.INSTANCE, this::handleIsInBackOff)
            .matchEquals(RESET_BACK_OFF, this::resetBackOff)
            .matchAny(m -> {
                log.warning("Unknown message: {}", m);
                unhandled(m);
            })
            .build();
}