001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.cache.ehcache;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ThreadUtil;
020    
021    /**
022     * @author Shuyang Zhou
023     */
024    public class ClearEhcacheThreadUtil {
025    
026            public static void clearEhcacheReplicationThread()
027                    throws InterruptedException {
028    
029                    Thread[] threads = ThreadUtil.getThreads();
030    
031                    for (Thread thread : threads) {
032                            if (thread == null) {
033                                    continue;
034                            }
035    
036                            String name = thread.getName();
037    
038                            if (name.equals(_THREAD_NAME)) {
039                                    thread.interrupt();
040    
041                                    thread.join(_WAIT_TIME);
042    
043                                    if (thread.isAlive() && _log.isWarnEnabled()) {
044                                            _log.warn(
045                                                    "Give up waiting on thread " + thread +
046                                                            " after waiting for " + _WAIT_TIME + "ms");
047                                    }
048                            }
049                    }
050            }
051    
052            private static final String _THREAD_NAME = "Replication Thread";
053    
054            private static final long _WAIT_TIME = 1000;
055    
056            private static Log _log = LogFactoryUtil.getLog(
057                    ClearEhcacheThreadUtil.class);
058    
059    }