use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project apiRecord by tobecoder2015.
the class HttpProxyServerHandle method handleProxyData.
private void handleProxyData(final ChannelHandlerContext ctx, final Object msg) throws InterruptedException {
if (cf == null) {
Bootstrap bootstrap = new Bootstrap();
// 注册线程池
bootstrap.group(NettyHttpProxyServer.proxyGroup).channel(// 使用NioSocketChannel来作为连接用的channel类
NioSocketChannel.class).handler(new HttpProxyInitializer(ctx.channel(), isSSL));
cf = bootstrap.connect(host, port).sync();
/*cf.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
System.out.println("11111"+msg);
if (future.isSuccess()) {
future.channel().writeAndFlush(msg);
} else {
ctx.channel().close();
}
}
});*/
}
cf.channel().writeAndFlush(msg);
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project jackrabbit-oak by apache.
the class ForwardHandler method channelActive.
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
group = new NioEventLoopGroup(0, r -> {
return new Thread(r, String.format("forward-handler-%d", threadNumber.getAndIncrement()));
});
Bootstrap b = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
if (flipPosition >= 0) {
ch.pipeline().addLast(new FlipHandler(flipPosition));
}
if (skipBytes > 0) {
ch.pipeline().addLast(new SkipHandler(skipPosition, skipBytes));
}
ch.pipeline().addLast(new BackwardHandler(ctx.channel()));
}
});
remote = b.connect(targetHost, targetPort).sync().channel();
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project jocean-http by isdom.
the class SignalTest method main.
/**
* @param args
* @throws Exception
* @throws Exception
*/
public static void main(String[] args) throws Exception {
final TestChannelPool pool = new TestChannelPool(1);
final HttpClient httpClient = new DefaultHttpClient(new AbstractChannelCreator() {
@Override
protected void initializeBootstrap(final Bootstrap bootstrap) {
bootstrap.group(new NioEventLoopGroup(1)).channel(NioSocketChannel.class);
}
}, pool, ENABLE_LOGGING, ENABLE_COMPRESSOR);
final DefaultSignalClient client = new DefaultSignalClient(httpClient);
client.registerRequestType(FetchPatientsRequest.class, FetchPatientsResponse.class, new URI("http://jumpbox.medtap.cn:8888"));
client.registerRequestType(QueryMyPatientsForDoctorRequest.class, QueryMyPatientsForDoctorResponse.class, new URI("http://api.iplusmed.com"));
client.registerRequestType(AddMultiMediasToJourneyRequest.class, AddMultiMediasToJourneyResponse.class, new URI("http://jumpbox.medtap.cn:8888"));
{
final QueryMyPatientsForDoctorRequest req = new QueryMyPatientsForDoctorRequest();
req.setDoctorId("8510");
((SignalClient) client).interaction().request(req).<QueryMyPatientsForDoctorResponse>build().subscribe(new Subscriber<QueryMyPatientsForDoctorResponse>() {
@Override
public void onCompleted() {
LOG.debug("FetchPatientsRequest: onCompleted.");
}
@Override
public void onError(Throwable e) {
LOG.debug("FetchPatientsRequest: onError: {}", ExceptionUtils.exception2detail(e));
}
@Override
public void onNext(final QueryMyPatientsForDoctorResponse response) {
LOG.debug("QueryMyPatientsForDoctorRequest: onNext: {}", response);
}
});
}
/*
final CountDownLatch latch = new CountDownLatch(1);
{
final FetchPatientsRequest req = new FetchPatientsRequest();
req.setAccountId("2");
client.defineInteraction(req)
.compose(RxNettys.<FetchPatientsResponse>filterProgress())
.subscribe(new Subscriber<FetchPatientsResponse>() {
@Override
public void onCompleted() {
latch.countDown();
LOG.debug("FetchPatientsRequest: onCompleted.");
}
@Override
public void onError(Throwable e) {
latch.countDown();
LOG.debug("FetchPatientsRequest: onError: {}", ExceptionUtils.exception2detail(e));
}
@Override
public void onNext(final FetchPatientsResponse response) {
LOG.debug("FetchPatientsRequest: onNext: {}", response);
}});
}
latch.await();
pool.awaitRecycleChannels();
*/
{
final HttpUtil.TrafficCounterFeature trafficCounter = HttpUtil.buildTrafficCounterFeature();
final HttpUtil.PayloadCounterFeature payloadCounter = HttpUtil.buildPayloadCounterFeature();
final AddMultiMediasToJourneyRequest req = new AddMultiMediasToJourneyRequest();
req.setCaseId("120");
req.setJourneyId("1");
final Subscription subscription = client.interaction().request(req).feature(// new Attachment("/Users/isdom/Desktop/997df3df73797e91dea4853c228fcbdee36ceb8a38cc8-1vxyhE_fw236.jpeg", "image/jpeg"))
ENABLE_LOGGING, trafficCounter, payloadCounter, new Attachment("/Users/isdom/Pictures/IMG_3492.JPG", "image/jpeg")).<AddMultiMediasToJourneyResponse>build().subscribe(new Subscriber<AddMultiMediasToJourneyResponse>() {
@Override
public void onCompleted() {
LOG.debug("AddMultiMediasToJourneyRequest: onCompleted.");
LOG.debug("traffic: upload: {}/download: {}", trafficCounter.outboundBytes(), trafficCounter.inboundBytes());
LOG.debug("payload: totalUpload: {}/totalDownload: {}", payloadCounter.totalUploadBytes(), payloadCounter.totalDownloadBytes());
}
@Override
public void onError(Throwable e) {
LOG.debug("AddMultiMediasToJourneyRequest: onError: {}", ExceptionUtils.exception2detail(e));
}
@Override
public void onNext(final AddMultiMediasToJourneyResponse resp) {
LOG.debug("AddMultiMediasToJourneyRequest: onNext: {}", resp);
}
});
// subscription.unsubscribe();
// TODO, why invoke onCompleted Event? not onError, check
// TO BE CONTINUE, 2015-05-13
LOG.debug("traffic: upload: {}/download: {}", trafficCounter.outboundBytes(), trafficCounter.inboundBytes());
LOG.debug("payload: totalUpload: {}/totalDownload: {}", payloadCounter.totalUploadBytes(), payloadCounter.totalDownloadBytes());
}
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project jocean-http by isdom.
the class Nettys4Test method createLocalConnection4Http.
public static Pair<Channel, Channel> createLocalConnection4Http(final String addr) throws Exception {
final BlockingQueue<Channel> serverChannels = new ArrayBlockingQueue<Channel>(1);
final Bootstrap clientbootstrap = new Bootstrap().group(Nettys4Test.EVENTLOOP4CLIENT).channel(LocalChannel.class).handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(final Channel ch) throws Exception {
Nettys.applyHandler(ch.pipeline(), HttpHandlers.LOGGING);
Nettys.applyHandler(ch.pipeline(), HttpHandlers.HTTPCLIENT);
}
}).remoteAddress(new LocalAddress(addr));
final Channel acceptorChannel = new ServerBootstrap().group(EVENTLOOP4BOSS, EVENTLOOP4SERVER).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(final Channel ch) throws Exception {
Nettys.applyHandler(ch.pipeline(), HttpHandlers.LOGGING);
Nettys.applyHandler(ch.pipeline(), HttpHandlers.HTTPSERVER);
serverChannels.offer(ch);
}
}).localAddress(new LocalAddress(addr)).bind().sync().channel();
try {
final Channel client = clientbootstrap.connect().sync().channel();
final Channel server = serverChannels.take();
return Pair.of(client, server);
} finally {
acceptorChannel.close().sync();
}
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project cdap by caskdata.
the class NettyRouterPipelineTest method testHttpPipelining.
@Test
public void testHttpPipelining() throws Exception {
final BlockingQueue<HttpResponseStatus> responseStatuses = new LinkedBlockingQueue<>();
EventLoopGroup eventGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(eventGroup).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("codec", new HttpClientCodec());
pipeline.addLast("aggregator", new HttpObjectAggregator(1048576));
pipeline.addLast("handler", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpResponse) {
responseStatuses.add(((HttpResponse) msg).status());
}
ReferenceCountUtil.release(msg);
}
});
}
});
// Create a connection and make five consecutive HTTP call without waiting for the first to respond
Channel channel = bootstrap.connect(HOSTNAME, ROUTER.getServiceMap().get(GATEWAY_NAME)).sync().channel();
for (int i = 0; i < 5; i++) {
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/v1/sleep?sleepMillis=3000");
request.headers().set(HttpHeaderNames.HOST, HOSTNAME);
channel.writeAndFlush(request);
}
// Should get the first response as normal one
HttpResponseStatus status = responseStatuses.poll(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpResponseStatus.OK, status);
// The rest four should be failure responses
for (int i = 0; i < 4; i++) {
Assert.assertEquals(HttpResponseStatus.NOT_IMPLEMENTED, responseStatuses.poll(1, TimeUnit.SECONDS));
}
eventGroup.shutdownGracefully();
channel.close();
Assert.assertTrue(responseStatuses.isEmpty());
}
Aggregations