日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了嵌入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ì)大家有所幫助,

分享到:
標(biāo)簽:Tomcat 共享 加載 嵌入
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定