com.sun.HttpServer 套接字积压
套接字积压是什么意思?
例如,我在服务器上有 Web 服务
@WebService
public class Calculator {
public int sum(int a, int b) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return a + b;
}
public static final int threadCount = 10;
public static void main(String[] args) throws IOException {
Executor manyThreads = Executors.newFixedThreadPool(threadCount);
HttpServer server = HttpServer.create(new InetSocketAddress(8989), threadCount);
server.setExecutor(manyThreads);
server.start();
Endpoint ep = Endpoint.create(new Calculator());
HttpContext context = server.createContext("/calc");
ep.publish(context);
}
}
和 Web 服务客户端,
public static void main(String[] args) throws IOException {
CalculatorService service = new CalculatorService();
final Calculator calc = service.getCalculatorPort();
Executor executor = Executors.newFixedThreadPool(100);
for (int i = 0; i < 1000000; ++i) {
executor.execute(new Runnable() {
public void run() {
try {
int currentThreads = runningThreads.incrementAndGet();
int res = calc.sum(10, 10);
System.out.println("Running threads: " + currentThreads + " Result: " + res);
} catch (ClientTransportException e) {
System.out.println("connection refused");
} finally {
runningThreads.decrementAndGet();
}
}
});
}
System.in.read();
}
只有 10 个工作线程,并且积压设置为 10,因此可以有 10 个已接受的连接和 10 个正在积压中等待的连接,任何其他连接都将被拒绝,对吗?
可能不是,因为我没有收到 ClientTransportException。
您能给我解释一下连接发生了什么以及为什么没有 ClientTransportException 吗?
What does socket backlog mean?
for example I have web service
@WebService
public class Calculator {
public int sum(int a, int b) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return a + b;
}
public static final int threadCount = 10;
public static void main(String[] args) throws IOException {
Executor manyThreads = Executors.newFixedThreadPool(threadCount);
HttpServer server = HttpServer.create(new InetSocketAddress(8989), threadCount);
server.setExecutor(manyThreads);
server.start();
Endpoint ep = Endpoint.create(new Calculator());
HttpContext context = server.createContext("/calc");
ep.publish(context);
}
}
and webservice client
public static void main(String[] args) throws IOException {
CalculatorService service = new CalculatorService();
final Calculator calc = service.getCalculatorPort();
Executor executor = Executors.newFixedThreadPool(100);
for (int i = 0; i < 1000000; ++i) {
executor.execute(new Runnable() {
public void run() {
try {
int currentThreads = runningThreads.incrementAndGet();
int res = calc.sum(10, 10);
System.out.println("Running threads: " + currentThreads + " Result: " + res);
} catch (ClientTransportException e) {
System.out.println("connection refused");
} finally {
runningThreads.decrementAndGet();
}
}
});
}
System.in.read();
}
on the server only 10 working threads and backlog is set to 10, so there can be 10 accepted connections and 10 connections that are waiting in backlog and any other connection will be refused, am I right?
May be not as I don't get ClientTransportException.
Could you give me explanation what is happening with connections and why there is no ClientTransportException.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是一个连接队列,在您调用
accept()
来了解应用程序中的连接之前,这些连接已被 TCP 接受。将其设置为像 10 这样的低值是毫无意义的:TCP 会将其增加到自己的最小值,根据平台可能是 50 或 500,如果没有,它所做的只是导致毫无意义的 ConnectExceptions . 您最好将此值保留为默认值。It is a queue of connections that have already been accepted by TCP before you have called
accept()
to become aware of them in your application. Setting it to a low value like 10 is pretty pointless: TCP will increase that to its own minimum, which might be 50 or 500 depending on the platform, and if it didn't, all it accomplishes is to cause pointlessConnectExceptions.
You're better off leaving this value to default.