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_")));
}
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"));
}
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_")));
}
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;
}
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);
}
Aggregations