Com.sun.net.httpserver.HttpServer не работает при использовании специальной службы исполнителяJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Com.sun.net.httpserver.HttpServer не работает при использовании специальной службы исполнителя

Сообщение Anonymous »

Я сделал простейший com.sun.net.httpserver.HttpServer ниже. Чего я не понимаю, так это почему server.setExecutor(mainExecutorGood) работает (когда вы переходите на localhost:8080/test, вы получаете ответ), но если вы server.setExecutor(mainExecutorBad) не работает (когда вы переходите на localhost:8080 /test браузер зависает).
Полный класс приведен ниже.
package xxx;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.logging.log4j.LogManager;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class ServerTest {

@SuppressWarnings("restriction")
public static HttpHandler testHandler() {
return new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
// TODO Auto-generated method stub
try {
String rt = "Test OK";
byte response[] = rt.getBytes("UTF-8");
httpExchange.getResponseHeaders().add("Content-Type", "text/plain; charset=UTF-8");
httpExchange.sendResponseHeaders(200, response.length);
OutputStream out = httpExchange.getResponseBody();
out.write(response);
out.close();
} catch (IOException ucx) {
LogManager.getLogger().catching(ucx);
throw new RuntimeException(ucx);
}

}

};
}

@SuppressWarnings("restriction")
public static void main(String args[]) {
int port = 8080;

try {
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);

ThreadFactory tf = new ThreadFactory() {
final AtomicInteger id = new AtomicInteger();
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread();
int tid = id.getAndIncrement();
t.setName("http_server_worker_thread_" + tid);
return t;
}
};

Executor mainExecutorBad = Executors.newCachedThreadPool(tf);
Executor mainExecutorGood = Executors.newCachedThreadPool();
server.setExecutor(mainExecutorGood);

server.createContext("/test", testHandler());

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Thread.currentThread().setName("main_thread");
server.start();

} catch (Throwable tr) {
System.err.println("throwable problem");
}
}

}



Подробнее здесь: https://stackoverflow.com/questions/787 ... cutor-serv
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»