/
QpidJMSTransport.java
173 lines (140 loc) · 5.32 KB
/
QpidJMSTransport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Licensed to the Rhiot under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.rhiot.spec.transport;
import io.rhiot.spec.Transport;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.qpid.jms.JmsConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Transport that wraps the Qpid JMS client connection.
*/
public class QpidJMSTransport implements Transport, MessageListener {
private static final Logger LOG = LoggerFactory.getLogger(QpidJMSTransport.class);
private static final String QUEUE_PREFIX = "queue://";
private static final String TOPIC_PREFIX = "topic://";
private String clientId;
private String brokerURL;
private String username;
private String password;
private List<Listener> listeners = new ArrayList<Listener>();
private Connection connection;
private Session session;
private MessageProducer producer;
private Map<Destination, MessageConsumer> consumers = new HashMap<Destination, MessageConsumer>();
public QpidJMSTransport(String brokerURL, String clientId) {
this.brokerURL = brokerURL;
this.clientId = clientId;
}
@Override
public void connect() throws Exception {
ConnectionFactory cf = new JmsConnectionFactory(brokerURL);
if (username != null && !username.isEmpty()) {
connection = cf.createConnection(username, password);
} else {
connection = cf.createConnection();
}
connection.setClientID(clientId);
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(null);
}
@Override
public void disconnect() throws Exception {
if (connection != null) {
connection.close();
}
}
@Override
public void subscribe(String destination) throws Exception {
Destination target = convertDestination(destination);
if (!consumers.containsKey(target)) {
MessageConsumer consumer = session.createConsumer(target);
consumer.setMessageListener(this);
consumers.put(target, consumer);
}
}
@Override
public void publish(String destination, byte[] message) throws Exception {
Destination target = convertDestination(destination);
BytesMessage outbound = session.createBytesMessage();
if (message != null && message.length > 0) {
outbound.writeBytes(message);
}
producer.send(target, outbound);
}
@Override
public void addListener(Listener listener) {
listeners.add(listener);
}
private Destination convertDestination(String destination) throws JMSException {
Destination result = null;
if (destination.startsWith(QUEUE_PREFIX)) {
result = session.createQueue(destination.substring(QUEUE_PREFIX.length()));
} else if (destination.startsWith(TOPIC_PREFIX)) {
result = session.createTopic(destination.substring(TOPIC_PREFIX.length()));
} else {
result = session.createTopic(destination);
}
return result;
}
@Override
public void onMessage(Message message) {
try {
if (message instanceof BytesMessage) {
Destination source = message.getJMSDestination();
BytesMessage bytesMsg = (BytesMessage) message;
byte[] payload = new byte[(int) bytesMsg.getBodyLength()];
bytesMsg.readBytes(payload);
listeners.forEach(listener -> {
listener.onMessage(source.toString(), payload);
});
} else {
LOG.debug("Received message type we don't yet handle: {}", message);
}
// TODO - Handle other message types.
} catch (Exception ex) {
LOG.error("Error delivering incoming message to listeners: {}", ex.getMessage());
LOG.trace("Error detail", ex);
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}