-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
JMSPublisher.java
204 lines (186 loc) · 9.39 KB
/
JMSPublisher.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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 org.apache.nifi.jms.processors;
import org.apache.nifi.logging.ComponentLog;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.SessionCallback;
import org.springframework.jms.support.JmsHeaders;
import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* Generic publisher of messages to JMS compliant messaging system.
*/
final class JMSPublisher extends JMSWorker {
JMSPublisher(CachingConnectionFactory connectionFactory, JmsTemplate jmsTemplate, ComponentLog processLog) {
super(connectionFactory, jmsTemplate, processLog);
processLog.debug("Created Message Publisher for {}", new Object[] {jmsTemplate});
}
void publish(String destinationName, byte[] messageBytes) {
this.publish(destinationName, messageBytes, null);
}
void publish(final String destinationName, final byte[] messageBytes, final Map<String, String> flowFileAttributes) {
this.jmsTemplate.send(destinationName, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
BytesMessage message = session.createBytesMessage();
message.writeBytes(messageBytes);
setMessageHeaderAndProperties(message, flowFileAttributes);
return message;
}
});
}
void publish(String destinationName, String messageText, final Map<String, String> flowFileAttributes) {
this.jmsTemplate.send(destinationName, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage message = session.createTextMessage(messageText);
setMessageHeaderAndProperties(message, flowFileAttributes);
return message;
}
});
}
void setMessageHeaderAndProperties(final Message message, final Map<String, String> flowFileAttributes) throws JMSException {
if (flowFileAttributes != null && !flowFileAttributes.isEmpty()) {
for (Entry<String, String> entry : flowFileAttributes.entrySet()) {
try {
if (entry.getKey().equals(JmsHeaders.DELIVERY_MODE)) {
this.jmsTemplate.setDeliveryMode(Integer.parseInt(entry.getValue()));
this.jmsTemplate.setExplicitQosEnabled(true);
} else if (entry.getKey().equals(JmsHeaders.EXPIRATION)) {
this.jmsTemplate.setTimeToLive(Integer.parseInt(entry.getValue()));
this.jmsTemplate.setExplicitQosEnabled(true);
} else if (entry.getKey().equals(JmsHeaders.PRIORITY)) {
this.jmsTemplate.setPriority(Integer.parseInt(entry.getValue()));
this.jmsTemplate.setExplicitQosEnabled(true);
} else if (entry.getKey().equals(JmsHeaders.REDELIVERED)) {
message.setJMSRedelivered(Boolean.parseBoolean(entry.getValue()));
} else if (entry.getKey().equals(JmsHeaders.TIMESTAMP)) {
message.setJMSTimestamp(Long.parseLong(entry.getValue()));
} else if (entry.getKey().equals(JmsHeaders.CORRELATION_ID)) {
message.setJMSCorrelationID(entry.getValue());
} else if (entry.getKey().equals(JmsHeaders.TYPE)) {
message.setJMSType(entry.getValue());
} else if (entry.getKey().equals(JmsHeaders.REPLY_TO)) {
Destination destination = buildDestination(entry.getValue());
if (destination != null) {
message.setJMSReplyTo(destination);
} else {
logUnbuildableDestination(entry.getValue(), JmsHeaders.REPLY_TO);
}
} else if (entry.getKey().equals(JmsHeaders.DESTINATION)) {
Destination destination = buildDestination(entry.getValue());
if (destination != null) {
message.setJMSDestination(destination);
} else {
logUnbuildableDestination(entry.getValue(), JmsHeaders.DESTINATION);
}
} else {
// not a special attribute handled above, so send it as a property using the specified property type
String type = flowFileAttributes.getOrDefault(entry.getKey().concat(".type"), "unknown").toLowerCase();
propertySetterMap.getOrDefault(type, JmsPropertySetterEnum.STRING)
.setProperty(message, entry.getKey(), entry.getValue());
}
} catch (NumberFormatException ne) {
this.processLog.warn("Incompatible value for attribute " + entry.getKey()
+ " [" + entry.getValue() + "] is not a number. Ignoring this attribute.");
}
}
}
}
private void logUnbuildableDestination(String destinationName, String headerName) {
this.processLog.warn("Failed to determine destination type from destination name '{}'. The '{}' header will not be set.", new Object[] {destinationName, headerName});
}
private Destination buildDestination(final String destinationName) {
Destination destination;
if (destinationName.toLowerCase().contains("topic")) {
destination = this.jmsTemplate.execute(new SessionCallback<Topic>() {
@Override
public Topic doInJms(Session session) throws JMSException {
return session.createTopic(destinationName);
}
});
} else if (destinationName.toLowerCase().contains("queue")) {
destination = this.jmsTemplate.execute(new SessionCallback<Queue>() {
@Override
public Queue doInJms(Session session) throws JMSException {
return session.createQueue(destinationName);
}
});
} else {
destination = null;
}
return destination;
}
/**
* Implementations of this interface use {@link javax.jms.Message} methods to set strongly typed properties.
*/
public interface JmsPropertySetter {
void setProperty(final Message message, final String name, final String value) throws JMSException, NumberFormatException;
}
public enum JmsPropertySetterEnum implements JmsPropertySetter {
BOOLEAN( (message, name, value) -> {
message.setBooleanProperty(name, Boolean.parseBoolean(value));
} ),
BYTE( (message, name, value) -> {
message.setByteProperty(name, Byte.parseByte(value));
} ),
SHORT( (message, name, value) -> {
message.setShortProperty(name, Short.parseShort(value));
} ),
INTEGER( (message, name, value) -> {
message.setIntProperty(name, Integer.parseInt(value));
} ),
LONG( (message, name, value) -> {
message.setLongProperty(name, Long.parseLong(value));
} ),
FLOAT( (message, name, value) -> {
message.setFloatProperty(name, Float.parseFloat(value));
} ),
DOUBLE( (message, name, value) -> {
message.setDoubleProperty(name, Double.parseDouble(value));
} ),
STRING( (message, name, value) -> {
message.setStringProperty(name, value);
} );
private final JmsPropertySetter setter;
JmsPropertySetterEnum(JmsPropertySetter setter) {
this.setter = setter;
}
public void setProperty(Message message, String name, String value) throws JMSException, NumberFormatException {
setter.setProperty(message, name, value);
}
}
/**
* This map helps us avoid using JmsPropertySetterEnum.valueOf and dealing with IllegalArgumentException on failed lookup.
*/
public static Map<String, JmsPropertySetterEnum> propertySetterMap = new HashMap<>();
static {
Arrays.stream(JmsPropertySetterEnum.values()).forEach(e -> propertySetterMap.put(e.name().toLowerCase(), e));
}
}