Search in sources :

Example 21 with Scheduler

use of rx.Scheduler in project tutorials by eugenp.

the class SchedulersLiveTest method givenWorker_whenScheduledOnNewThread_thenResultIsBoundToNewThread.

// it's not safe, not every time is running correctly
@Ignore
@Test
public void givenWorker_whenScheduledOnNewThread_thenResultIsBoundToNewThread() throws InterruptedException {
    System.out.println("newThread_1");
    Scheduler scheduler = Schedulers.newThread();
    Scheduler.Worker worker = scheduler.createWorker();
    worker.schedule(() -> {
        result += Thread.currentThread().getName() + "_Start";
        worker.schedule(() -> result += "_worker_");
        result += "_End";
    });
    await().until(() -> assertTrue(result.equals("RxNewThreadScheduler-1_Start_End_worker_")));
}
Also used : TestScheduler(rx.schedulers.TestScheduler) Scheduler(rx.Scheduler) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 22 with Scheduler

use of rx.Scheduler in project tutorials by eugenp.

the class SchedulersLiveTest method givenScheduledWorker_whenScheduleAnAction_thenResultAction.

@Test
public void givenScheduledWorker_whenScheduleAnAction_thenResultAction() throws InterruptedException {
    System.out.println("scheduling");
    Scheduler scheduler = Schedulers.immediate();
    Scheduler.Worker worker = scheduler.createWorker();
    worker.schedule(() -> result += "action");
    assertTrue(result.equals("action"));
}
Also used : TestScheduler(rx.schedulers.TestScheduler) Scheduler(rx.Scheduler) Test(org.junit.Test)

Example 23 with Scheduler

use of rx.Scheduler in project tutorials by eugenp.

the class SchedulersLiveTest method givenWorker_whenScheduledOnTrampoline_thenComposeResultAsBlocking.

@Test
public void givenWorker_whenScheduledOnTrampoline_thenComposeResultAsBlocking() throws InterruptedException {
    System.out.println("trampoline_2");
    Scheduler scheduler = Schedulers.trampoline();
    Scheduler.Worker worker = scheduler.createWorker();
    worker.schedule(() -> {
        result += Thread.currentThread().getName() + "Start";
        worker.schedule(() -> {
            result += "_middleStart";
            worker.schedule(() -> result += "_worker_");
            result += "_middleEnd";
        });
        result += "_mainEnd";
    });
    await().until(() -> assertTrue(result.equals("mainStart_mainEnd_middleStart_middleEnd_worker_")));
}
Also used : TestScheduler(rx.schedulers.TestScheduler) Scheduler(rx.Scheduler) Test(org.junit.Test)

Example 24 with Scheduler

use of rx.Scheduler in project scalecube by scalecube.

the class MembershipProtocolImpl method doInitialSync.

