今天遇到了一個廣告網(wǎng)絡(luò)比較現(xiàn)實的需求,如下:
最為一個廣告服務(wù)端,可以從publisher的app接收到很多的加載廣告的請求。。。這個時候可以將這些請求的數(shù)據(jù)發(fā)給一些中間的機(jī)構(gòu)(exchange),然后由他們返回廣告的數(shù)據(jù)。。。因為請求量較大,而且要保證延遲不能太高,所以這里與這些中間機(jī)構(gòu)進(jìn)行通信的時候就只能采用長連接的方式了,不能每次請求都生成一次連接來進(jìn)行http請求。。。
其實以前一般用netty開發(fā)都是作為服務(wù)端來的大致思路如下:
(1)創(chuàng)建一個eventLoopGroup,用于維護(hù)nio的io事件
(2)創(chuàng)建一個niosocketchanel,然后將其注冊到eventLoopGroup上面去,并未channel設(shè)置初始化的handler
(3)調(diào)用channel的connect方法發(fā)起與遠(yuǎn)端的連接請求
(4)當(dāng)鏈接建立以后,剛剛提到的初始化handler將會用于響應(yīng),為channel添加http的decode與encode的handler。。
(5)最后就可以開始進(jìn)行http通信了。。
當(dāng)然這里就不要主動的去斷開channel了,斷開還是讓對方的服務(wù)器去做吧,或者超時,或者什么的。。反正客戶端不會主動斷開。。。
其實只要上面的步驟想出來,接下來寫代碼用netty來實現(xiàn)一個基于長連接的http客戶端還算是很簡單 的。。直接上代碼吧:
[java] view plain copypackage fjs;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.QueryStringEncoder;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
public class Fjs {
public static AtomicInteger number = new AtomicInteger(0);
public static AtomicLong time = new AtomicLong(0);
public static void doIt(Channel channel) {
if (number.get() 《 50) {
number.incrementAndGet();
time.set(System.currentTimeMillis());
QueryStringEncoder encoder = new QueryStringEncoder(“http://www.baidu.com/oapi/reqAd.jsp?pub=923875870&adspace=65826983&adcount=1&response=HTML&devip=22.56.22.66&user=900&format=IMG&position=top&height=&width=&device=Mozilla%2F5.0%20%28Linux%3B%20Android%204.2.1%3B%20en-us%3B%20Nexus%204%20Build%2FJOP40D%29%20AppleWebKit%2F535.19%20%28KHTML%2C%20like%20Gecko%29%20Chrome%2F18.0.1025.166%20Mobile%20Safari%2F535.19&beacon=TRUE&phpsnip=104”);
URI uriGet = null;
try {
uriGet = new URI(encoder.toString());
} catch (URISyntaxException e) {
System.out.println(“我擦,,,,”);
}
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriGet.toASCIIString());
channel.pipeline().write(request);
channel.flush();
} else {
System.out.println(“over”);
}
}
public static void main(String args[]) throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup();
NioSocketChannel channel = new NioSocketChannel(); //創(chuàng)建一個channel,待會用它來發(fā)起鏈接
channel.pipeline().addFirst(new InitHandler()); //為這個channel添加一個初始化的handler,用于響應(yīng)待會channel建立成功
group.register(channel); //注冊這個channel
channel.connect(new InetSocketAddress(“www.baidu.com”, 80)); //調(diào)用connect方法
Thread.currentThread().sleep(Long.MAX_VALUE);
}
public static class InitHandler implements ChannelInboundHandler {
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
public void channelRegistered(ChannelHandlerContext ctx)
throws Exception {
// TODO Auto-generated method stub
}
public void channelUnregistered(ChannelHandlerContext ctx)
throws Exception {
// TODO Auto-generated method stub
}
評論
查看更多