Search in sources :

Example 56 with Action0

use of rx.functions.Action0 in project jocean-http by isdom.

the class DefaultSignalClientTestCase method buildResponse.

private static Observable<HttpObject> buildResponse(final Object responseBean, final Action1<Action0> onTerminate) {
    final byte[] responseBytes = JSON.toJSONBytes(responseBean);
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(responseBytes));
    onTerminate.call(new Action0() {

        @Override
        public void call() {
            final boolean released = response.release();
            if (LOG.isDebugEnabled()) {
                LOG.debug("buildBytesResponse release {} released({})", response, released);
            }
        }
    });
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    return Observable.<HttpObject>just(response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Action0(rx.functions.Action0) HttpObject(io.netty.handler.codec.http.HttpObject) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 57 with Action0

use of rx.functions.Action0 in project jocean-http by isdom.

the class DefaultSignalClientTestCase method buildBytesResponse.

private static Observable<HttpObject> buildBytesResponse(final byte[] bodyAsBytes, final Action1<Action0> onTerminate) {
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(bodyAsBytes));
    onTerminate.call(new Action0() {

        @Override
        public void call() {
            final boolean released = response.release();
            if (LOG.isDebugEnabled()) {
                LOG.debug("buildBytesResponse release {} released({})", response, released);
            }
        }
    });
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    return Observable.<HttpObject>just(response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Action0(rx.functions.Action0) HttpObject(io.netty.handler.codec.http.HttpObject) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 58 with Action0

use of rx.functions.Action0 in project jocean-http by isdom.

the class DefaultHttpServerBuilder method defineServer.

public Observable<? extends HttpTrade> defineServer(final SocketAddress localAddress, final Func0<Feature[]> featuresBuilder, final Feature... features) {
    return Observable.unsafeCreate(new Observable.OnSubscribe<HttpTrade>() {

        @Override
        public void call(final Subscriber<? super HttpTrade> subscriber) {
            if (!subscriber.isUnsubscribed()) {
                final ServerBootstrap bootstrap = _creator.newBootstrap();
                final List<Channel> awaitChannels = new CopyOnWriteArrayList<>();
                bootstrap.childHandler(new Initializer() {

                    @Override
                    protected void initChannel(final Channel channel) throws Exception {
                        channel.config().setAutoRead(false);
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("dump inbound channel({})'s config: \n{}", channel, Nettys.dumpChannelConfig(channel.config()));
                        }
                        if (_inboundRecvBufSize > 0) {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("channel({})'s default SO_RCVBUF is {} bytes, and will be reset to {} bytes", channel, channel.config().getOption(ChannelOption.SO_RCVBUF), _inboundRecvBufSize);
                            }
                            channel.config().setOption(ChannelOption.SO_RCVBUF, _inboundRecvBufSize);
                        }
                        final Feature[] actualFeatures = JOArrays.addFirst(Feature[].class, featuresOf(featuresBuilder), features);
                        final Feature[] applyFeatures = (null != actualFeatures && actualFeatures.length > 0) ? actualFeatures : _defaultFeatures;
                        for (Feature feature : applyFeatures) {
                            if (feature instanceof FeatureOverChannelHandler) {
                                ((FeatureOverChannelHandler) feature).call(_APPLY_BUILDER, channel.pipeline());
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("initChannel with feature:{}", feature);
                                }
                            }
                        }
                        Nettys.applyHandler(channel.pipeline(), HttpHandlers.HTTPSERVER);
                        awaitInboundRequest(channel, subscriber, awaitChannels);
                    }
                });
                final ChannelFuture future = bootstrap.bind(localAddress);
                try {
                    future.sync();
                    subscriber.add(RxNettys.subscriptionForCloseChannel(future.channel()));
                    subscriber.add(Subscriptions.create(new Action0() {

                        @Override
                        public void call() {
                            while (!awaitChannels.isEmpty()) {
                                try {
                                    awaitChannels.remove(0).close();
                                } catch (Exception e) {
                                    LOG.warn("exception when remove all awaitChannels, detail: {}", ExceptionUtils.exception2detail(e));
                                }
                            }
                        }
                    }));
                    if (null != features) {
                        final ServerChannelAware serverChannelAware = serverChannelAwareOf(features);
                        if (null != serverChannelAware) {
                            serverChannelAware.setServerChannel((ServerChannel) future.channel());
                        }
                    }
                } catch (Exception e) {
                    subscriber.onError(e);
                }
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Action0(rx.functions.Action0) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) ServerChannelAware(org.jocean.http.util.Nettys.ServerChannelAware) ServerChannel(io.netty.channel.ServerChannel) Feature(org.jocean.http.Feature) Observable(rx.Observable) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) IOException(java.io.IOException) ChannelInitializer(io.netty.channel.ChannelInitializer) FeatureOverChannelHandler(org.jocean.http.Feature.FeatureOverChannelHandler) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 59 with Action0

use of rx.functions.Action0 in project AnDevCon-RxPatterns by colintheshots.

the class Example3 method onResume.

@Override
protected void onResume() {
    super.onResume();
    Observable.create(new Observable.OnSubscribe<String>() {

        @Override
        public void call(Subscriber<? super String> subscriber) {
            for (int i = 0; i <= 10; i++) {
                subscriber.onNext(Integer.toString(i));
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    subscriber.onError(e);
                }
            }
            subscriber.onCompleted();
        }
    }).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "doOnSubscribe()");
        }
    }).doOnNext(new Action1<String>() {

        @Override
        public void call(String s) {
            Log.d(TAG, "doOnNext() " + s);
        }
    }).doOnCompleted(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "doOnCompleted()");
        }
    }).doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "doOnUnSubscribe()");
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            Log.d(TAG, "doOnError() " + throwable.getLocalizedMessage());
        }
    }).doOnTerminate(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "doOnTerminate()");
        }
    }).finallyDo(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "finallyDo()");
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

        @Override
        public void call(String s) {
            mTextView.setText(s);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
        }
    });
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) Observable(rx.Observable)

Example 60 with Action0

use of rx.functions.Action0 in project AnDevCon-RxPatterns by colintheshots.

the class Example6 method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_example6);
    mOutputTextView1 = (TextView) findViewById(R.id.example6_textView1);
    mOutputTextView2 = (TextView) findViewById(R.id.example6_textView2);
    mOutputTextView3 = (TextView) findViewById(R.id.example6_textView3);
    createSub = exampleObservable.doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "Called unsubscribe OnDestroy()");
        }
    }).subscribe(new Action1<Integer>() {

        @Override
        public void call(Integer i) {
            mOutputTextView1.setText(Integer.toString(i) + " OnCreate()");
        }
    }, errorHandler);
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1)

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