Restlet服务器就像单线程一样

时间:2014-01-23 20:24:06

标签: java multithreading restlet thread-sleep

我已经将以下代码作为对restlet

的hello world测试
public static void main(String[] args) throws Exception {
    //Find a way to get these from the ARGS...
    Settings.setCurrent(new Settings());

    // Create a new Restlet component and add a HTTP server connector to it  

component.getServers().add(Protocol.HTTP, 8182);

    component.getContext().getParameters().add("maxThreads", "512");
component.getContext().getParameters().add("minThreads", "100");

component.getDefaultHost().attach("/findMissingPackages", Jeblet.class);

    // Now, let's start the component!  
// Note that the HTTP server connector is also automatically started.  
component.start();
}

@Get
public String toString() {
  try {
  Thread.sleep(10000);
  }
  catch(Exception ex) { }
  String settingString = "stuff";
  return settingString;
}

我遇到的问题是,如果我在chrome中打开两个选项卡并连续两次访问服务器,则需要20秒才能在第二个选项卡上获得响应。这两个标签应该花费10秒钟。

当我调试时我只有一个调度员。我怎么告诉restlet我想要多个线程?

1 个答案:

答案 0 :(得分:1)

打开新的浏览器选项卡(或窗口)与打开新连接不同。浏览器非常擅长重新使用已经打开的连接,延迟20秒就是证据。您可以通过打印服务器中的远程IP +端口来验证这一点,两个请求都是相同的。

在Firefox中,您可以通过按ctrl + F5强制建立新连接,Chrome可能具有类似的功能。但是你也可以写一个小的(多线程)客户端程序来执行get-request:它并不难写,当你需要测试/调试服务器的其他功能时会派上用场。