1

我正在尝试使用 restlet 2.0.11 运行服务器,但由于线程过多,服务器放弃了。有人可以通过示例帮助我增加服务器的线程数吗?

try
{
    Server server = new Server(Protocol.HTTP, m_iPort, ContentProvider.class);
    server.start();
}
catch (Exception e)
{
    e.printStackTrace();
}

我已经搜索了一个样本,但是

getContext().getParameters().add("maxThreads", "512"); 

给我空指针异常。

4

2 回答 2

1

Eric 的回答对于 Restlet 2.0.11 可能是正确的,但由于这仍然是 Restlet maxThreads 的顶级 Google 匹配,我想我应该发布一个更新。

我不知道这改变了哪个版本的 Restlet/Jetty,但 Restlet 2.3.7 不再支持 maxThreads 和 minThreads 设置。你现在必须打电话

server.getContext().getParameters().add("threadPool.maxThreads", "256");

据我所知,这可能是一个喜欢清理代码的贡献者在 Jetty 项目中进行的外观重构,而不考虑用户在升级他们的库时会遇到什么。此外,Jetty 似乎根本不会警告您无效参数。它只是默默地忽略它们。这正是您不希望看到的自由格式 String-String 参数。

于 2016-12-15T21:01:36.727 回答
0

maxThreads在 org.restlet.Server 的实例上设置更高的值,您可以在创建它时获取 org.restlet.Server 的实例作为它的返回值,然后获取它的 Context,获取参数列表并分配一个新属性 maxThreads一个整数,像这样:

import org.restlet.Component;
import org.restlet.Server;
//...
Server server = mycomponent.getServers().add(Protocol.HTTP, "localhost", 9090);
server.getContext().getParameters().add("maxThreads", "512");

看起来你的错误是你正在获取组件的上下文属性,或者 Java 程序本身,并且自然地在那里分配参数将被忽略。

更完整的例子:

http://www.programcreek.com/java-api-examples/index.php?api=org.restlet.data.Protocol

万一链接断开的整个程序:

package carpool.serverMain;

import java.util.ArrayList; 

import org.json.JSONObject; 
import org.restlet.Component; 
import org.restlet.Server; 
import org.restlet.data.Protocol; 

import carpool.common.DebugLog; 
import carpool.configurations.CarpoolConfig; 
import carpool.configurations.EnumConfig.Gender; 
import carpool.dbservice.LocationDaoService; 
import carpool.factory.JSONFactory; 
import carpool.service.*; 



public class ServerMain { 

 //private static Log log = LogFactory.getLog(ServiceMain.class); 

 private static ServerMain me; 

 private Component component; 

 public void init(String[] arguments) { 

 } 

 /**
  * Start the Thread, accept incoming connections 
  *  
  * Use this entry point to start with embedded HTTP Server 
  *  
  * @throws Exception 
  */ 
 public void start() throws Exception { 
  component = new Component(); 

  // Add a new HTTP server listening on port 

  Server server = component.getServers().add(Protocol.HTTP, 8015); 
  server.getContext().getParameters().add("maxThreads", "256"); 

  // Attach the sample application 
  RoutingService routingService = new RoutingService(); 

  component.getDefaultHost().attach(routingService); 

  // Start the component. 
  //log.info("ready to start"); 
  DebugLog.d("ready to start"); 
  component.start(); 

 } 

 /**
  * Stops RESTlet application 
  */ 
// public void stop() { 
//  component.getDefaultHost().detach(component.getApplication()); 
// } 

 public static ServerMain getInstance() { 
  if (me == null) { 
   me = new ServerMain(); 
  } 

  return me; 
 } 



 public static void main(String... args) throws Exception { 
  CarpoolConfig.initConfig(); 
  DebugLog.initializeLogger(); 
  LocationDaoService.init(); 
  DebugLog.d("Excuting"); 
  // Load server logic 
  try { 
   ServerMain.getInstance().init(args); 
   ServerMain.getInstance().start(); 
  } catch (Exception e) { 
   //log.error("Failed to start server", e); 
  } 
  Thread thread = new CleanThreadService(); 
  thread.start(); 
 } 

}
于 2016-09-12T22:00:28.003 回答