private void schedulePendingResponse(final ConnectivityCommandResponse response, final ActorRef sender) { getContext().system().scheduler() .scheduleOnce(flushPendingResponsesTimeout, sender, response, getContext().dispatcher(), getSelf()); }
private void schedulePendingResponse(final ConnectivityCommandResponse response, final ActorRef sender) { getContext().system().scheduler() .scheduleOnce(flushPendingResponsesTimeout, sender, response, getContext().dispatcher(), getSelf()); }
private void stopChildActor(final ActorRef actor) { log.debug("Stopping child actor <{}>.", actor.path()); getContext().stop(actor); }
private void stopChildActor(final ActorRef actor) { log.debug("Stopping child actor <{}>.", actor.path()); getContext().stop(actor); }
private void stopSelfIfDeletedAfterDelay() { final ExecutionContextExecutor dispatcher = getContext().dispatcher(); cancelStopSelfIfDeletedTrigger(); stopSelfIfDeletedTrigger = getContext().system() .scheduler() .scheduleOnce(DELETED_ACTOR_LIFETIME, getSelf(), STOP_SELF_IF_DELETED, dispatcher, ActorRef.noSender()); }
private void stopSelfIfDeletedAfterDelay() { final ExecutionContextExecutor dispatcher = getContext().dispatcher(); cancelStopSelfIfDeletedTrigger(); stopSelfIfDeletedTrigger = getContext().system() .scheduler() .scheduleOnce(DELETED_ACTOR_LIFETIME, getSelf(), STOP_SELF_IF_DELETED, dispatcher, ActorRef.noSender()); }
final ActorRef aggregationActor = getContext().actorOf( AggregateActor.props(connectionId, clientActorRouter, connection.getClientCount(), timeout)); PatternsCS.ask(aggregationActor, cmd, timeout)
private void stopSelf() { log.debug("Shutting down"); // stop the supervisor (otherwise it'd restart this actor) which causes this actor to stop, too. getContext().getParent().tell(PoisonPill.getInstance(), getSelf()); }
private void stopSelf() { log.debug("Shutting down"); // stop the supervisor (otherwise it'd restart this actor) which causes this actor to stop, too. getContext().getParent().tell(PoisonPill.getInstance(), getSelf()); }
private void respondWithCreateConnectionResponse(final Connection connection, final CreateConnection command, final ActorRef origin) { origin.tell(CreateConnectionResponse.of(connection, command.getDittoHeaders()), getSelf()); getContext().getParent().tell(ConnectionSupervisorActor.ManualReset.getInstance(), getSelf()); }
private void respondWithCreateConnectionResponse(final Connection connection, final CreateConnection command, final ActorRef origin) { origin.tell(CreateConnectionResponse.of(connection, command.getDittoHeaders()), getSelf()); getContext().getParent().tell(ConnectionSupervisorActor.ManualReset.getInstance(), getSelf()); }
private ConnectionActor(final String connectionId, final ActorRef pubSubMediator, final ActorRef conciergeForwarder, final ClientActorPropsFactory propsFactory, @Nullable final Consumer<ConnectivityCommand<?>> customCommandValidator) { this.connectionId = connectionId; this.pubSubMediator = pubSubMediator; this.conciergeForwarder = conciergeForwarder; this.propsFactory = propsFactory; final DittoConnectivityCommandValidator dittoCommandValidator = new DittoConnectivityCommandValidator(propsFactory, conciergeForwarder, CONNECTION_VALIDATOR); if (customCommandValidator != null) { this.commandValidator = new CompoundConnectivityCommandInterceptor(dittoCommandValidator, customCommandValidator); } else { this.commandValidator = dittoCommandValidator; } final ConnectionConfigReader configReader = ConnectionConfigReader.fromRawConfig(getContext().system().settings().config()); snapshotThreshold = configReader.snapshotThreshold(); snapshotAdapter = new ConnectionMongoSnapshotAdapter(); connectionCreatedBehaviour = createConnectionCreatedBehaviour(); final java.time.Duration javaFlushTimeout = configReader.flushPendingResponsesTimeout(); flushPendingResponsesTimeout = Duration.create(javaFlushTimeout.toMillis(), TimeUnit.MILLISECONDS); }
private void startClientActorIfRequired() { checkNotNull(connectionId, "connectionId"); checkConnectionNotNull(); if (clientActorRouter == null) { final int clientCount = connection.getClientCount(); log.info("Starting ClientActor for connection <{}> with <{}> clients.", connectionId, clientCount); final Props props = propsFactory.getActorPropsForType(connection, conciergeForwarder); final ClusterRouterPoolSettings clusterRouterPoolSettings = new ClusterRouterPoolSettings(clientCount, 1, true, Collections.singleton(CLUSTER_ROLE)); final RoundRobinPool roundRobinPool = new RoundRobinPool(clientCount); final Props clusterRouterPoolProps = new ClusterRouterPool(roundRobinPool, clusterRouterPoolSettings).props(props); // start client actor without name so it does not conflict with its previous incarnation clientActorRouter = getContext().actorOf(clusterRouterPoolProps); } else { log.debug("ClientActor already started."); } }
private void startClientActorIfRequired() { checkNotNull(connectionId, "connectionId"); checkConnectionNotNull(); if (clientActorRouter == null) { final int clientCount = connection.getClientCount(); log.info("Starting ClientActor for connection <{}> with <{}> clients.", connectionId, clientCount); final Props props = propsFactory.getActorPropsForType(connection, conciergeForwarder); final ClusterRouterPoolSettings clusterRouterPoolSettings = new ClusterRouterPoolSettings(clientCount, 1, true, Collections.singleton(CLUSTER_ROLE)); final RoundRobinPool roundRobinPool = new RoundRobinPool(clientCount); final Props clusterRouterPoolProps = new ClusterRouterPool(roundRobinPool, clusterRouterPoolSettings).props(props); // start client actor without name so it does not conflict with its previous incarnation clientActorRouter = getContext().actorOf(clusterRouterPoolProps); } else { log.debug("ClientActor already started."); } }
private ConnectionActor(final String connectionId, final ActorRef pubSubMediator, final ActorRef conciergeForwarder, final ClientActorPropsFactory propsFactory, @Nullable final Consumer<ConnectivityCommand<?>> customCommandValidator) { this.connectionId = connectionId; this.pubSubMediator = pubSubMediator; this.conciergeForwarder = conciergeForwarder; this.propsFactory = propsFactory; final DittoConnectivityCommandValidator dittoCommandValidator = new DittoConnectivityCommandValidator(propsFactory, conciergeForwarder, CONNECTION_VALIDATOR); if (customCommandValidator != null) { this.commandValidator = new CompoundConnectivityCommandInterceptor(dittoCommandValidator, customCommandValidator); } else { this.commandValidator = dittoCommandValidator; } final ConnectionConfigReader configReader = ConnectionConfigReader.fromRawConfig(getContext().system().settings().config()); snapshotThreshold = configReader.snapshotThreshold(); snapshotAdapter = new ConnectionMongoSnapshotAdapter(); connectionCreatedBehaviour = createConnectionCreatedBehaviour(); final java.time.Duration javaFlushTimeout = configReader.flushPendingResponsesTimeout(); flushPendingResponsesTimeout = Duration.create(javaFlushTimeout.toMillis(), TimeUnit.MILLISECONDS); clientActorAskTimeout = configReader.clientActorAskTimeout(); LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId); }
private void createConnection(final CreateConnection command) { final ActorRef origin = getSender(); final ActorRef self = getSelf(); final ActorRef parent = getContext().getParent(); getContext().become(connectionCreatedBehaviour);
private void handleModifyConnection(final ModifyConnection command, final ActorRef origin) { checkNotNull(connection, "Connection"); final ActorRef self = getSelf(); final ActorRef parent = getContext().getParent(); final DittoHeaders dittoHeaders = command.getDittoHeaders(); final ConnectivityCommandResponse commandResponse = ModifyConnectionResponse.modified(connectionId, dittoHeaders); if (ConnectionStatus.OPEN.equals(connection.getConnectionStatus())) { final OpenConnection openConnectionAfterModification = OpenConnection.of(connectionId, dittoHeaders); log.debug("Desired connection state is {}, forwarding {} to client actor.", connection.getConnectionStatus(), openConnectionAfterModification); askClientActor(openConnectionAfterModification, response -> { final PerformTask performTask = new PerformTask("subscribe for events and schedule ModifyConnectionResponse", subscribeForEventsAndScheduleResponse(commandResponse, origin)); parent.tell(ConnectionSupervisorActor.ManualReset.getInstance(), self); self.tell(performTask, ActorRef.noSender()); }, error -> handleException("connect-after-modify", origin, error) ); } else { log.debug("Desired connection state is {}, do not open connection.", connection.getConnectionStatus()); origin.tell(commandResponse, getSelf()); } }
private void handleModifyConnection(final ModifyConnection command, final ActorRef origin) { checkNotNull(connection, "Connection"); final ActorRef self = getSelf(); final ActorRef parent = getContext().getParent(); final DittoHeaders dittoHeaders = command.getDittoHeaders(); final ConnectivityCommandResponse commandResponse = ModifyConnectionResponse.modified(connectionId, dittoHeaders); if (ConnectionStatus.OPEN.equals(connection.getConnectionStatus())) { final OpenConnection openConnectionAfterModification = OpenConnection.of(connectionId, dittoHeaders); log.debug("Desired connection state is {}, forwarding {} to client actor.", connection.getConnectionStatus(), openConnectionAfterModification); askClientActor(openConnectionAfterModification, response -> { final PerformTask performTask = new PerformTask("subscribe for events and schedule ModifyConnectionResponse", subscribeForEventsAndScheduleResponse(commandResponse, origin)); parent.tell(ConnectionSupervisorActor.ManualReset.getInstance(), self); self.tell(performTask, ActorRef.noSender()); }, error -> handleException("connect-after-modify", origin, error) ); } else { log.debug("Desired connection state is {}, do not open connection.", connection.getConnectionStatus()); origin.tell(commandResponse, 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()); }); }); }