private <T extends ConnectivityCommand> void validateAndForward(final T command, final Consumer<T> target) { final ActorRef origin = getSender(); try { commandValidator.accept(command); target.accept(command); } catch (final Exception e) { handleException(command.getType(), origin, e); stopSelf(); } }
private <T extends ConnectivityCommand> void validateAndForward(final T command, final Consumer<T> target) { final ActorRef origin = getSender(); try { commandValidator.accept(command); target.accept(command); } catch (final Exception e) { handleException(command.getType(), origin, e); stopSelf(); } }
private void retrieveConnection(final RetrieveConnection command) { checkConnectionNotNull(); getSender().tell(RetrieveConnectionResponse.of(connection, command.getDittoHeaders()), getSelf()); }
private void retrieveConnection(final RetrieveConnection command) { checkConnectionNotNull(); getSender().tell(RetrieveConnectionResponse.of(connection, command.getDittoHeaders()), getSelf()); }
private void retrieveConnectionStatus(final RetrieveConnectionStatus command) { checkConnectionNotNull(); getSender().tell(RetrieveConnectionStatusResponse.of(connectionId, connection.getConnectionStatus(), command.getDittoHeaders()), getSelf()); }
private void retrieveConnectionMetrics(final RetrieveConnectionMetrics command) { checkConnectionNotNull(); final ActorRef origin = getSender(); askClientActorIfStarted(command, response -> origin.tell(response, getSelf()), error -> handleException("retrieve-metrics", origin, error), () -> respondWithEmptyMetrics(command, origin)); }
private void retrieveConnectionStatus(final RetrieveConnectionStatus command) { checkConnectionNotNull(); getSender().tell(RetrieveConnectionStatusResponse.of(connectionId, connection.getConnectionStatus(), command.getDittoHeaders()), getSelf()); }
private void retrieveConnectionMetrics(final RetrieveConnectionMetrics command) { checkConnectionNotNull(); final ActorRef origin = getSender(); askClientActorIfStarted(command, response -> origin.tell(response, getSelf()), error -> handleException("retrieve-metrics", origin, error), () -> respondWithEmptyMetrics(command, origin)); }
private void handleCommandDuringInitialization(final ConnectivityCommand command) { log.debug("Unexpected command during initialization of actor received: {} - " + "Terminating this actor and sending 'ConnectionNotAccessibleException' to requester ...", command.getType()); getSender().tell(ConnectionNotAccessibleException.newBuilder(command.getId()) .dittoHeaders(command.getDittoHeaders()) .build(), getSelf()); }
private void handleCommandDuringInitialization(final ConnectivityCommand command) { log.debug("Unexpected command during initialization of actor received: {} - " + "Terminating this actor and sending 'ConnectionNotAccessibleException' to requester ...", command.getType()); getSender().tell(ConnectionNotAccessibleException.newBuilder(command.getId()) .dittoHeaders(command.getDittoHeaders()) .build(), getSelf()); }
private void deleteConnection(final DeleteConnection command) { final ConnectionDeleted connectionDeleted = ConnectionDeleted.of(command.getConnectionId(), command.getDittoHeaders()); final ActorRef origin = getSender(); final ActorRef self = getSelf(); persistEvent(connectionDeleted, persistedEvent -> { stopClientActor(); origin.tell(DeleteConnectionResponse.of(connectionId, command.getDittoHeaders()), self); stopSelf(); // All subscriptions stop automatically once this actor stops. // connectionActor.unsubscribeFromEvents(); }); }
private void deleteConnection(final DeleteConnection command) { final ConnectionDeleted connectionDeleted = ConnectionDeleted.of(command.getConnectionId(), command.getDittoHeaders()); final ActorRef origin = getSender(); final ActorRef self = getSelf(); persistEvent(connectionDeleted, persistedEvent -> { stopClientActor(); origin.tell(DeleteConnectionResponse.of(connectionId, command.getDittoHeaders()), self); stopSelf(); // All subscriptions stop automatically once this actor stops. // connectionActor.unsubscribeFromEvents(); }); }
private void testConnection(final TestConnection command) { final ActorRef origin = getSender(); final ActorRef self = getSelf(); restoreConnection(command.getConnection()); final PerformTask stopSelfTask = new PerformTask("stop self after test", ConnectionActor::stopSelf); askClientActor(command, response -> { origin.tell(TestConnectionResponse.success(command.getConnectionId(), response.toString(), command.getDittoHeaders()), self); // terminate this actor's supervisor after a connection test again: self.tell(stopSelfTask, ActorRef.noSender()); }, error -> { handleException("test", origin, error); // terminate this actor's supervisor after a connection test again: self.tell(stopSelfTask, ActorRef.noSender()); }); }
private void testConnection(final TestConnection command) { final ActorRef origin = getSender(); final ActorRef self = getSelf(); restoreConnection(command.getConnection()); final PerformTask stopSelfTask = new PerformTask("stop self after test", ConnectionActor::stopSelf); askClientActor(command, response -> { origin.tell(TestConnectionResponse.success(command.getConnectionId(), response.toString(), command.getDittoHeaders()), self); // terminate this actor's supervisor after a connection test again: self.tell(stopSelfTask, ActorRef.noSender()); }, error -> { handleException("test", origin, error); // terminate this actor's supervisor after a connection test again: self.tell(stopSelfTask, ActorRef.noSender()); }); }
private Receive createConnectionCreatedBehaviour() { return ReceiveBuilder.create() .match(TestConnection.class, testConnection -> getSender().tell(TestConnectionResponse.alreadyCreated(testConnection.getConnectionId(), testConnection.getDittoHeaders()), getSelf())) .match(CreateConnection.class, createConnection -> { .dittoHeaders(createConnection.getDittoHeaders()) .build(); getSender().tell(conflictException, getSelf()); }) .match(ModifyConnection.class,
private void openConnection(final OpenConnection command) { checkConnectionNotNull(); final ConnectionOpened connectionOpened = ConnectionOpened.of(command.getConnectionId(), command.getDittoHeaders()); final ActorRef origin = getSender(); final ActorRef self = getSelf(); persistEvent(connectionOpened, persistedEvent -> { restoreConnection(connection.toBuilder().connectionStatus(ConnectionStatus.OPEN).build()); askClientActor(command, response -> { final ConnectivityCommandResponse commandResponse = OpenConnectionResponse.of(connectionId, command.getDittoHeaders()); final PerformTask performTask = new PerformTask("open connection", subscribeForEventsAndScheduleResponse(commandResponse, origin)); self.tell(performTask, ActorRef.noSender()); }, error -> handleException("open-connection", origin, error) ); }); }
private void openConnection(final OpenConnection command) { checkConnectionNotNull(); final ConnectionOpened connectionOpened = ConnectionOpened.of(command.getConnectionId(), command.getDittoHeaders()); final ActorRef origin = getSender(); final ActorRef self = getSelf(); persistEvent(connectionOpened, persistedEvent -> { restoreConnection(connection.toBuilder().connectionStatus(ConnectionStatus.OPEN).build()); askClientActor(command, response -> { final ConnectivityCommandResponse commandResponse = OpenConnectionResponse.of(connectionId, command.getDittoHeaders()); final PerformTask performTask = new PerformTask("open connection", subscribeForEventsAndScheduleResponse(commandResponse, origin)); self.tell(performTask, ActorRef.noSender()); }, error -> handleException("open-connection", origin, error) ); }); }
final ActorRef origin = getSender(); final ActorRef self = getSelf();
private void modifyConnection(final ModifyConnection command) { final ActorRef origin = getSender(); final ActorRef self = getSelf(); if (connection != null && !connection.getConnectionType().equals(command.getConnection().getConnectionType())) { handleException("modify", origin, ConnectionConfigurationInvalidException .newBuilder("ConnectionType <" + connection.getConnectionType().getName() + "> of existing connection <" + connectionId + "> cannot be changed!") .dittoHeaders(command.getDittoHeaders()) .build() ); return; } persistEvent(ConnectionModified.of(command.getConnection(), command.getDittoHeaders()), persistedEvent -> { restoreConnection(persistedEvent.getConnection()); getContext().become(connectionCreatedBehaviour); // if client actor is started: send an artificial CloseConnection command to gracefully disconnect and stop the child actors askClientActorIfStarted(CloseConnection.of(connectionId, DittoHeaders.empty()), onSuccess -> { final PerformTask modifyTask = createModifyConnectionTask(true, command, origin); self.tell(modifyTask, ActorRef.noSender()); }, error -> handleException("connect-after-modify", origin, error), () -> { final PerformTask modifyTask = createModifyConnectionTask(false, command, origin); self.tell(modifyTask, ActorRef.noSender()); }); }); }
private void modifyConnection(final ModifyConnection command) { final ActorRef origin = getSender(); final ActorRef self = getSelf(); if (connection != null && !connection.getConnectionType().equals(command.getConnection().getConnectionType())) { handleException("modify", origin, ConnectionConfigurationInvalidException .newBuilder("ConnectionType <" + connection.getConnectionType().getName() + "> of existing connection <" + connectionId + "> cannot be changed!") .dittoHeaders(command.getDittoHeaders()) .build() ); return; } persistEvent(ConnectionModified.of(command.getConnection(), command.getDittoHeaders()), persistedEvent -> { restoreConnection(persistedEvent.getConnection()); getContext().become(connectionCreatedBehaviour); // if client actor is started: send an artificial CloseConnection command to gracefully disconnect and stop the child actors askClientActorIfStarted(CloseConnection.of(connectionId, DittoHeaders.empty()), onSuccess -> { final PerformTask modifyTask = createModifyConnectionTask(true, command, origin); self.tell(modifyTask, ActorRef.noSender()); }, error -> handleException("connect-after-modify", origin, error), () -> { final PerformTask modifyTask = createModifyConnectionTask(false, command, origin); self.tell(modifyTask, ActorRef.noSender()); }); }); }