我已經爲我的webapp設置了一個DoSFilter,並已將其部署到Jetty中。事情很好。不過,我偶爾想更改過濾器配置。爲此,我總是必須更改web.xml並重新部署相當不方便的web應用程序。我會想動態地做到這一點。Jetty:如何使用JMX配置DoSFilter參數
我知道我可以配置使用JMX的QoSFilter和DoSFilter,因爲它們在Jetty中公開爲MBean。我在做同樣的事情時遇到麻煩。
到目前爲止,我已經能夠做到以下幾點:
- 配置碼頭啓用JMX和jmxremote。我成功地使用jconsole連接到jetty jvm。
- 在我的部署描述符中,我定義爲一個過濾器如下
<filter> <filter-name>DoSFilter</filter-name> <filter-class>org.eclipse.jetty.servlets.DoSFilter</filter-class> <init-param> <param-name>maxRequestsPerSec</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>requestPort</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>maxRequestMs</param-name> <param-value>600000</param-value> </init-param> <init-param> <param-name>excludePatterns</param-name> <param-value>/healthCheck</param-value> </init-param> <init-param> <param-name>managedAttr</param-name> <param-value>true</param-value> </init-param> </filter>
使得該過濾器添加到ServletContext中,從而露出它作爲一個MBean的managedAttr參數是設置爲true。
3我還添加了一個上下文PARAM要求碼頭,使DoSFilter作爲一個MBean如下
<context-param> <param-name>org.eclipse.jetty.server.context.ManagedAttributes</param-name> <param-value>DoSFilter</param-value> </context-param>
現在這個工作,但不是我想要的方式運行。當我打開Jconsole並轉到MBeans選項卡時,可以看到org.eclipse.jetty.servlets.DoSFilter下的DoSFilter。
但是,當我向下移動樹時,我只能看到該MBean的元數據信息。沒有屬性或操作附加到該MBean,我無法更改任何內容。
請指教。我在這裏做錯了什麼?
我的web應用程序是一個Spring應用程序並沒有定義
<servlet> <servlet-name>capture</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
感謝
UPDATE只有一個servlet的:我試着在嵌入式Jetty和配置的東西這似乎很好地工作。但是我仍然無法在獨立部署中使用它。
以下是我測試如何進行嵌入式服務器
public static void main(String[] args) throws Exception {
Server server = new Server();
Connector connector = new ServerConnector(server);
server.addConnector(connector);
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
MBeanContainer mbeanContainer = new MBeanContainer(mbeanServer);
server.addBean(mbeanContainer);
WebAppContext context = new WebAppContext(server,"/home/neo/ideaprojects/helloworld/src/main/webapp/", "/");
DoSFilter filter = new DoSFilter();
FilterHolder holder = new FilterHolder(filter);
String name = "DosFilter";
holder.setName(name);
holder.setInitParameter("managedAttr", "true");
context.setInitParameter(ServletContextHandler.MANAGED_ATTRIBUTES, name);
context.addFilter(holder, "/*", EnumSet.of(DispatcherType.FORWARD));
// ----------------------------------------
server.start();
server.join();
}
更新
I see the following debug logs in jetty standalone mode
2014-04-05 20:18:32.834:DBUG:oejj.MBeanContainer:main: beanAdded [email protected]{/,file:/tmp/jetty-0.0.0.0-8080-ROOT.war-_-any-6506776307143437350.dir/webapp/,STARTING}{/ROOT.war}->[email protected]
2014-04-05 20:18:32.835:DBUG:oejj.ObjectMBean:main: ObjectMbean: mbeanFor [email protected] mClass=class org.eclipse.jetty.jmx.ObjectMBean
2014-04-05 20:18:32.835:DBUG:oejj.ObjectMBean:main: mbeanFor [email protected] is [email protected]
2014-04-05 20:18:32.835:DBUG:oejj.ObjectMBean:main: No MBean Influence for DoSFilter
2014-04-05 20:18:32.835:DBUG:oejj.ObjectMBean:main: No MBean Influence for Object
2014-04-05 20:18:32.836:DBUG:oejj.ObjectMBean:main: No MBean Influence for Filter
2014-04-05 20:18:32.836:DBUG:oejj.ObjectMBean:main: Influence Count: 3
2014-04-05 20:18:32.836:DBUG:oejj.ObjectMBean:main: No @ManagedObject declared on class org.eclipse.jetty.servlets.DoSFilter
2014-04-05 20:18:32.836:DBUG:oejj.ObjectMBean:main: Influenced by: org.eclipse.jetty.servlets.DoSFilter
2014-04-05 20:18:32.836:DBUG:oejj.ObjectMBean:main: Annotations not found for: org.eclipse.jetty.servlets.DoSFilter
2014-04-05 20:18:32.836:DBUG:oejj.ObjectMBean:main: Influenced by: java.lang.Object
2014-04-05 20:18:32.836:DBUG:oejj.ObjectMBean:main: Annotations not found for: java.lang.Object
2014-04-05 20:18:32.836:DBUG:oejj.ObjectMBean:main: Influenced by: javax.servlet.Filter
2014-04-05 20:18:32.836:DBUG:oejj.ObjectMBean:main: Annotations not found for: javax.servlet.Filter
2014-04-05 20:18:32.836:DBUG:oejj.MBeanContainer:main: Registered org.eclipse.jetty.servlets:context=ROOT,type=dosfilter,id=0