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.upgrade.util;
016    
017    import com.liferay.portal.kernel.concurrent.ThreadPoolExecutor;
018    import com.liferay.portal.kernel.dao.db.DB;
019    import com.liferay.portal.kernel.dao.db.DBManagerUtil;
020    import com.liferay.portal.kernel.executor.PortalExecutorManagerUtil;
021    import com.liferay.portal.kernel.util.LoggingTimer;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    import java.util.concurrent.Callable;
026    import java.util.concurrent.Future;
027    
028    /**
029     * @author Miguel Pastor
030     */
031    public class ParallelUpgradeSchemaUtil {
032    
033            public static void execute(String... sqlFileNames) throws Exception {
034                    ThreadPoolExecutor threadPoolExecutor =
035                            PortalExecutorManagerUtil.getPortalExecutor(
036                                    ParallelUpgradeSchemaUtil.class.getName());
037    
038                    List<Future<Void>> futures = new ArrayList<>(sqlFileNames.length);
039    
040                    try {
041                            for (String sqlFileName : sqlFileNames) {
042                                    futures.add(
043                                            threadPoolExecutor.submit(
044                                                    new CallableSQLExecutor(sqlFileName)));
045                            }
046    
047                            for (Future<Void> future : futures) {
048                                    future.get();
049                            }
050                    }
051                    finally {
052                            threadPoolExecutor.shutdown();
053                    }
054            }
055    
056            private static class CallableSQLExecutor implements Callable<Void> {
057    
058                    @Override
059                    public Void call() throws Exception {
060                            DB db = DBManagerUtil.getDB();
061    
062                            try (LoggingTimer loggingTimer = new LoggingTimer(_sqlFileName)) {
063                                    db.runSQLTemplate(_sqlFileName, false);
064                            }
065    
066                            return null;
067                    }
068    
069                    private CallableSQLExecutor(String sqlFileName) {
070                            _sqlFileName = sqlFileName;
071                    }
072    
073                    private final String _sqlFileName;
074    
075            }
076    
077    }