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