This commit is contained in:
jinyu 2015-12-31 15:07:43 +08:00
parent c8cb22b69a
commit e043cd90a7
3 changed files with 39 additions and 29 deletions

View File

@ -16,6 +16,7 @@ import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.SslHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
@ -25,6 +26,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLEngine;
@ -91,6 +93,12 @@ public class Netty4HttpClient extends AbstractHttpClient {
sslEngine.setUseClientMode(true); sslEngine.setUseClientMode(true);
channel.pipeline().addFirst( channel.pipeline().addFirst(
new SslHandler(sslEngine)); new SslHandler(sslEngine));
if (params != null && params.getReadTimeout() > 0) {
channel.pipeline().addFirst(
new ReadTimeoutHandler(params
.getReadTimeout(),
TimeUnit.MILLISECONDS));
}
} }
channel.pipeline().addLast(new RequestHandler(future)); channel.pipeline().addLast(new RequestHandler(future));
channel.writeAndFlush(uriRequest); channel.writeAndFlush(uriRequest);

View File

@ -39,26 +39,30 @@ public class Netty4HttpClientFactory extends HttpClientFactory {
this.workerThreads = workerThreads; this.workerThreads = workerThreads;
} }
private volatile Bootstrap bootstrap;
@Override @Override
public HttpClient newInstance() { public HttpClient newInstance() {
Bootstrap b = new Bootstrap(); if (bootstrap == null) {
b.option(ChannelOption.SO_KEEPALIVE, true).option( bootstrap = new Bootstrap();
ChannelOption.TCP_NODELAY, true); bootstrap.option(ChannelOption.SO_KEEPALIVE, true).option(
EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads); ChannelOption.TCP_NODELAY, true);
b.group(workerGroup).channel(NioSocketChannel.class) EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads);
.handler(new ChannelInitializer<SocketChannel>() { bootstrap.group(workerGroup).channel(NioSocketChannel.class)
@Override .handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel channel) @Override
throws Exception { protected void initChannel(SocketChannel channel)
ChannelPipeline pipeline = channel.pipeline(); throws Exception {
pipeline.addLast(new HttpClientCodec()); ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new HttpContentDecompressor()); pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new ChunkedWriteHandler()); pipeline.addLast(new HttpContentDecompressor());
pipeline.addLast(new HttpResponseDecoder()); pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(new HttpObjectAggregator( pipeline.addLast(new HttpResponseDecoder());
Integer.MAX_VALUE)); pipeline.addLast(new HttpObjectAggregator(
} Integer.MAX_VALUE));
}); }
return new Netty4HttpClient(b); });
}
return new Netty4HttpClient(bootstrap);
} }
} }

View File

@ -43,15 +43,13 @@ public class MenuTest extends TokenTest {
buttons.add(new Button("个人中心", domain + "/user", ButtonType.view)); buttons.add(new Button("个人中心", domain + "/user", ButtonType.view));
Button button = new Button("小哥介绍", domain, ButtonType.view); Button button = new Button("小哥介绍", domain, ButtonType.view);
button.pushSub(new Button( button.pushSub(new Button("小哥介绍", "http://x.eqxiu.com/s/89oy462U",
"小哥介绍",
"http://x.eqxiu.com/s/89oy462U",
ButtonType.view)); ButtonType.view));
button.pushSub(new Button( button.pushSub(new Button("小哥官网", "http://www.jdxiaoge.com",
"小哥官网", ButtonType.view));
"http://www.jdxiaoge.com", button.pushSub(new Button("兴趣部落",
"http://buluo.qq.com/p/barindex.html?from=share&bid=282651",
ButtonType.view)); ButtonType.view));
button.pushSub(new Button("兴趣部落", "http://buluo.qq.com/p/barindex.html?from=share&bid=282651", ButtonType.view));
button.pushSub(new Button("服务流程", "FLOW", ButtonType.click)); button.pushSub(new Button("服务流程", "FLOW", ButtonType.click));
button.pushSub(new Button("在线客服", "KF", ButtonType.click)); button.pushSub(new Button("在线客服", "KF", ButtonType.click));
buttons.add(button); buttons.add(button);
@ -107,15 +105,15 @@ public class MenuTest extends TokenTest {
JsonResult result = menuApi.createCustomMenu(buttons, matchRule); JsonResult result = menuApi.createCustomMenu(buttons, matchRule);
Assert.assertEquals(0, result.getCode()); Assert.assertEquals(0, result.getCode());
} }
@Test @Test
public void testGetAllMenus() throws WeixinException { public void testGetAllMenus() throws WeixinException {
List<Menu> menus = menuApi.getAllMenu(); List<Menu> menus = menuApi.getAllMenu();
System.err.println(menus); System.err.println(menus);
} }
@Test @Test
public void testMatchMenu()throws WeixinException{ public void testMatchMenu() throws WeixinException {
List<Button> buttons = menuApi.matchCustomMenu("paihuaing"); List<Button> buttons = menuApi.matchCustomMenu("paihuaing");
System.err.println(buttons); System.err.println(buttons);
} }