Today, I came across a more realistic demand for an advertising network, as follows:
The ad server typically receives a large number of requests to load ads from the publisher's app. At this point, the data from these requests can be sent to some intermediate exchanges, which then return the ad data. Because the volume of requests is high and the delay must be minimal, it's necessary to use a long connection when communicating with these intermediate services. Creating a new HTTP connection for each request is not feasible due to performance constraints.
In practice, the general approach to using Netty to develop a server is as follows:
(1) Create an eventLoopGroup to manage NIO I/O events.
(2) Create a NioSocketChannel and register it with the eventLoopGroup. At this stage, no channel initializer is set yet.
(3) Call the connect method on the channel to initiate a connection with the remote server.
(4) Once the connection is established, the previously mentioned initialization handler will be triggered. It adds HTTP decoding and encoding handlers to the channel.
(5) Finally, HTTP communication can begin.
Of course, the channel should not be disconnected by the client. Disconnection may occur due to the other server, timeout, or any other reason. The client itself won't actively close the connection.
As long as the above steps are followed, it's relatively straightforward to write code using Netty to implement an HTTP client based on a long connection. Here's the actual code:
[java] view plain copy
package 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 to initiate the connection.
channel.pipeline().addFirst(new InitHandler()); // Add an initialization handler to handle pending events.
group.register(channel); // Register the channel with the event loop group.
channel.connect(new InetSocketAddress("", 80)); // Initiate the connection.
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
}
}
Iskra Automobile Generator,skra Generator Production, Lada Alternator Catalogue, New Holland Alternator Catalogue
YIWU JINGHONG AUTO PARTS CO.,LTD , https://en.jhauto.ru