View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.commander;
21  
22  import org.waarp.common.database.data.AbstractDbData;
23  import org.waarp.common.logging.WaarpLogger;
24  import org.waarp.common.logging.WaarpLoggerFactory;
25  import org.waarp.openr66.context.ErrorCode;
26  
27  import java.util.concurrent.BlockingQueue;
28  import java.util.concurrent.RejectedExecutionHandler;
29  import java.util.concurrent.ThreadFactory;
30  import java.util.concurrent.ThreadPoolExecutor;
31  import java.util.concurrent.TimeUnit;
32  
33  /**
34   * Thread Pool Executor for ClientRunner
35   */
36  public class ThreadPoolRunnerExecutor extends ThreadPoolExecutor {
37    /**
38     * Internal Logger
39     */
40    private static final WaarpLogger logger =
41        WaarpLoggerFactory.getLogger(ThreadPoolRunnerExecutor.class);
42  
43    /**
44     * RejectedExecutionHandler for this ThreadPoolRunnerExecutor
45     */
46    private static class RunnerRejectedExecutionHandler
47        implements RejectedExecutionHandler {
48  
49      @Override
50      public final void rejectedExecution(final Runnable arg0,
51                                          final ThreadPoolExecutor arg1) {
52        if (arg0 instanceof ClientRunner) {
53          final ClientRunner runner = (ClientRunner) arg0;
54          runner.changeUpdatedInfo(AbstractDbData.UpdatedInfo.INERROR,
55                                   ErrorCode.Unknown, true);
56        } else {
57          logger.warn("Not ClientRunner: {}", arg0.getClass().getName());
58        }
59      }
60  
61    }
62  
63    /**
64     * @param corePoolSize
65     * @param maximumPoolSize
66     * @param keepAliveTime
67     * @param unit
68     * @param workQueue
69     */
70    public ThreadPoolRunnerExecutor(final int corePoolSize,
71                                    final int maximumPoolSize,
72                                    final long keepAliveTime, final TimeUnit unit,
73                                    final BlockingQueue<Runnable> workQueue) {
74      super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
75      setRejectedExecutionHandler(new RunnerRejectedExecutionHandler());
76    }
77  
78    /**
79     * @param corePoolSize
80     * @param maximumPoolSize
81     * @param keepAliveTime
82     * @param unit
83     * @param workQueue
84     * @param threadFactory
85     */
86    public ThreadPoolRunnerExecutor(final int corePoolSize,
87                                    final int maximumPoolSize,
88                                    final long keepAliveTime, final TimeUnit unit,
89                                    final BlockingQueue<Runnable> workQueue,
90                                    final ThreadFactory threadFactory) {
91      super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
92            threadFactory);
93      setRejectedExecutionHandler(new RunnerRejectedExecutionHandler());
94    }
95  
96    /**
97     * @param corePoolSize
98     * @param maximumPoolSize
99     * @param keepAliveTime
100    * @param unit
101    * @param workQueue
102    * @param handler
103    */
104   public ThreadPoolRunnerExecutor(final int corePoolSize,
105                                   final int maximumPoolSize,
106                                   final long keepAliveTime, final TimeUnit unit,
107                                   final BlockingQueue<Runnable> workQueue,
108                                   final RejectedExecutionHandler handler) {
109     super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
110           handler);
111     setRejectedExecutionHandler(handler);
112   }
113 
114   /**
115    * @param corePoolSize
116    * @param maximumPoolSize
117    * @param keepAliveTime
118    * @param unit
119    * @param workQueue
120    * @param threadFactory
121    * @param handler
122    */
123   public ThreadPoolRunnerExecutor(final int corePoolSize,
124                                   final int maximumPoolSize,
125                                   final long keepAliveTime, final TimeUnit unit,
126                                   final BlockingQueue<Runnable> workQueue,
127                                   final ThreadFactory threadFactory,
128                                   final RejectedExecutionHandler handler) {
129     super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
130           threadFactory, handler);
131     setRejectedExecutionHandler(handler);
132   }
133 
134 }