Jetty内部只有一个线程池负责NIO事件,业务事件的执行,因此在做压力测试的时候,如何简单知道当前这个线程池运行状态对于系统调优来说很重要,其实Jetty主框架是配置代替编码的模式,因此你只需要配置一下,就可以获得Jetty内部任何数据。
先看看配置:
当前丢在了jetty-deploy.xml中,其实完全可以自己去写一个xml分离开来:
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Configure the deployment manager -->
<!-- -->
<!-- Sets up 2 monitored dir app providers that are configured -->
<!-- to behave in a similaraly to the legacy ContextDeployer -->
<!-- and WebAppDeployer from previous versions of Jetty. -->
<!-- =========================================================== -->
<Call name="addBean">
<Arg>
<New class="com.taobao.top.xbox.util.JettyThreadPoolInspector">
<Set name="server">
<Ref id="Server" />
</Set>
<Set name="interval">5</Set>
</New>
</Arg>
</Call>
......
</Configure>
看看下面这段简单的代码,就知道如何输出jetty内部的线程了:
/**
* @author fangweng
* @email fangweng@taobao.com
* @date 2011-6-22
*
*/
public class JettyThreadPoolInspector implements LifeCycle,Runnable{
private static final Log logger = LogFactory.getLog(JettyThreadPoolInspector.class);
Server server;
int interval = 10 * 60;//每隔多少时间输出队列信息
Thread innerThread;
boolean flag = true;
public int getInterval() {
return interval;
}
public void setInterval(int interval) {
this.interval = interval;
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public void run()
{
while(flag)
{
try
{
logger.warn(new StringBuilder().append("Jetty Thread Pool: ").
append(server.getThreadPool().toString()).toString());
Thread.sleep(interval * 1000);
}
catch(Exception ex)
{
logger.error(ex);
}
}
}
@Override
public void start() throws Exception {
innerThread = new Thread(this,"JettyThreadPoolInspector-thread");
innerThread.start();
}
@Override
public void stop() throws Exception {
this.flag = false;
innerThread.interrupt();
}
......
至此为止,其实想说明的就是Jetty内部透明很容易做到,随意可植入,如果不喜欢循环输出,都可以实现监听kill -3的模式来dump出关心的信息。