Skip to content

Commit 29cd51f

Browse files
Merge pull request #857 from nisan-abeywickrama/5.3.x
[5.3.x] Replace hardcoded JMS connection pool configurations with TOML-based configuration support
2 parents 785e8f8 + b8daa24 commit 29cd51f

File tree

6 files changed

+51
-11
lines changed

6 files changed

+51
-11
lines changed

components/event-publisher/event-output-adapters/org.wso2.carbon.event.output.adapter.jms/src/main/java/org/wso2/carbon/event/output/adapter/jms/JMSEventAdapter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void testConnect() throws TestConnectionNotSupportedException {
114114
try {
115115
Hashtable<String, String> adaptorProperties = new Hashtable<String, String>();
116116
adaptorProperties.putAll(eventAdapterConfiguration.getStaticProperties());
117-
JMSConnectionFactory jmsConnectionFactory = new JMSConnectionFactory(adaptorProperties, eventAdapterConfiguration.getName(), adaptorProperties.get(JMSEventAdapterConstants.ADAPTER_JMS_DESTINATION), 1);
117+
JMSConnectionFactory jmsConnectionFactory = new JMSConnectionFactory(adaptorProperties, eventAdapterConfiguration.getName(), adaptorProperties.get(JMSEventAdapterConstants.ADAPTER_JMS_DESTINATION), 1, globalProperties);
118118
Connection connection = jmsConnectionFactory.createConnection();
119119
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
120120
connection.close();
@@ -199,7 +199,7 @@ private PublisherDetails initPublisher(
199199
maxConnections = JMSEventAdapterConstants.ADAPTER_MAX_THREAD_POOL_SIZE;
200200
}
201201

202-
JMSConnectionFactory jmsConnectionFactory = new JMSConnectionFactory(adapterProperties, outputEventAdaptorConfiguration.getName(), messageConfig.get(JMSEventAdapterConstants.ADAPTER_JMS_DESTINATION), maxConnections);
202+
JMSConnectionFactory jmsConnectionFactory = new JMSConnectionFactory(adapterProperties, outputEventAdaptorConfiguration.getName(), messageConfig.get(JMSEventAdapterConstants.ADAPTER_JMS_DESTINATION), maxConnections, globalProperties);
203203
JMSMessageSender jmsMessageSender = new JMSMessageSender(jmsConnectionFactory);
204204
publisherDetails = new PublisherDetails(jmsConnectionFactory, jmsMessageSender, messageConfig);
205205

components/event-publisher/event-output-adapters/org.wso2.carbon.event.output.adapter.jms/src/main/java/org/wso2/carbon/event/output/adapter/jms/internal/util/JMSConnectionFactory.java

+28-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import javax.naming.InitialContext;
2929
import javax.naming.NamingException;
3030
import java.util.Hashtable;
31+
import java.util.Map;
3132

3233
/**
3334
* Encapsulate a JMS Connection factory definition within an Axis2.xml
@@ -64,17 +65,20 @@ public class JMSConnectionFactory {
6465

6566
private int maxConnections;
6667

68+
private Map<String, String> jmsConnectionProperties;
69+
6770
private GenericObjectPool connectionPool;
6871

6972
private String destinationName;
7073

7174
/**
7275
* Digest a JMS CF definition from an axis2.xml 'Parameter' and construct
7376
*/
74-
public JMSConnectionFactory(Hashtable<String, String> parameters, String name, String destination, int maxConcurrentConnections) {
77+
public JMSConnectionFactory(Hashtable<String, String> parameters, String name, String destination, int maxConcurrentConnections, Map<String, String> jmsConnectionProperties) {
7578
this.parameters = parameters;
7679
this.name = name;
7780
this.destinationName = destination;
81+
this.jmsConnectionProperties = jmsConnectionProperties;
7882

7983
if (maxConcurrentConnections > 0) {
8084
this.maxConnections = maxConcurrentConnections;
@@ -100,13 +104,13 @@ public JMSConnectionFactory(Hashtable<String, String> parameters, String name, S
100104
// need to initialize
101105
private void createConnectionPool() {
102106
GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
103-
poolConfig.minEvictableIdleTimeMillis = 3000;
104-
poolConfig.maxWait = 3000;
107+
poolConfig.minEvictableIdleTimeMillis = (Long) parseJmsProperty(JMSEventAdapterConstants.ADAPTER_MIN_EVICTABLE_IDLE_TIME_NAME, 3000L);
108+
poolConfig.maxWait = (Long) parseJmsProperty(JMSEventAdapterConstants.ADAPTER_MAX_WAIT_NAME, 3000L);
105109
poolConfig.maxActive = maxConnections;
106110
poolConfig.maxIdle = maxConnections;
107-
poolConfig.minIdle = 0;
111+
poolConfig.minIdle = (Integer) parseJmsProperty(JMSEventAdapterConstants.ADAPTER_MIN_IDLE_NAME, 0);
108112
poolConfig.numTestsPerEvictionRun = Math.max(1, maxConnections / 10);
109-
poolConfig.timeBetweenEvictionRunsMillis = 5000;
113+
poolConfig.timeBetweenEvictionRunsMillis = (Long) parseJmsProperty(JMSEventAdapterConstants.ADAPTER_TIME_BETWEEN_EVICTION_RUNS_NAME, 5000L);
110114
this.connectionPool = new GenericObjectPool(new PoolableJMSConnectionFactory(), poolConfig);
111115

112116
}
@@ -289,6 +293,25 @@ public JMSPooledConnectionHolder getConnectionFromPool() {
289293
}
290294
}
291295

296+
/**
297+
* Parses a JMS property value and returns it as the specified type (Integer or Long).
298+
* If the value is empty, returns the provided default value.
299+
*
300+
* @param jmsPropertyKey the JMS property key as a String
301+
* @param defaultValue the default value to use if the value is empty
302+
* @return the parsed value as Integer or Long, based on the type of defaultValue
303+
*/
304+
private Number parseJmsProperty(String jmsPropertyKey, Number defaultValue) {
305+
String value = jmsConnectionProperties.get(jmsPropertyKey);
306+
if (value != null && !value.isEmpty()) {
307+
if (defaultValue instanceof Long) {
308+
return Long.parseLong(value);
309+
} else if (defaultValue instanceof Integer) {
310+
return Integer.parseInt(value);
311+
}
312+
}
313+
return defaultValue;
314+
}
292315

293316
public synchronized void close() {
294317

components/event-publisher/event-output-adapters/org.wso2.carbon.event.output.adapter.jms/src/main/java/org/wso2/carbon/event/output/adapter/jms/internal/util/JMSEventAdapterConstants.java

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public class JMSEventAdapterConstants {
4444
public static final String ADAPTER_MAX_THREAD_POOL_SIZE_NAME = "maxThread";
4545
public static final String ADAPTER_KEEP_ALIVE_TIME_NAME = "keepAliveTimeInMillis";
4646
public static final String ADAPTER_EXECUTOR_JOB_QUEUE_SIZE_NAME = "jobQueueSize";
47+
public static final String ADAPTER_MAX_WAIT_NAME = "maxWait";
48+
public static final String ADAPTER_MIN_IDLE_NAME = "minIdle";
49+
public static final String ADAPTER_MIN_EVICTABLE_IDLE_TIME_NAME = "minEvictableIdleTimeInMillis";
50+
public static final String ADAPTER_TIME_BETWEEN_EVICTION_RUNS_NAME = "timeBetweenEvictionRunsInMillis";
4751
public static final int ADAPTER_MIN_THREAD_POOL_SIZE = 8;
4852
public static final int ADAPTER_MAX_THREAD_POOL_SIZE = 100;
4953
public static final int ADAPTER_EXECUTOR_JOB_QUEUE_SIZE = 2000;

components/event-publisher/event-output-adapters/org.wso2.carbon.event.output.adapter.jms/src/test/java/org/wso2/carbon/event/output/adapter/jms/JmsPublisherTestCase.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class JmsPublisherTestCase {
3030
private OutputEventAdapterConfiguration eventAdapterConfiguration;
3131
private static final Log LOGGER = LogFactory.getLog(JmsPublisherTestCase.class);
3232
private static final Path testDir = Paths.get("src", "test", "resources");
33+
private Map<String, String> globalProperties;
3334

3435
private void setupCarbonConfig(String tenantName) {
3536
System.setProperty("carbon.home",
@@ -57,11 +58,15 @@ private JMSEventAdapter getJmsAdaptor() throws NamingException {
5758
//staticProperties.put("transport.LOGGER.UserName","admin");
5859
//staticProperties.put("transport.LOGGER.Password","admin");
5960
eventAdapterConfiguration.setStaticProperties(staticProperties);
60-
Map<String, String> globalProperties = new HashMap<>();
61+
globalProperties = new HashMap<>();
62+
globalProperties.put("minIdle", "0");
63+
globalProperties.put("timeBetweenEvictionRunsInMillis", "5000");
64+
globalProperties.put("maxWait", "3000");
65+
globalProperties.put("maxThread", "100");
6166
globalProperties.put("keepAliveTimeInMillis", "20000");
62-
globalProperties.put("minThread", "8");
67+
globalProperties.put("minEvictableIdleTimeInMillis", "3000");
6368
globalProperties.put("jobQueueSize", "10000");
64-
globalProperties.put("maxThread", "100");
69+
globalProperties.put("minThread", "8");
6570

6671
return new JMSEventAdapter(eventAdapterConfiguration, globalProperties);
6772
}
@@ -118,7 +123,7 @@ public void testConnectionFactory() throws Exception {
118123
adaptorProperties.putAll(eventAdapterConfiguration.getStaticProperties());
119124
JMSConnectionFactory jmsConnectionFactory = new JMSConnectionFactory(adaptorProperties,
120125
eventAdapterConfiguration.getName(), adaptorProperties.get(JMSEventAdapterConstants.ADAPTER_JMS_DESTINATION),
121-
1);
126+
1, globalProperties);
122127
jmsConnectionFactory.createConnection();
123128
jmsConnectionFactory.getConnectionFromPool();
124129
jmsConnectionFactory.getContext();

features/event-publisher/org.wso2.carbon.event.output.adapter.server.feature/src/main/resources/conf_templates/org.wso2.carbon.event.output.adapter.default.json

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"output_adapter.jms.max_thread": "100",
44
"output_adapter.jms.keep_alive_time": "20000ms",
55
"output_adapter.jms.job_queue_size": "10000",
6+
"output_adapter.jms.max_wait": "3000",
7+
"output_adapter.jms.min_idle": "0",
8+
"output_adapter.jms.min_evictable_idle_time": "3000",
9+
"output_adapter.jms.time_between_evictions": "5000",
610
"output_adapter.email.from_address": "[email protected]",
711
"output_adapter.email.username": "abcd",
812
"output_adapter.email.password": "xxxx",

features/event-publisher/org.wso2.carbon.event.output.adapter.server.feature/src/main/resources/conf_templates/templates/repository/conf/output-event-adapters.xml.j2

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
<property key="maxThread">{{output_adapter.jms.max_thread}}</property>
7272
<property key="keepAliveTimeInMillis">{{output_adapter.jms.keep_alive_time}}</property>
7373
<property key="jobQueueSize">{{output_adapter.jms.job_queue_size}}</property>
74+
<property key="maxWait">{{output_adapter.jms.max_wait}}</property>
75+
<property key="minIdle">{{output_adapter.jms.min_idle}}</property>
76+
<property key="minEvictableIdleTimeInMillis">{{output_adapter.jms.min_evictable_idle_time}}</property>
77+
<property key="timeBetweenEvictionRunsMillis">{{output_adapter.jms.time_between_evictions}}</property>
7478
</adapterConfig>
7579

7680
<adapterConfig type="mqtt">

0 commit comments

Comments
 (0)