]> git.basschouten.com Git - openhab-addons.git/blob
94ee4d27bfe0cd594427c579379bdb3684faf8d1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.transport.modbus.internal;
14
15 import org.apache.commons.pool2.impl.DefaultEvictionPolicy;
16 import org.apache.commons.pool2.impl.EvictionPolicy;
17 import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.io.transport.modbus.internal.pooling.ModbusSlaveConnectionEvictionPolicy;
21
22 import net.wimpi.modbus.net.ModbusSlaveConnection;
23
24 /**
25  * Configuration for Modbus connection pool
26  *
27  * Default is that
28  * - there is only one connection per endpoint
29  * - clients are served "fairly" (first-come-first-serve)
30  *
31  * @author Sami Salonen - Initial contribution
32  *
33  */
34 @NonNullByDefault
35 public class ModbusPoolConfig extends GenericKeyedObjectPoolConfig<ModbusSlaveConnection> {
36
37     @SuppressWarnings("unused")
38     private EvictionPolicy<ModbusSlaveConnection> evictionPolicy = new DefaultEvictionPolicy<>();
39
40     public ModbusPoolConfig() {
41         // When the pool is exhausted, multiple calling threads may be simultaneously blocked waiting for instances
42         // to
43         // become available. As of pool 1.5, a "fairness" algorithm has been implemented to ensure that threads
44         // receive
45         // available instances in request arrival order.
46         setFairness(true);
47
48         // Limit one connection per endpoint (i.e. same ip:port pair or same serial device).
49         // If there are multiple read/write requests to process at the same time, block until previous one finishes
50         setBlockWhenExhausted(true);
51         setMaxTotalPerKey(1);
52
53         // block infinitely when exhausted
54         setMaxWaitMillis(-1);
55
56         // Connections are "tested" on return. Effectively, disconnected connections are destroyed when returning on
57         // pool
58         // Note that we do not test on borrow -- that would mean blocking situation when connection cannot be
59         // established.
60         // Instead, borrowing connection from pool can return unconnected connection.
61         setTestOnReturn(true);
62
63         // disable JMX
64         setJmxEnabled(false);
65
66         // Evict idle connections every 10 seconds
67         setEvictionPolicy(new ModbusSlaveConnectionEvictionPolicy());
68         setTimeBetweenEvictionRunsMillis(10000);
69         // Let eviction re-create ready-to-use idle (=unconnected) connections
70         // This is to avoid occasional / rare deadlocks seen with pool 2.8.1 & 2.4.3 when
71         // borrow hangs (waiting indefinitely for idle object to appear in the pool)
72         // https://github.com/openhab/openhab-addons/issues/8460
73         setMinIdlePerKey(1);
74     }
75
76     @Override
77     public void setEvictionPolicyClassName(@Nullable String evictionPolicyClassName) {
78         // Protect against https://issues.apache.org/jira/browse/POOL-338
79         // Disallow re-setting eviction policy with class name. Only setEvictionPolicy allowed
80         throw new IllegalStateException("setEvictionPolicyClassName disallowed! Will fail in OSGI");
81     }
82 }