Long connection client based on Netty

Today, I came across a more realistic requirement for an advertising network. Here's what it looks like:

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 advertisement data. Due to the high volume of requests and the need to keep latency low, we must use a long connection when communicating with these intermediaries. It's not feasible to create a new connection for each HTTP request.

In fact, the general approach of using Netty to develop a server is as follows:

(1) Create an eventLoopGroup to manage the NIO I/O events.

(2) Create a NioSocketChannel and register it with the eventLoopGroup. No initial handler is set on the channel at this stage.

(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 is triggered, adding HTTP decoder and encoder handlers to the channel.

(5) Finally, HTTP communication can begin.

Of course, the channel should not be closed actively. Disconnection should either be handled by the other server, or due to a timeout, or any other reason. In this case, the client will never disconnect on its own.

In reality, once these steps are followed, it's quite straightforward to write code using Netty to implement an HTTP client that uses 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 and use it to initiate the connection.

channel.pipeline().addFirst(new InitHandler()); // Add an initialization handler for this channel to respond to the pending connection.

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

}

}

}

100W PERC Mono Solar Cell

PERC mono panels are fabricated using monocrystalline silicon wafers, which are known for their high purity and uniform crystal structure. This results in a higher efficiency compared to polycrystalline or thin-film technologies. The "passivated" aspect of PERC refers to the treatment of the cell's surfaces with specialized materials that reduce recombination losses, allowing electrons to flow more freely through the cell. This is achieved by applying an oxide layer on both the emitter and rear surface, improving the cell's light absorption and reducing the reflection of light.Unlike traditional cells where the front side is used for the emitter, PERC cells have the emitter on the rear and the contacts on the rear as well. This design minimizes shading effects caused by the front contacts, leading to improved efficiency under low-light conditions.

Key Features

1. Higher Durability: The multi-busbar design can decrease the risk of the cell micro-cracks and fingers broken.

2. High Power Density: High conversion efficiency and greater power output are achieved through lower series resistance and improved lighting.

3. PID Resistant: Tested in accordance to the standard IEC 62804, our PV modules have demonstrated resistance against PID (Potential Induced Degradation), which translates to security for your investment.

4. Bigger Cells with better performance: A slight increase of the size of our cells, Boosts the performance of the newest modules by six percent on average.

In summary, sunpower monocrystalline solar panels are a testament to the ongoing innovation in solar technology, offering a balance of high efficiency, durability, and cost-effectiveness that makes them a preferred choice for both commercial and residential solar energy projects worldwide.

Monocrystalline Solar Panels,Mono Solar Panel,100 Watt Monocrystalline Solar Panel,100W Monocrystalline Solar Panel

Ningbo Taiye Technology Co., Ltd. , https://www.tysolarpower.com

Posted on