Skip to content

Package: JmsConsumerWrapper

JmsConsumerWrapper

nameinstructionbranchcomplexitylinemethod
JmsConsumerWrapper(String, boolean, boolean, MessageListener)
M: 50 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
close()
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
finalize()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 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 javax.jms.Connection;
16: import javax.jms.JMSException;
17: import javax.jms.MessageConsumer;
18: import javax.jms.MessageListener;
19: import javax.jms.Session;
20:
21: import org.apache.commons.lang3.StringUtils;
22: import org.eclipse.kapua.KapuaErrorCodes;
23: import org.eclipse.kapua.KapuaException;
24: import org.slf4j.Logger;
25: import org.slf4j.LoggerFactory;
26:
27: /**
28: * Jms consumer wrapper.<BR>
29: * This class wrap a single session per connection and manage the close operation of connection on session close.<BR>
30: * The connection is taken from a connection pool ({@link org.apache.activemq.ActiveMQConnectionFactory})
31: *
32: * @since 1.0
33: */
34: public class JmsConsumerWrapper {
35:
36: private static final Logger logger = LoggerFactory.getLogger(JmsConsumerWrapper.class);
37:
38: protected String destination;
39: protected Connection connection;
40: protected Session session;
41: protected MessageConsumer consumer;
42:
43: /**
44: *
45: * @param destination
46: * @param transacted
47: * @param start
48: * start activeMQ connection
49: * @throws JMSException
50: * @throws KapuaException
51: */
52: public JmsConsumerWrapper(String destination, boolean transacted, boolean start, MessageListener messageListener) throws JMSException, KapuaException {
53:• if (StringUtils.isEmpty(destination)) {
54: throw new KapuaException(KapuaErrorCodes.INTERNAL_ERROR, "Invalid destination (empty)!");
55: }
56: connection = JmsConnectionFactory.VM_CONN_FACTORY.createConnection();
57:• if (start == true) {
58: connection.start();
59: }
60: session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
61: consumer = session.createConsumer(session.createTopic(destination));
62: consumer.setMessageListener(messageListener);
63: this.destination = destination;
64: }
65:
66: public void close() {
67: try {
68: connection.close();
69: } catch (JMSException e) {
70: logger.error("Exception on connection close {}", e.getMessage(), e);
71: }
72: }
73:
74: @Override
75: protected void finalize() throws Throwable {
76: close();
77: super.finalize();
78: }
79:
80: }