Restlet 在处理完一个请求后停止服务器
Restlet stop server after exactly one request is handled
我想使用Restlet创建一个本地服务器。该服务器应该收到一个请求(除了图标),并且在处理该请求后它应该关闭。我不想使用 System.exit
,我希望它正常关闭。
正如您在我的代码中看到的那样,我在假设请求得到正确处理时注释了该行。但是我不能让服务器停在那里。
此时如何告诉服务器停止等待请求?
我成功了,但有 2 个问题我想解决。
- 当我在请求中停止服务器时出现异常
如果我在请求中停止服务器,发送给客户端的response
将不会显示
public class Main {
public static void main(String[] args){
Server serv = null;
Restlet restlet = new Restlet() {
@Override
public void handle(Request request, Response response) {
if(!request.toString().contains("favicon")){
System.out.println("do stuff");
response.setEntity("Request will be handled", MediaType.TEXT_PLAIN);
//stop server after the first request is handled.
//so the program should shut down here
//if I use serv.stop() here (besides it's currently not final)
//I'd get exceptions and the user wouldn't see the response
}
}
};
// Avoid conflicts with other Java containers listening on 8080!
try {
serv = new Server(Protocol.HTTP, 8182, restlet);
serv.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我想出了一个办法。在响应中添加一个 OnSent
事件,并在此处关闭服务器。
public class Main {
private Server serv;
public Main(){
run();
}
public void run(){
Restlet restlet = new Restlet() {
@Override
public void handle(Request request, Response response) {
response.setEntity("Request will be handled", MediaType.TEXT_PLAIN);
if(!request.toString().contains("favicon")){
System.out.println("do stuff");
response.setOnSent(new Uniform() {
@Override
public void handle(Request req, Response res) {
try {
serv.stop();//stop the server
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
};
// Avoid conflicts with other Java containers listening on 8080!
try {
serv = new Server(Protocol.HTTP, 8182, restlet);
serv.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
new Main();
}
}
我想使用Restlet创建一个本地服务器。该服务器应该收到一个请求(除了图标),并且在处理该请求后它应该关闭。我不想使用 System.exit
,我希望它正常关闭。
正如您在我的代码中看到的那样,我在假设请求得到正确处理时注释了该行。但是我不能让服务器停在那里。
此时如何告诉服务器停止等待请求? 我成功了,但有 2 个问题我想解决。
- 当我在请求中停止服务器时出现异常
如果我在请求中停止服务器,发送给客户端的
response
将不会显示public class Main { public static void main(String[] args){ Server serv = null; Restlet restlet = new Restlet() { @Override public void handle(Request request, Response response) { if(!request.toString().contains("favicon")){ System.out.println("do stuff"); response.setEntity("Request will be handled", MediaType.TEXT_PLAIN); //stop server after the first request is handled. //so the program should shut down here //if I use serv.stop() here (besides it's currently not final) //I'd get exceptions and the user wouldn't see the response } } }; // Avoid conflicts with other Java containers listening on 8080! try { serv = new Server(Protocol.HTTP, 8182, restlet); serv.start(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
我想出了一个办法。在响应中添加一个 OnSent
事件,并在此处关闭服务器。
public class Main {
private Server serv;
public Main(){
run();
}
public void run(){
Restlet restlet = new Restlet() {
@Override
public void handle(Request request, Response response) {
response.setEntity("Request will be handled", MediaType.TEXT_PLAIN);
if(!request.toString().contains("favicon")){
System.out.println("do stuff");
response.setOnSent(new Uniform() {
@Override
public void handle(Request req, Response res) {
try {
serv.stop();//stop the server
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
};
// Avoid conflicts with other Java containers listening on 8080!
try {
serv = new Server(Protocol.HTTP, 8182, restlet);
serv.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
new Main();
}
}