14
14
* #L%
15
15
*/
16
16
17
- import java .io .IOException ;
18
- import java .nio .charset .StandardCharsets ;
19
- import java .util .UUID ;
20
- import java .util .concurrent .TimeoutException ;
21
-
22
17
import com .google .gson .Gson ;
23
18
import com .rabbitmq .client .BuiltinExchangeType ;
24
19
import com .rabbitmq .client .CancelCallback ;
31
26
import org .slf4j .Logger ;
32
27
import org .slf4j .LoggerFactory ;
33
28
29
+ import java .io .IOException ;
30
+ import java .nio .charset .StandardCharsets ;
31
+ import java .util .UUID ;
32
+ import java .util .concurrent .TimeoutException ;
33
+
34
34
/**
35
35
* Helper class wrapping methods for interacting with message broker.
36
36
*/
@@ -58,22 +58,7 @@ public static Channel connectToTopicExchange(final String host,
58
58
final String userName ,
59
59
final String password ,
60
60
final String exchange ) throws IOException , TimeoutException {
61
- LOGGER .info ("Connecting to messagebroker {}:{} with user {}" , host , port , userName != null ? userName : "<null>" );
62
- final ConnectionFactory factory = new ConnectionFactory ();
63
-
64
- factory .setHost (host );
65
- factory .setPort (port );
66
-
67
- if (StringUtils .isNotBlank (userName )) {
68
- factory .setUsername (userName );
69
- }
70
- if (StringUtils .isNotBlank (password )) {
71
- factory .setPassword (password );
72
- }
73
-
74
- factory .setAutomaticRecoveryEnabled (true );
75
-
76
- final Connection connection = factory .newConnection ();
61
+ final Connection connection = getConnection (host , port , userName , password );
77
62
78
63
final Channel channel = connection .createChannel ();
79
64
@@ -84,40 +69,53 @@ public static Channel connectToTopicExchange(final String host,
84
69
}
85
70
86
71
/**
87
- * Connect to the message broker specified by {@code host} and {@code port }.
72
+ * Connect to message broker. Message broker details and credentials are specified in the given {@code dataSource }.
88
73
* Create the given {@code exchange} if it does not exist yet.
89
74
*
90
- * @param host the host where the message broker is running
91
- * @param port the port where the message broker is listening
92
- * @param exchange the topic exchange's name
75
+ * @param dataSource the {@link MessageBrokerDataSource} containing the message broker connection details
93
76
* @return a {@link Channel} object representing the established connection to the message broker
94
77
* @throws IOException in case of error
95
78
* @throws TimeoutException in case of error
96
79
* @see MessageBrokerUtil#connectToTopicExchange(String, int, String, String, String)
97
80
*/
98
- public static Channel connectToTopicExchange (final String host ,
99
- final int port ,
100
- final String exchange ) throws IOException , TimeoutException {
101
- return connectToTopicExchange (host , port , null , null , exchange );
81
+ public static Channel connectToTopicExchange (final MessageBrokerDataSource dataSource )
82
+ throws IOException , TimeoutException {
83
+ return connectToTopicExchange (
84
+ dataSource .getMessageBrokerServer (),
85
+ dataSource .getMessageBrokerPort (),
86
+ dataSource .getUserName (),
87
+ dataSource .getPassword (),
88
+ dataSource .getExchange ()
89
+ );
102
90
}
103
91
104
92
/**
105
- * Connect to message broker. Message broker details and credentials are specified in the given {@code dataSource}.
106
- * Create the given {@code exchange} if it does not exist yet.
93
+ * Connect to the message broker specified by {@code host} and {@code port}
94
+ * with the credentials specified by {@code userName} and {@code password}.
95
+ * Create the given {@code queue} if it does not exist yet.
107
96
*
108
- * @param dataSource the {@link MessageBrokerDataSource} containing the message broker connection details
97
+ * @param host the host where the message broker is running
98
+ * @param port the port where the message broker is listening
99
+ * @param userName the username to use when connecting to message broker - optional
100
+ * @param password the password to use when connecting to message broker - optional
101
+ * @param queue the queue's name
109
102
* @return a {@link Channel} object representing the established connection to the message broker
110
103
* @throws IOException in case of error
111
104
* @throws TimeoutException in case of error
112
- * @see MessageBrokerUtil#connectToTopicExchange(String, int, String, String, String)
113
105
*/
114
- public static Channel connectToTopicExchange (final MessageBrokerDataSource dataSource )
115
- throws IOException , TimeoutException {
116
- return connectToTopicExchange (dataSource .getMessageBrokerServer (),
117
- dataSource .getMessageBrokerPort (),
118
- dataSource .getUserName (),
119
- dataSource .getPassword (),
120
- dataSource .getExchange ());
106
+ public static Channel connectToQueue (final String host ,
107
+ final int port ,
108
+ final String userName ,
109
+ final String password ,
110
+ final String queue ) throws IOException , TimeoutException {
111
+ final Connection connection = getConnection (host , port , userName , password );
112
+
113
+ final Channel channel = connection .createChannel ();
114
+
115
+ LOGGER .info ("Creating queue {}" , queue );
116
+ channel .queueDeclare (queue , true , false , false , null );
117
+
118
+ return channel ;
121
119
}
122
120
123
121
/**
@@ -165,12 +163,14 @@ public static void registerListenerOnTopic(final Channel channel,
165
163
final MessageBrokerDataSource dataSource ,
166
164
final DeliverCallback deliverCallback ,
167
165
final CancelCallback cancelCallback ) throws IOException {
168
- registerListenerOnTopic (channel ,
169
- dataSource .getExchange (),
170
- dataSource .getTopic (),
171
- dataSource .getId (),
172
- deliverCallback ,
173
- cancelCallback );
166
+ registerListenerOnTopic (
167
+ channel ,
168
+ dataSource .getExchange (),
169
+ dataSource .getTopic (),
170
+ dataSource .getId (),
171
+ deliverCallback ,
172
+ cancelCallback
173
+ );
174
174
}
175
175
176
176
/**
@@ -191,14 +191,46 @@ public static void convertAndSendToTopic(final Channel channel,
191
191
final String topic ,
192
192
final Object payload ) throws IOException {
193
193
final String jsonMessage = GSON .toJson (payload );
194
- sendToTopic (channel , exchange , topic , jsonMessage );
195
- }
196
-
197
- private static void sendToTopic (final Channel channel ,
198
- final String exchange ,
199
- final String topic ,
200
- final String jsonMessage ) throws IOException {
201
194
LOGGER .info ("Publishing message to topic {}/{}: {}" , exchange , topic , jsonMessage );
202
195
channel .basicPublish (exchange , topic , null , jsonMessage .getBytes (StandardCharsets .UTF_8 ));
203
196
}
197
+
198
+ /**
199
+ * Converts the given {@code payload} object to a JSON string and sends it to the given {@code queue}.
200
+ * <p>
201
+ * Use {@link MessageBrokerUtil#connectToQueue(String, int, String, String, String)} to create {@link Channel}.
202
+ *
203
+ * @param channel the {@link Channel} object representing the established connection to the message broker
204
+ * @param queue the queue's name
205
+ * @param payload the object to send
206
+ * @throws IOException in case of error
207
+ */
208
+ public static void convertAndSendToQueue (final Channel channel , final String queue , final Object payload )
209
+ throws IOException {
210
+ final String jsonMessage = GSON .toJson (payload );
211
+ LOGGER .info ("Publishing message to queue {}: {}" , queue , jsonMessage );
212
+ channel .basicPublish ("" , queue , null , jsonMessage .getBytes (StandardCharsets .UTF_8 ));
213
+ }
214
+
215
+ private static Connection getConnection (final String host ,
216
+ final int port ,
217
+ final String userName ,
218
+ final String password ) throws IOException , TimeoutException {
219
+ LOGGER .info ("Connecting to messagebroker {}:{} with user {}" , host , port , userName != null ? userName : "<null>" );
220
+ final ConnectionFactory factory = new ConnectionFactory ();
221
+
222
+ factory .setHost (host );
223
+ factory .setPort (port );
224
+
225
+ if (StringUtils .isNotBlank (userName )) {
226
+ factory .setUsername (userName );
227
+ }
228
+ if (StringUtils .isNotBlank (password )) {
229
+ factory .setPassword (password );
230
+ }
231
+
232
+ factory .setAutomaticRecoveryEnabled (true );
233
+
234
+ return factory .newConnection ();
235
+ }
204
236
}
0 commit comments