001    /**
002     * Copyright (c) 2000-present 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.fabric.agent;
016    
017    import com.liferay.portal.fabric.worker.FabricWorker;
018    import com.liferay.portal.kernel.concurrent.BaseFutureListener;
019    import com.liferay.portal.kernel.concurrent.NoticeableFuture;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.process.ClassPathUtil;
023    import com.liferay.portal.kernel.process.ProcessCallable;
024    import com.liferay.portal.kernel.process.ProcessException;
025    
026    import java.io.Serializable;
027    
028    import java.util.concurrent.Future;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class PortalClassPathWarmupFabricAgentListener
034            implements FabricAgentListener {
035    
036            @Override
037            public void registered(FabricAgent fabricAgent) {
038                    try {
039                            long startTime = System.currentTimeMillis();
040    
041                            FabricWorker<Serializable> fabricWorker = fabricAgent.execute(
042                                    ClassPathUtil.getPortalProcessConfig(), _warmupProcessCallable);
043    
044                            NoticeableFuture<Serializable> noticeableFuture =
045                                    fabricWorker.getProcessNoticeableFuture();
046    
047                            noticeableFuture.addFutureListener(
048                                    new FinishFutureListener(startTime));
049                    }
050                    catch (ProcessException pe) {
051                            _log.error(
052                                    "Unable to start portal class path warmup fabric worker", pe);
053                    }
054            }
055    
056            @Override
057            public void unregistered(FabricAgent fabricAgent) {
058            }
059    
060            protected class FinishFutureListener
061                    extends BaseFutureListener<Serializable> {
062    
063                    public FinishFutureListener(long startTime) {
064                            _startTime = startTime;
065                    }
066    
067                    @Override
068                    public void completeWithCancel(Future<Serializable> future) {
069                            _log.error("Portal class path warmup cancelled");
070                    }
071    
072                    @Override
073                    public void completeWithException(
074                            Future<Serializable> future, Throwable throwable) {
075    
076                            _log.error("Portal class path warmup failed", throwable);
077                    }
078    
079                    @Override
080                    public void completeWithResult(
081                            Future<Serializable> future, Serializable result) {
082    
083                            if (_log.isInfoEnabled()) {
084                                    _log.info(
085                                            "Portal class path warmup finished successfully in " +
086                                                    (System.currentTimeMillis() - _startTime) + "ms");
087                            }
088                    }
089    
090                    private final long _startTime;
091    
092            }
093    
094            private static final Log _log = LogFactoryUtil.getLog(
095                    PortalClassPathWarmupFabricAgentListener.class);
096    
097            private static final ProcessCallable<Serializable> _warmupProcessCallable =
098                    new ProcessCallable<Serializable>() {
099    
100                            @Override
101                            public String call() {
102                                    if (_log.isInfoEnabled()) {
103                                            _log.info("Portal class path warmup successful");
104                                    }
105    
106                                    return null;
107                            }
108    
109                            private static final long serialVersionUID = 1L;
110    
111                    };
112    
113    }