// ================================================
// ============== Action Methods ==================
// ================================================
private CompletableFuture<Void> doInitialSync() {
    LOGGER.debug("Making initial Sync to all seed members: {}", seedMembers);
    if (seedMembers.isEmpty()) {
        schedulePeriodicSync();
        return CompletableFuture.completedFuture(null);
    }
    CompletableFuture<Void> syncResponseFuture = new CompletableFuture<>();
    // Listen initial Sync Ack
    String cid = memberRef.get().id();
    transport.listen().observeOn(scheduler).filter(msg -> SYNC_ACK.equals(msg.qualifier())).filter(msg -> cid.equals(msg.correlationId())).filter(this::checkSyncGroup).take(1).timeout(config.getSyncTimeout(), TimeUnit.MILLISECONDS, scheduler).subscribe(message -> {
        SyncData syncData = message.data();
        LOGGER.info("Joined cluster '{}': {}", syncData.getSyncGroup(), syncData.getMembership());
        onSyncAck(message, true);
        schedulePeriodicSync();
        syncResponseFuture.complete(null);
    }, throwable -> {
        LOGGER.info("Timeout getting initial SyncAck from seed members: {}", seedMembers);
        schedulePeriodicSync();
        syncResponseFuture.complete(null);
    });
    Message syncMsg = prepareSyncDataMsg(SYNC, cid);
    seedMembers.forEach(address -> transport.send(address, syncMsg));
    return syncResponseFuture;
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) FailureDetector(io.scalecube.cluster.fdetector.FailureDetector) ScheduledFuture(java.util.concurrent.ScheduledFuture) Address(io.scalecube.transport.Address) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Observable(rx.Observable) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Schedulers(rx.schedulers.Schedulers) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Transport(io.scalecube.transport.Transport) Message(io.scalecube.transport.Message) Subscribers(rx.observers.Subscribers) ClusterMath(io.scalecube.cluster.ClusterMath) GossipProtocol(io.scalecube.cluster.gossip.GossipProtocol) Logger(org.slf4j.Logger) Subscriber(rx.Subscriber) Collection(java.util.Collection) Set(java.util.Set) FailureDetectorEvent(io.scalecube.cluster.fdetector.FailureDetectorEvent) Scheduler(rx.Scheduler) DEAD(io.scalecube.cluster.membership.MemberStatus.DEAD) Subject(rx.subjects.Subject) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ALIVE(io.scalecube.cluster.membership.MemberStatus.ALIVE) Member(io.scalecube.cluster.Member) Preconditions(com.google.common.base.Preconditions) PublishSubject(rx.subjects.PublishSubject) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) Message(io.scalecube.transport.Message)

Example 25 with Scheduler

use of rx.Scheduler in project scalecube by scalecube.

the class NettyClientTransport method connect.

private Observable<ChannelContext> connect(Address address) {
    AsyncSubject<ChannelContext> subject = AsyncSubject.create();
    ChannelFuture channelFuture = bootstrap.connect(address.host(), address.port());
    channelFuture.addListener((ChannelFutureListener) future -> {
        Throwable error = future.cause();
        if (future.isSuccess()) {
            future.channel().pipeline().fireChannelActive();
            try {
                ChannelContext channelContext = ChannelSupport.getChannelContextOrThrow(future.channel());
                channelContext.listenClose(input -> outboundChannels.remove(address));
                subject.onNext(channelContext);
                subject.onCompleted();
            } catch (Exception throwable) {
                error = throwable;
            }
        }
        if (error != null) {
            outboundChannels.remove(address);
            subject.onError(future.cause());
        }
    });
    Scheduler scheduler = Schedulers.from(channelFuture.channel().eventLoop());
    return subject.subscribeOn(scheduler);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelContext(io.scalecube.streams.ChannelContext) Address(io.scalecube.transport.Address) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Scheduler(rx.Scheduler) ChannelFuture(io.netty.channel.ChannelFuture) Observable(rx.Observable) ConcurrentMap(java.util.concurrent.ConcurrentMap) Consumer(java.util.function.Consumer) Bootstrap(io.netty.bootstrap.Bootstrap) AsyncSubject(rx.subjects.AsyncSubject) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Schedulers(rx.schedulers.Schedulers) ChannelContext(io.scalecube.streams.ChannelContext) Scheduler(rx.Scheduler)

Aggregations

Scheduler (rx.Scheduler)27 Test (org.junit.Test)13 TestScheduler (rx.schedulers.TestScheduler)9 Observable (rx.Observable)7 Schedulers (rx.schedulers.Schedulers)7 Executors (java.util.concurrent.Executors)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Logger (org.slf4j.Logger)5 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)4 Member (io.scalecube.cluster.Member)4 Message (io.scalecube.transport.Message)4 Transport (io.scalecube.transport.Transport)4 Collections (java.util.Collections)4 ExecutorService (java.util.concurrent.ExecutorService)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 TimeUnit (java.util.concurrent.TimeUnit)4 LoggerFactory (org.slf4j.LoggerFactory)4 Collection (java.util.Collection)3 ScheduledFuture (java.util.concurrent.ScheduledFuture)3