1 /*
2  * Copyright (C) 2013, 2014 Brett Wooldridge
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.zaxxer.hikari.pool;
18
19 import java.util.concurrent.ScheduledExecutorService;
20
21 /**
22  * A factory for {@link ProxyLeakTask} Runnables that are scheduled in the future to report leaks.
23  *
24  * @author Brett Wooldridge
25  * @author Andreas Brenk
26  */

27 class ProxyLeakTaskFactory
28 {
29    private ScheduledExecutorService executorService;
30    private long leakDetectionThreshold;
31
32    ProxyLeakTaskFactory(final long leakDetectionThreshold, final ScheduledExecutorService executorService)
33    {
34       this.executorService = executorService;
35       this.leakDetectionThreshold = leakDetectionThreshold;
36    }
37
38    ProxyLeakTask schedule(final PoolEntry poolEntry)
39    {
40       return (leakDetectionThreshold == 0) ? ProxyLeakTask.NO_LEAK : scheduleNewTask(poolEntry);
41    }
42
43    void updateLeakDetectionThreshold(final long leakDetectionThreshold)
44    {
45       this.leakDetectionThreshold = leakDetectionThreshold;
46    }
47
48    private ProxyLeakTask scheduleNewTask(PoolEntry poolEntry) {
49       ProxyLeakTask task = new ProxyLeakTask(poolEntry);
50       task.schedule(executorService, leakDetectionThreshold);
51
52       return task;
53    }
54 }
55