Search in sources :

Example 51 with Action0

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();
}
Also used : Action0(rx.functions.Action0) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) File(java.io.File) Test(org.junit.Test)

Example 52 with Action0

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();
}
Also used : Action0(rx.functions.Action0) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) File(java.io.File) Test(org.junit.Test)

Example 53 with Action0

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();
                    }
                }
            });
        }
    };
}
Also used : Action0(rx.functions.Action0) Transformer(rx.Observable.Transformer) DisposableWrapper(org.jocean.idiom.DisposableWrapper) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) Func1(rx.functions.Func1) Func0(rx.functions.Func0)

Example 54 with Action0

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;
}
Also used : Action0(rx.functions.Action0) Channel(io.netty.channel.Channel) SocketAddress(java.net.SocketAddress)

Example 55 with Action0

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();
        }
    };
}
Also used : OperationContext(com.vmware.xenon.common.OperationContext) Action0(rx.functions.Action0)

Aggregations

Action0 (rx.functions.Action0)134 Subscription (rx.Subscription)58 Test (org.junit.Test)56 CountDownLatch (java.util.concurrent.CountDownLatch)50 Action1 (rx.functions.Action1)28 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 ArrayList (java.util.ArrayList)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 List (java.util.List)15 Func1 (rx.functions.Func1)13 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)12 Observable (rx.Observable)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 OnClick (butterknife.OnClick)10 IOException (java.io.IOException)9 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)8 UiThreadTest (android.support.test.annotation.UiThreadTest)7 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)7 TestCollapserTimer (com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer)7 Method (java.lang.reflect.Method)7