use of rx.functions.Action0 in project rxjava-file by davidmoten.
the class FileObservableTest method testFileTailingFromStartOfFile.
@Test
public void testFileTailingFromStartOfFile() throws InterruptedException, IOException {
final File log = new File("target/test.log");
log.delete();
log.createNewFile();
append(log, "a0");
Observable<String> tailer = FileObservable.tailer().file(log).onWatchStarted(new Action0() {
@Override
public void call() {
append(log, "a1");
append(log, "a2");
}
}).sampleTimeMs(50).utf8().tailText();
final List<String> list = new ArrayList<String>();
final CountDownLatch latch = new CountDownLatch(3);
Subscription sub = tailer.subscribeOn(Schedulers.io()).subscribe(new Action1<String>() {
@Override
public void call(String line) {
System.out.println("received: '" + line + "'");
list.add(line);
latch.countDown();
}
});
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertEquals(Arrays.asList("a0", "a1", "a2"), list);
sub.unsubscribe();
}
use of rx.functions.Action0 in project rxjava-file by davidmoten.
the class FileObservableTest method testFileTailingWhenFileIsCreatedAfterSubscription.
@Test
public void testFileTailingWhenFileIsCreatedAfterSubscription() throws InterruptedException, IOException {
final File log = new File("target/test.log");
log.delete();
append(log, "a0");
Observable<String> tailer = FileObservable.tailer().file(log).startPosition(0).sampleTimeMs(50).utf8().onWatchStarted(new Action0() {
@Override
public void call() {
try {
log.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
append(log, "a1");
append(log, "a2");
}
}).tailText();
final List<String> list = new ArrayList<String>();
final CountDownLatch latch = new CountDownLatch(3);
Subscription sub = tailer.subscribeOn(Schedulers.io()).subscribe(new Action1<String>() {
@Override
public void call(String line) {
System.out.println("received: '" + line + "'");
list.add(line);
latch.countDown();
}
});
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertEquals(Arrays.asList("a0", "a1", "a2"), list);
sub.unsubscribe();
}
use of rx.functions.Action0 in project jocean-http by isdom.
the class StreamUtil method src2dwb.
public static <SRC, STATE> Transformer<SRC, DisposableWrapper<ByteBuf>> src2dwb(final Func0<DisposableWrapper<ByteBuf>> newdwb, final Func1<SRC, byte[]> src2bytes, final Func1<SRC, STATE> newstate, final Action2<SRC, STATE> updatestate) {
final AtomicReference<DisposableWrapper<ByteBuf>> ref = new AtomicReference<>();
return new Transformer<SRC, DisposableWrapper<ByteBuf>>() {
@Override
public Observable<DisposableWrapper<ByteBuf>> call(final Observable<SRC> upstream) {
return upstream.flatMap(new Func1<SRC, Observable<DisposableWrapper<ByteBuf>>>() {
@Override
public Observable<DisposableWrapper<ByteBuf>> call(final SRC src) {
if (null == ref.get()) {
ref.set(newdwb.call());
StateableUtil.setStateTo(newstate.call(src), ref.get());
}
final byte[] bytes = src2bytes.call(src);
if (bytes.length <= ref.get().unwrap().maxWritableBytes()) {
ref.get().unwrap().writeBytes(bytes);
updatestate.call(src, StateableUtil.<STATE>stateOf(ref.get()));
return Observable.empty();
} else {
final DisposableWrapper<ByteBuf> newbuf = newdwb.call();
newbuf.unwrap().writeBytes(bytes);
StateableUtil.setStateTo(newstate.call(src), newbuf);
return Observable.just(ref.getAndSet(newbuf));
}
}
}, new Func1<Throwable, Observable<DisposableWrapper<ByteBuf>>>() {
@Override
public Observable<DisposableWrapper<ByteBuf>> call(final Throwable e) {
return Observable.error(e);
}
}, new Func0<Observable<DisposableWrapper<ByteBuf>>>() {
@Override
public Observable<DisposableWrapper<ByteBuf>> call() {
if (null == ref.get()) {
LOG.debug("src2dwb onCompleted with ref is null");
return Observable.empty();
} else {
final DisposableWrapper<ByteBuf> last = ref.getAndSet(null);
if (last.unwrap().readableBytes() > 0) {
LOG.debug("src2dwb onCompleted with last as content");
return Observable.just(last);
} else {
last.dispose();
LOG.debug("src2dwb onCompleted with last NO content");
return Observable.empty();
}
}
}
}).doOnUnsubscribe(new Action0() {
@Override
public void call() {
final DisposableWrapper<ByteBuf> last = ref.getAndSet(null);
if (null != last) {
LOG.debug("src2dwb doOnUnsubscribe with last disposed.");
last.dispose();
}
}
});
}
};
}
use of rx.functions.Action0 in project jocean-http by isdom.
the class DefaultChannelPool method recycleChannel.
@Override
public boolean recycleChannel(final Channel channel) {
if (channel.isActive() && Nettys.isChannelReady(channel)) {
final SocketAddress address = channel.remoteAddress();
if (null != address) {
final Queue<Channel> channels = getOrCreateChannels(address);
channels.add(channel);
Nettys.applyHandler(channel.pipeline(), _onChannelInactive, new Action0() {
@Override
public void call() {
channels.remove(channel);
channel.close();
LOG.info("removeChannel: channel({}) inactive, so remove from pool.", channel);
}
});
LOG.info("recycleChannel: channel({}) save to queue for ({}), can be reused.", channel, address);
return true;
}
}
channel.close();
LOG.info("recycleChannel: try recycle channel({}), BUT it has been closed.", channel);
return false;
}
use of rx.functions.Action0 in project photon-model by vmware.
the class AzureUtils method injectOperationContext.
public static Action0 injectOperationContext(Action0 original) {
OperationContext operationContext = OperationContext.getOperationContext();
return new Action0() {
@Override
public void call() {
OperationContext.restoreOperationContext(operationContext);
original.call();
}
};
}
Aggregations