Skip to content

Package: JmsConnectionFactory

JmsConnectionFactory

nameinstructionbranchcomplexitylinemethod
static {...}
M: 57 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 14 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 org.apache.activemq.ActiveMQConnectionFactory;
16: import org.eclipse.kapua.broker.core.setting.BrokerSetting;
17: import org.eclipse.kapua.broker.core.setting.BrokerSettingKey;
18: import org.slf4j.Logger;
19: import org.slf4j.LoggerFactory;
20:
21: /**
22: * Provide connection factory for embedded broker connection.
23: *
24: * @since 1.0
25: */
26: public class JmsConnectionFactory {
27:
28: private JmsConnectionFactory() {
29: }
30:
31: private static final Logger logger = LoggerFactory.getLogger(JmsConnectionFactory.class);
32:
33: /**
34: * ActiveMQ vm connection factory instance
35: */
36: public static final ActiveMQConnectionFactory VM_CONN_FACTORY;
37:
38: // the workers used the same string connection without asynch=true
39: static {
40: logger.info("Instantiate amq embedded connection factory...");
41:
42: // By default, the embedded broker operates in asynchronous mode, so that calls to a send method return immediately (in other words, messages are dispatched to consumers in a separate
43: // thread). If you turn off asynchronous mode, however, you can reduce the amount of context switching. For example, you can disable asynchronous mode on a VM endpoint as follows:
44: // https://access.redhat.com/documentation/en-US/Fuse_ESB_Enterprise/7.1/html/ActiveMQ_Tuning_Guide/files/GenTuning-Colocate.html
45: // TODO parameter to be added to configuration
46: // connectionFactoryUri = "vm://" + KapuaEnvironmentConfig.getInstance().getString(KapuaEnvironmentConfigKeys.BROKER_NAME) + "?create=false&waitForStart=3600000&async=true";
47: String connectionFactoryUri = String.format("vm://%s?create=false&waitForStart=3600000&async=true", BrokerSetting.getInstance().getString(BrokerSettingKey.BROKER_NAME));
48: logger.info("Using connection factory url: " + connectionFactoryUri);
49:
50: // connection factory
51: VM_CONN_FACTORY = new ActiveMQConnectionFactory(connectionFactoryUri);
52:
53: // In this case, it is possible to optimize away the session threading layer and the MessageConsumer threads can then pull messages directly from the transport layer.
54: // https://access.redhat.com/documentation/en-US/Fuse_ESB_Enterprise/7.1/html/ActiveMQ_Tuning_Guide/files/GenTuning-Consumer-ContextSwitch.html
55: VM_CONN_FACTORY.setAlwaysSessionAsync(false);
56:
57: // When true a MessageProducer will always use Sync sends when sending a Message even if it is not required for the Delivery Mode
58: // http://activemq.apache.org/connection-configuration-uri.html
59: VM_CONN_FACTORY.setAlwaysSyncSend(false);
60:
61: // Should a JMS message be copied to a new JMS Message object as part of the send() method in JMS. This is enabled by default to be compliant with the JMS specification. You can disable it
62: // if you do not mutate JMS messages after they are sent for a performance boost.
63: // http://activemq.apache.org/connection-configuration-uri.html
64: VM_CONN_FACTORY.setCopyMessageOnSend(false);
65:
66: // If you are sure that your consumers are always fast, however, you could achieve better performance by disabling asynchronous dispatch on the broker (thereby avoiding the cost of
67: // unnecessary context switching).
68: // https://access.redhat.com/documentation/en-US/Fuse_ESB_Enterprise/7.1/html/ActiveMQ_Tuning_Guide/files/GenTuning-Consumer-ContextSwitch.html
69: VM_CONN_FACTORY.setDispatchAsync(false);
70:
71: // Enables an optimised acknowledgement mode where messages are acknowledged in batches rather than individually. Alternatively, you could use Session.DUPS_OK_ACKNOWLEDGE acknowledgement
72: // mode for the consumers which can often be faster. WARNING enabling this issue could cause some issues with auto-acknowledgement on reconnection
73: // http://activemq.apache.org/connection-configuration-uri.html
74: VM_CONN_FACTORY.setOptimizeAcknowledge(false);
75:
76: // If this flag is set then an larger prefetch limit is used - only applicable for durable topic subscribers
77: // http://activemq.apache.org/connection-configuration-uri.html
78: VM_CONN_FACTORY.setOptimizedMessageDispatch(true);
79:
80: // Forces the use of Async Sends which adds a massive performance boost; but means that the send() method will return immediately whether the message has been sent or not which could lead
81: // to message loss.
82: // http://activemq.apache.org/connection-configuration-uri.html
83: VM_CONN_FACTORY.setUseAsyncSend(true);
84:
85: logger.info("Instantiate activemq embedded connection factory... done");
86: }
87:
88: }