NoClassDefFoundError 線上異沉酒總結(jié)

11-Jul-2019 15:45:10.758 SEVERE [catalina-exec-2] org.springframework.boot.web.servlet.support.ErrorPageFilter.forwardToErrorPage Forwarding to error page from request [/mjyx/search] due to exception [null] java.lang.ExceptionInInitializerError at 
...
xxx.xxx.xxx.Handler.invoke(XXXHandler.java:42) at 
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.IllegalArgumentException at java.util.concurrent.ThreadPoolExecutor.<init>(ThreadPoolExecutor.java:1307) at xxx.xxx.xxx.ThreadPoolConfig.<clinit>(ThreadPoolConfig.java:17) ... 73 more
org.springframework.boot.web.servlet.support.ErrorPageFilter.forwardToErrorPage Forwarding to error page from request [/aaa/bbb] due to exception [Could not initialize class xxx.xxx.xxx.ThreadPoolConfig] java.lang.NoClassDefFoundError: Could not initialize class xxx.xxx.xxx.ThreadPoolConfig at 

以上是線上異常報錯, tail -f第一感覺是類未找到谓传。代碼在線下測試通過蜈项,并在出現(xiàn)問題后,將線上jar 包download 到本地測試也正常良拼。
進一步找詳細報錯信息战得,ThreadPoolConfig的17行及ThreadPoolExecutor的1307行

  • ThreadPoolConfig 17行

public class ThreadPoolConfig {

 ...
    public static final ExecutorService POOL_EXECUTOR = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2, 20, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024), SEARCH_THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy());
  ...
  

ThreadPoolExecutor的1307行 JDK 代碼

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,  1307 行
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

從表現(xiàn)上來看可能是BlockingQueue 構(gòu)造問題

 public LinkedBlockingQueue(int capacity) {
        if (capacity <= 0) throw new IllegalArgumentException();
        this.capacity = capacity;
        last = head = new Node<E>(null);
    }

查看以上構(gòu)建方法capacity =1024 不可能報異常,進一步查看
ThreadPoolExecutor構(gòu)造參數(shù)庸推,有可能出問題的是

  if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();

查看

 public static final ExecutorService POOL_EXECUTOR = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2, 20, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024), SEARCH_THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy());

coreSize 取物理機核數(shù),有可能線上物理機核數(shù)與線下機器核數(shù)不一致造成浇冰。
修改coreSize 后結(jié)果正常贬媒。

/**
 * Thrown if the Java Virtual Machine or a <code>ClassLoader</code> instance
 * tries to load in the definition of a class (as part of a normal method call
 * or as part of creating a new instance using the <code>new</code> expression)
 * and no definition of the class could be found.
 * <p>
 * The searched-for class definition existed when the currently
 * executing class was compiled, but the definition can no longer be
 * found.
 *
 * @author  unascribed
 * @since   JDK1.0
 */
public
class NoClassDefFoundError

該類注釋發(fā)現(xiàn),該異常是虛擬機在或classloader 構(gòu)造類的運行時肘习,未找到該類的definition,而該類在編譯時是存在的际乘。
···
而異常ClassNotFoundException

/**
 * Thrown when an application tries to load in a class through its
 * string name using:
 * <ul>
 * <li>The <code>forName</code> method in class <code>Class</code>.
 * <li>The <code>findSystemClass</code> method in class
 *     <code>ClassLoader</code> .
 * <li>The <code>loadClass</code> method in class <code>ClassLoader</code>.
 * </ul>
 * <p>
 * but no definition for the class with the specified name could be found.
 *
 * <p>As of release 1.4, this exception has been retrofitted to conform to
 * the general purpose exception-chaining mechanism.  The "optional exception
 * that was raised while loading the class" that may be provided at
 * construction time and accessed via the {@link #getException()} method is
 * now known as the <i>cause</i>, and may be accessed via the {@link
 * Throwable#getCause()} method, as well as the aforementioned "legacy method."
 *
 * @author  unascribed
 * @see     java.lang.Class#forName(java.lang.String)
 * @see     java.lang.ClassLoader#findSystemClass(java.lang.String)
 * @see     java.lang.ClassLoader#loadClass(java.lang.String, boolean)
 * @since   JDK1.0
 */
public class ClassNotFoundException extends ReflectiveOperationException {
  

是該類在編譯期壓根就不存在,當運行時用class.forName時漂佩,找不到類報異常.
綜上兩點總結(jié)

  • NoClassDefFoundError與 ClassNotFoundException不同脖含,不要被該異常轉(zhuǎn)移注意力。
  • 注意線程池的coreSize不要大于maxSize,尤其在運行時投蝉。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末养葵,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子瘩缆,更是在濱河造成了極大的恐慌关拒,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件庸娱,死亡現(xiàn)場離奇詭異着绊,居然都是意外死亡,警方通過查閱死者的電腦和手機熟尉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門归露,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人斤儿,你說我怎么就攤上這事剧包∪迹” “怎么了?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵玄捕,是天一觀的道長踩蔚。 經(jīng)常有香客問我,道長枚粘,這世上最難降的妖魔是什么馅闽? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮馍迄,結(jié)果婚禮上福也,老公的妹妹穿的比我還像新娘。我一直安慰自己攀圈,他們只是感情好暴凑,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著赘来,像睡著了一般现喳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上犬辰,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天嗦篱,我揣著相機與錄音,去河邊找鬼幌缝。 笑死灸促,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼吮蛹,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了典鸡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤贴硫,失蹤者是張志新(化名)和其女友劉穎椿每,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體英遭,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡间护,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了挖诸。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片汁尺。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖多律,靈堂內(nèi)的尸體忽然破棺而出痴突,到底是詐尸還是另有隱情搂蜓,我是刑警寧澤,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布辽装,位于F島的核電站帮碰,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏拾积。R本人自食惡果不足惜殉挽,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望拓巧。 院中可真熱鬧斯碌,春花似錦、人聲如沸肛度。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽承耿。三九已至冠骄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瘩绒,已是汗流浹背猴抹。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留锁荔,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓蝙砌,卻偏偏與公主長得像阳堕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子择克,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359