akka.actor.Scheduler Java Examples

The following examples show how to use akka.actor.Scheduler. 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: Slave.java    From akka-tutorial with Apache License 2.0 6 votes vote down vote up
private void handle(AddressMessage message) {
	
	// Cancel any running connect schedule, because got a new address
	if (this.connectSchedule != null) {
		this.connectSchedule.cancel();
		this.connectSchedule = null;
	}

	// Find the shepherd actor in the remote actor system
	final ActorSelection selection = this.getContext().getSystem().actorSelection(String.format("%s/user/%s", message.address, Shepherd.DEFAULT_NAME));

	// Register the local actor system by periodically sending subscription messages (until an acknowledgement was received)
	final Scheduler scheduler = this.getContext().getSystem().scheduler();
	final ExecutionContextExecutor dispatcher = this.getContext().getSystem().dispatcher();
	this.connectSchedule = scheduler.schedule(
			Duration.Zero(),
			Duration.create(5, TimeUnit.SECONDS),
			() -> selection.tell(new Shepherd.SubscriptionMessage(), this.getSelf()),
			dispatcher
	);
}
 
Example #2
Source File: GradingResponsePoller.java    From judgels with GNU General Public License v2.0 5 votes vote down vote up
public GradingResponsePoller(Scheduler scheduler, ExecutionContext executor, ProgrammingSubmissionService submissionService, BasicAuthHeader sealtielClientAuthHeader, MessageService messageService, long interval) {
    this.scheduler = scheduler;
    this.executor = executor;
    this.submissionService = submissionService;
    this.sealtielClientAuthHeader = sealtielClientAuthHeader;
    this.messageService = messageService;
    this.interval = interval;
    this.isConnected = false;
}
 
Example #3
Source File: SandalphonThreadsScheduler.java    From judgels with GNU General Public License v2.0 5 votes vote down vote up
@Inject
public SandalphonThreadsScheduler(ActorSystem actorSystem, SandalphonConfiguration config, ProgrammingSubmissionService programmingSubmissionService, @Named("sealtiel") BasicAuthHeader sealtielClientAuthHeader, MessageService messageService) {
    Scheduler scheduler = actorSystem.scheduler();
    ExecutionContextExecutor context = actorSystem.dispatcher();

    GradingResponsePoller poller = new GradingResponsePoller(scheduler, context, programmingSubmissionService, sealtielClientAuthHeader, messageService, TimeUnit.MILLISECONDS.convert(2, TimeUnit.SECONDS));

    if (config.getSealtielConfig().isPresent()) {
        scheduler.schedule(Duration.create(1, TimeUnit.SECONDS), Duration.create(3, TimeUnit.SECONDS), poller, context);
    }
}
 
Example #4
Source File: RabbitMQClientActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private CompletionStage<Status.Status> connect(final Connection connection, final Duration createChannelTimeout,
        final Duration internalReconnectTimeout) {

    final CompletableFuture<Status.Status> future = new CompletableFuture<>();
    if (rmqConnectionActor == null) {
        // complete the future if something went wrong during creation of the connection-factory-factory
        final RabbitMQExceptionHandler rabbitMQExceptionHandler =
                new RabbitMQExceptionHandler(throwable -> future.complete(new Status.Failure(throwable)));

        final Optional<ConnectionFactory> connectionFactoryOpt =
                tryToCreateConnectionFactory(rabbitConnectionFactoryFactory, connection, rabbitMQExceptionHandler);

        if (connectionFactoryOpt.isPresent()) {
            final ConnectionFactory connectionFactory = connectionFactoryOpt.get();

            final Props props = com.newmotion.akka.rabbitmq.ConnectionActor.props(connectionFactory,
                    FiniteDuration.apply(internalReconnectTimeout.getSeconds(), TimeUnit.SECONDS),
                    (rmqConnection, connectionActorRef) -> {
                        log.info("Established RMQ connection: {}", rmqConnection);
                        return null;
                    });

            rmqConnectionActor = startChildActorConflictFree(RMQ_CONNECTION_ACTOR_NAME, props);
            rmqPublisherActor = startRmqPublisherActor();

            // create publisher channel
            final CreateChannel createChannel = CreateChannel.apply(
                    ChannelActor.props((channel, channelActor) -> {
                        log.info("Did set up publisher channel: {}. Telling the publisher actor the new channel",
                                channel);
                        // provide the new channel to the publisher after the channel was connected (also includes reconnects)
                        if (rmqPublisherActor != null) {
                            final ChannelCreated channelCreated = new ChannelCreated(channelActor);
                            rmqPublisherActor.tell(channelCreated, channelActor);
                        }
                        return null;
                    }),
                    Option.apply(PUBLISHER_CHANNEL));


            final Scheduler scheduler = getContext().system().scheduler();
            final ExecutionContext dispatcher = getContext().dispatcher();
            Patterns.ask(rmqConnectionActor, createChannel, createChannelTimeout).handle((reply, throwable) -> {
                if (throwable != null) {
                    future.complete(new Status.Failure(throwable));
                } else {
                    // waiting for "final RabbitMQExceptionHandler rabbitMQExceptionHandler" to get its chance to
                    // complete the future with an Exception before we report Status.Success right now
                    // so delay this by 1 second --
                    // with Java 9 this could be done more elegant with "orTimeout" or "completeOnTimeout" methods:
                    scheduler.scheduleOnce(Duration.ofSeconds(1L),
                            () -> future.complete(new Status.Success("channel created")),
                            dispatcher);
                }
                return null;
            });
        }
    } else {
        log.debug("Connection <{}> is already open.", connectionId());
        future.complete(new Status.Success("already connected"));
    }
    return future;

}
 
Example #5
Source File: AbstractContextAwareMsgProcessor.java    From iotplatform with Apache License 2.0 4 votes vote down vote up
protected Scheduler getScheduler() {
  return systemContext.getScheduler();
}
 
Example #6
Source File: ActorSystemContext.java    From iotplatform with Apache License 2.0 4 votes vote down vote up
public Scheduler getScheduler() {
    return actorSystem.scheduler();
}