我仍然在使用Netty 3.10。我写了一个单元测试来检查Netty boss线程的性能。我在单元测试主线程中使用一个简单的Netty服务器引导程序,并在缓存的线程池中生成100个Javasync-IO客户端。我注意到我认为奇怪的性能下降。每个客户端都打开一个套接字,写入数据并关闭,关闭后记录持续时间(毫秒)。我的单元测试是附加的。我的单元测试的典型输出是,按照给定的顺序:
因此,有 1 个客户端必须等待 6 秒钟才能获得打开的 TCP/IP 通道,11 个客户端必须等待 3 秒钟。我还检查了时间花在/丢失的地方。它始终是客户端的新套接字(字符串,整数)
。在服务器端,当管道工厂被触发时,时间已经一去不复返了。
我的单元测试的线程模型是这种情况的原因还是真正的Netty bootstrap/boss?
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
public class NettyServerBossTest {
private static final String SRV_HOST = "localhost";
private static final int SRV_PORT = 8080;
private static final byte[] MSG = "Hello world!".getBytes(Charset.forName("UTF-8"));
private static final int WAIT_MAX_MILLIS = 10 * 1000;
private final ChannelGroup channels = new DefaultChannelGroup();
private final int expected = 100;
private final AtomicInteger actual = new AtomicInteger();
private volatile boolean failed;
private ExecutorService clientThreads;
private Throwable cause;
private ServerBootstrap bootstrap;
@Test
public void test() {
createServer();
createClients();
awaitClients();
verifyFailure();
}
private void awaitClients() {
final long startMillis = System.currentTimeMillis();
final long maxMillis = startMillis + WAIT_MAX_MILLIS;
while ((this.actual.get() < this.expected) && !isFailed() && (System.currentTimeMillis() < maxMillis)) {
try {
Thread.sleep(250L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Total duration: " + (System.currentTimeMillis() - startMillis));
Assert.assertEquals(this.expected, this.actual.get());
}
private void createClients() {
this.clientThreads = Executors.newCachedThreadPool();
for (int i = 0; i < this.expected; i++) {
this.clientThreads.execute(new PlainSocketClient());
}
}
private void closeChannels() {
try {
this.channels.close().await(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void createServer() {
final ExecutorService bosses = Executors.newCachedThreadPool();
final ExecutorService workers = Executors.newCachedThreadPool();
final ChannelFactory factory = new NioServerSocketChannelFactory(bosses, workers);
this.bootstrap = new ServerBootstrap(factory);
this.bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() {
return Channels.pipeline(new DiscardServerHandler());
}
});
this.bootstrap.setOption("child.tcpNoDelay", Boolean.TRUE);
this.bootstrap.setOption("child.keepAlive", Boolean.TRUE);
this.bootstrap.bind(new InetSocketAddress(SRV_HOST, SRV_PORT));
}
/**
* Fail unit test
*
* @param cause
* cause of failure
*/
public synchronized void setCause(Throwable cause) {
if (!this.failed && (cause == null)) {
this.failed = true;
this.cause = cause;
}
}
@After
public void after() {
closeChannels();
if (this.clientThreads != null) {
this.clientThreads.shutdownNow();
}
if (this.bootstrap != null) {
this.bootstrap.releaseExternalResources();
}
}
/**
* Check if unit test has failed
*
* @return <code>true</code> if failed, <code>false</code> if still OK
*/
public boolean isFailed() {
return this.failed;
}
/**
* Get cause of failure
*
* @return cause or <code>null</code>
*/
public synchronized Throwable getCause() {
return this.cause;
}
/**
* Make sure test has not failed with exception
*/
public void verifyFailure() {
if (this.failed) {
throw new IllegalStateException("test failed", getCause());
}
}
public abstract class TestRunnable implements Runnable {
@Override
public final void run() {
try {
execute();
} catch (Exception e) {
handleException(e);
}
}
protected abstract void handleException(Throwable e);
protected abstract void execute() throws Exception;
}
public abstract class AsyncThreadsTestRunnable extends TestRunnable {
@Override
protected final void handleException(Throwable e) {
setCause(e);
}
}
public class PlainSocketClient extends AsyncThreadsTestRunnable {
@Override
protected void execute() throws Exception {
final long startMillis = System.currentTimeMillis();
try (Socket sock = new Socket(SRV_HOST, SRV_PORT)) {
sock.getOutputStream().write(MSG);
}
NettyServerBossTest.this.actual.incrementAndGet();
System.out.println("Client done. Duration: " + (System.currentTimeMillis() - startMillis));
}
}
public class DiscardServerHandler extends SimpleChannelHandler {
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
NettyServerBossTest.this.channels.add(e.getChannel());
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
// discard
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
e.getCause().printStackTrace();
Channel ch = e.getChannel();
ch.close();
}
}
}
我认为您记录的时间并不是全部花在打开的套接字上,而是花在线程切换上,因为当线程A打开套接字时,CPU可能会切换到线程B,然后当套接字打开完成时,CPU也许不会立即切换到线程A,而是在执行了许多其他线程之后。我已经更改了您的PlainSocketClient,添加了一个synchronized,以确保减少线程切换影响:
public class PlainSocketClient extends AsyncThreadsTestRunnable {
private static final String LOCK = "LOCK";
@Override
protected void execute() throws Exception {
synchronized (LOCK) {
final long startMillis = System.currentTimeMillis();
try (Socket sock = new Socket(SRV_HOST, SRV_PORT)) {
sock.getOutputStream().write(MSG);
}
NettyServerBossTest.this.actual.incrementAndGet();
System.out.println("Client done. Duration: " + (System.currentTimeMillis() - startMillis));
}
}
}
然后他们几乎只输出0或者1。你可以自己测试一下。这只是证明了线程切换的时间消耗,并不意味着你需要在你的代码中增加一个同步。
客户端通道在服务器入站通道激活方法中初始化。我可以看到pcap已经完成了三次握手(253是本地主机,15是远程主机) 但是当写入客户端通道时,它抛出 java.nio.channels。NotYetConnectedException:io.netty.channel.AbstractChannel$AbstractUnsafe处为null。flush0()(未知源) 根据我的理解,客户端通道应该
netty文档讨论TCP客户端。 但对UDP来说,这句话似乎无关紧要?!我找到了仅使用引导程序的UDP服务器的示例,如:1,2,3 我对此感到很困惑,所以我的两个问题是: null
我希望netty服务器A在启动时连接netty服务器B,听起来像代理,所以我尝试了netty代理示例,但它只是在中启动netty客户端,只有一个新的连接处于活动状态,客户端才会被创建。当服务器A启动时,我需要通知服务器B做一些“注册”的事情,我该怎么办?
目前,您可以使用SocketAddress引导Netty以创建通道工厂。为了与JBoss AS7进行紧密集成并使用其托管套接字绑定,我需要在Netty通道工厂中使用已创建的ServerSocket,无融资创业。这可能吗?我没有找到任何相关的API。 我正在努力解决https://community.jboss.org/thread/203485 如果没有,我可以要求将其作为一个功能吗?
我制作了一个FTP客户端(被动),它无法连接到服务器。我使用的FTP服务器是Filezilla;我只是用它来测试。每次我运行java程序(FTP客户端)时,Filezilla都会断开连接,并在Eclipse中出现以下错误: 这是FTP客户端: 这是我连接的程序: 还尝试编写我的lan ip而不是
当我运行时,我可以看到目录,它的内容就在其中: 谢了!