日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當前位置:首頁 > 智能硬件 > 人工智能AI
[導讀]   今天遇到了一個廣告網(wǎng)絡比較現(xiàn)實的需求,如下:   最為一個廣告服務端,可以從publisher的app接收到很多的加載廣告的請求。。。這個時候可以將這些請求的數(shù)據(jù)發(fā)給一些中間的機構(

  今天遇到了一個廣告網(wǎng)絡比較現(xiàn)實的需求,如下:

  最為一個廣告服務端,可以從publisher的app接收到很多的加載廣告的請求。。。這個時候可以將這些請求的數(shù)據(jù)發(fā)給一些中間的機構(exchange),然后由他們返回廣告的數(shù)據(jù)。。。因為請求量較大,而且要保證延遲不能太高,所以這里與這些中間機構進行通信的時候就只能采用長連接的方式了,不能每次請求都生成一次連接來進行http請求。。。

  其實以前一般用netty開發(fā)都是作為服務端來的大致思路如下:

  (1)創(chuàng)建一個eventLoopGroup,用于維護nio的io事件

 ?。?)創(chuàng)建一個niosocketchanel,然后將其注冊到eventLoopGroup上面去,并未channel設置初始化的handler

  (3)調(diào)用channel的connect方法發(fā)起與遠端的連接請求

 ?。?)當鏈接建立以后,剛剛提到的初始化handler將會用于響應,為channel添加http的decode與encode的handler。。

 ?。?)最后就可以開始進行http通信了。。

  當然這里就不要主動的去斷開channel了,斷開還是讓對方的服務器去做吧,或者超時,或者什么的。。反正客戶端不會主動斷開。。。

  其實只要上面的步驟想出來,接下來寫代碼用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,用于響應待會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

  }

本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關閉