本文介紹了嵌入Tomcat 8的共享類加載器的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我已將Tomcat從7.0.34版升級(jí)到8.0.33版,從那以后,我一直面臨共享Web應(yīng)用程序上下文和Junit上下文的問題。
我有一個(gè)帶有Singleton類的Web應(yīng)用程序,它收集有關(guān)該Web應(yīng)用程序的統(tǒng)計(jì)數(shù)據(jù)。我還有在嵌入式Tomcat中運(yùn)行Web應(yīng)用程序的Junit。Junit查詢Web應(yīng)用程序,然后檢查統(tǒng)計(jì)數(shù)據(jù)。
我試著做一個(gè)簡(jiǎn)單的例子:
單件:
public class Counter {
private static Counter instance;
private AtomicLong counter;
private Counter(){}
public static Counter getInstance(){
if(instance == null){
synchronized (Counter.class) {
if(instance == null){
instance = new Counter();
}
}
}
return instance;
}
public long incrementAndGet(){
return counter.incrementAndGet();
}
public long getValue(){
return counter.get();
}
}
Servlet:
@WebServlet(name="servlet",loadOnStartup=1, urlPatterns="/servletTest")
public class Servlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("Hi, you are the #" + Counter.getInstance().incrementAndGet() + " visitor");
}
}
contextListener:
public class MyContextListener implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent arg0) {}
@Override
public void contextInitialized(ServletContextEvent arg0) {
Counter.getInstance().incrementAndGet();
}
}
測(cè)試單位:
public void mainTest() throws ServletException, LifecycleException{
Tomcat tomcat = new Tomcat();
tomcat.setPort(50000);
StandardContext ctx = (StandardContext) tomcat.addWebapp("/fe", System.getProperty("FEBaseDir")); //The FEBaseDir property is supposed to be taken from Maven build using 'test' profile
tomcat.start();
Counter.getInstance().getValue();
}
當(dāng)我使用Tomcat7時(shí),一切都運(yùn)行正常。但是自從我把Tomcat升級(jí)到Tomcat 8.0.33之后,它就不能工作了。包含靜態(tài)數(shù)據(jù)的單例類加載兩次。先是Tomcat,然后是Junit本身。
我已嘗試向Tomcat傳遞類加載器,但不起作用。
public void mainTest() throws ServletException, LifecycleException{
Tomcat tomcat = new Tomcat();
tomcat.setPort(50000);
StandardContext ctx = (StandardContext) tomcat.addWebapp("/fe", System.getProperty("FEBaseDir")); //The FEBaseDir property is supposed to be taken from Maven build using 'test' profile
ctx.setCrossContext(true);
ctx.setLoader((Loader) new WebappLoader(Thread.currentThread().getContextClassLoader()));
ctx.setParentClassLoader(Thread.currentThread().getContextClassLoader());
tomcat.getEngine().setParentClassLoader(Thread.currentThread().getContextClassLoader());
tomcat.getHost().setParentClassLoader(Thread.currentThread().getContextClassLoader());
tomcat.getService().setParentClassLoader(Thread.currentThread().getContextClassLoader());
tomcat.getServer().setParentClassLoader(Thread.currentThread().getContextClassLoader());
tomcat.start();
Counter.getInstance().getValue();
}
我做錯(cuò)了什么?
推薦答案
您可以嘗試使用StandardContext
中的setDelegate方法來阻止Web應(yīng)用程序類加載器重新加載Counter
類,但這會(huì)以一種不好的方式影響安全性,因此我建議不要這樣做。
公開統(tǒng)計(jì)信息的常用方法是使用JMX(MBean)。您可以通過使用值true
調(diào)用StandardContext
中的setUseNaming方法來啟用此功能。
您可以這樣注冊(cè)一個(gè)mBean(復(fù)制自here):
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName beanPoolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + poolName + ")");
mBeanServer.registerMBean(hikariPool, beanPoolName);
您可以檢索如下所示的值(從here復(fù)制):
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (foo)");
HikariPoolMXBean poolProxy = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class);
int idleConnections = poolProxy.getIdleConnections();
另請(qǐng)參閱this SO question,您可能需要閱讀更多文檔(根據(jù)我的經(jīng)驗(yàn),理解整個(gè)JMX并使其正常工作需要一些時(shí)間)。不過,我還沒有嘗試過將其與單元測(cè)試結(jié)合使用,所以YMMV。
這篇關(guān)于嵌入Tomcat 8的共享類加載器的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,