Today I encountered a more realistic demand for an advertising network, as follows:
The most ad server can receive a lot of requests to load ads from the publisher's app. . . At this time, the data of these requests can be sent to some intermediate exchanges, and then they return the data of the advertisement. . . Because the amount of requests is large, and the delay must not be too high, you can only use the long connection method when communicating with these intermediate organizations. You cannot generate a connection for each request to make an http request. . .
In fact, the general idea of ​​using netty development as a server is as follows:
(1) Create an eventLoopGroup to maintain the io event of nio
(2) create a niosocketchanel, and then register it to the eventLoopGroup, there is no channel to set the initialized handler
(3) call the connect method of the channel to initiate a connection request with the remote
(4) When the link is established, the initialization handler just mentioned will be used for the response, adding the http decode and encode handler for the channel. .
(5) Finally, you can start http communication. .
Of course, don't take the initiative to disconnect the channel, disconnect or let the other server do it, or timeout, or whatever. . Anyway, the client will not actively disconnect. . .
In fact, as long as the above steps are coming up, it is quite simple to write code using netty to implement a http client based on long connection. . Directly on the code:
[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://Ad.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("I wipe,,,,");
}
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(); //Create a channel and use it to initiate the link.
Channel.pipeline().addFirst(new InitHandler()); //Add an initialized handler for this channel to respond to the pending channel creation.
Group.register(channel); //Register this channel
Channel.connect(new InetSocketAddress("", 80)); //call the connect method
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
}
This Automation curtain is specially designed for automation industry. SDKELI LSC2 light curtain is designed for automation field, with small size, compact structure and strong anti-interference ability, and the product meets IEC 61496-2 standards. The automatic light curtain is with reliable quality and very competitive price. It has been used in many factories and has replaced curtains from Sick, Omron, Banner, Keyence, etc.
Automatic Light Curtain,Laser Light Curtain,Automation Light Beam Sensor,Automatic Infrared Beam Sensor,Infrared Beam Curttain Sensor,Infrared Beam Sensor
Jining KeLi Photoelectronic Industrial Co.,Ltd , https://www.sdkelien.com