1 /*
2 * This file is part of AceLogger.
3 *
4 * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU
5 * Lesser General Public License as published by the Free Software Foundation, either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public License along with AceLogger.
13 * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
14 */
15 package net.sourceforge.acelogger.execution.manager;
16
17 import java.util.List;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.TimeUnit;
20
21 import net.sourceforge.acelogger.execution.LogController;
22
23 /**
24 * Abstracts all common tasks for {@link ExecutorManager} based on {@link ExecutorService}.
25 *
26 * @author Zardi (https://sourceforge.net/users/daniel_zardi)
27 * @version 1.0.0
28 * @since 1.0.0
29 */
30 public abstract class BaseUtilConcurrentExecutorManager extends BaseExecutorManager {
31
32 /**
33 * The service that will executor our tasks.
34 */
35 private ExecutorService executor;
36
37 /**
38 * Constructs a new BaseUtilConcurrentExecutorManager with the supplied identifier and executor
39 * service.
40 *
41 * @param identifier
42 * The string that identifies this executor manager.
43 * @param executor
44 * The {@link ExecutorService} that will be used to execute our tasks.
45 * @since 1.0.0
46 */
47 public BaseUtilConcurrentExecutorManager(String identifier, ExecutorService executor) {
48 super(identifier);
49 setExecutor(executor);
50 }
51
52 /**
53 * Sets the executor service that will be used to execute our tasks.
54 *
55 * @param executor
56 * The {@link ExecutorService} that will be used.
57 * @since 1.0.0
58 */
59 private void setExecutor(ExecutorService executor) {
60 this.executor = executor;
61 }
62
63 /** {@inheritDoc} */
64 public void execute(Runnable command) {
65 executor.execute(command);
66 }
67
68 /** {@inheritDoc} */
69 public boolean isTerminated() {
70 return executor.isTerminated();
71 }
72
73 /** {@inheritDoc} */
74 public boolean orderProperShutdown() {
75 executor.shutdown();
76 return executor.isShutdown();
77 }
78
79 /** {@inheritDoc} */
80 public boolean awaitTermination(long terminationTimeout) {
81 // TODO: should this check for a illegal state? this can be called only after shutdown
82 if (!executor.isTerminated()) {
83 // There are tasks on the queue, let's wait.
84 try {
85 executor.awaitTermination(terminationTimeout, TimeUnit.MILLISECONDS);
86 } catch (InterruptedException e) {
87 LogController.getInternalLogger().error(
88 "Interrupted while wainting for executor \"{0}\" shutdown", e,
89 getIdentifier());
90 }
91 }
92 return executor.isTerminated();
93 }
94
95 /** {@inheritDoc} */
96 public List<Runnable> terminateAndRetrieveTasks() {
97 // TODO: should this check for a illegal state? this can be called only after shutdown
98 return executor.shutdownNow();
99 }
100
101 }