Skip to content

Package: JmsAssistantProducerPool$DESTINATIONS

JmsAssistantProducerPool$DESTINATIONS

nameinstructionbranchcomplexitylinemethod
static {...}
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.broker.core.pool;
14:
15: import java.util.HashMap;
16: import java.util.Map;
17:
18: import org.apache.commons.pool2.impl.GenericObjectPool;
19: import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
20: import org.eclipse.kapua.broker.core.setting.BrokerSetting;
21: import org.eclipse.kapua.broker.core.setting.BrokerSettingKey;
22: import org.slf4j.Logger;
23: import org.slf4j.LoggerFactory;
24:
25: /**
26: * This class is needed by {@link org.eclipse.kapua.broker.core.plugin.KapuaSecurityBrokerFilter} to handle a vm connection.<BR>
27: * Indeed this bundle is instantiated during the broker startup then if {@link org.eclipse.kapua.broker.core.plugin.KapuaSecurityBrokerFilter} try to instantiate a connection receive an error from the
28: * broker. (the vm factory couldn't
29: * reach the broker)<BR>
30: * Then this class is needed to instantiate only a connection to be useful for the filter when it need
31: * ({@link org.eclipse.kapua.broker.core.plugin.KapuaSecurityBrokerFilter#addConnection(org.apache.activemq.broker.ConnectionContext, org.apache.activemq.command.ConnectionInfo) add connection} and
32: * {@link org.eclipse.kapua.broker.core.plugin.KapuaSecurityBrokerFilter#removeConnection(org.apache.activemq.broker.ConnectionContext, org.apache.activemq.command.ConnectionInfo, Throwable) remove
33: * connection}).<BR>
34: * <BR>
35: *
36: * <b>NOTE:<BR>
37: * with virtual topic support the destinations are removed! The message destination will be coded inside send method!
38: * </b>
39: *
40: * @since 1.0
41: */
42: public class JmsAssistantProducerPool extends GenericObjectPool<JmsAssistantProducerWrapper> {
43:
44: private static final Logger logger = LoggerFactory.getLogger(JmsAssistantProducerPool.class);
45:
46: public enum DESTINATIONS {
47: /**
48: * To be used to send messages without known destination.
49: * Otherwise the inactive monitor will not be able to remove the destination because it has a producer!
50: */
51: NO_DESTINATION
52: }
53:
54: private static Map<DESTINATIONS, JmsAssistantProducerPool> pools;
55:
56: static {
57: pools = new HashMap<JmsAssistantProducerPool.DESTINATIONS, JmsAssistantProducerPool>();
58: logger.info("Create pools (internal broker use)...");
59: logger.info("Create NoDestination pool...");
60: pools.put(DESTINATIONS.NO_DESTINATION,
61: new JmsAssistantProducerPool(new JmsAssistantProducerWrapperFactory(null)));
62: logger.info("Create pools (internal broker use)... done.");
63: }
64:
65: /**
66: * Create a JmsAssistantProducerPool from the given factory
67: *
68: * @param factory
69: */
70: protected JmsAssistantProducerPool(JmsAssistantProducerWrapperFactory factory) {
71: super(factory);
72: int totalMaxSize = BrokerSetting.getInstance().getInt(BrokerSettingKey.BROKER_CLIENT_POOL_NO_DEST_TOTAL_MAX_SIZE, 10);
73: int maxSize = BrokerSetting.getInstance().getInt(BrokerSettingKey.BROKER_CLIENT_POOL_NO_DEST_MAX_SIZE, 10);
74: int minSize = BrokerSetting.getInstance().getInt(BrokerSettingKey.BROKER_CLIENT_POOL_NO_DEST_MIN_SIZE, 5);
75:
76: GenericObjectPoolConfig jmsPoolConfig = new GenericObjectPoolConfig();
77: jmsPoolConfig.setMaxTotal(totalMaxSize);
78: jmsPoolConfig.setMaxIdle(maxSize);
79: jmsPoolConfig.setMinIdle(minSize);
80: logger.info("Set test on return to true for JmsAssistantProducerPool");
81: jmsPoolConfig.setTestOnReturn(true);
82: logger.info("Set test on borrow to true for JmsAssistantProducerPool");
83: jmsPoolConfig.setTestOnBorrow(true);
84: logger.info("Set block when exausted to true for JmsAssistantProducerPool");
85: jmsPoolConfig.setBlockWhenExhausted(true);
86:
87: setConfig(jmsPoolConfig);
88: }
89:
90: /**
91: * Return a JmsAssistantProducerPool for the given destination
92: *
93: * @param destination
94: * @return
95: */
96: public static JmsAssistantProducerPool getIOnstance(DESTINATIONS destination) {
97: return pools.get(destination);
98: }
99:
100: /**
101: * Close all connection pools
102: */
103: public static void closePools() {
104: if (pools != null) {
105: logger.info("Close NoDestination pool...");
106: pools.get(DESTINATIONS.NO_DESTINATION).close();
107: logger.info("Close pools... done.");
108: } else {
109: logger.warn("Cannot close producer pools... Pools not initialized!");
110: }
111: }
112:
113: }