17
17
package com .ericsson .ei .handlers ;
18
18
19
19
import java .util .ArrayList ;
20
+ import java .util .Arrays ;
20
21
import java .util .List ;
21
22
22
23
import org .apache .commons .lang3 .StringUtils ;
24
+ import org .json .JSONObject ;
23
25
import org .slf4j .Logger ;
24
26
import org .slf4j .LoggerFactory ;
25
27
import org .springframework .amqp .core .AcknowledgeMode ;
28
+ import org .springframework .amqp .core .AmqpAdmin ;
26
29
import org .springframework .amqp .core .Binding ;
30
+ import org .springframework .amqp .core .Binding .DestinationType ;
27
31
import org .springframework .amqp .core .BindingBuilder ;
28
32
import org .springframework .amqp .core .Queue ;
29
33
import org .springframework .amqp .core .TopicExchange ;
30
34
import org .springframework .amqp .rabbit .connection .CachingConnectionFactory ;
31
35
import org .springframework .amqp .rabbit .connection .ConnectionFactory ;
36
+ import org .springframework .amqp .rabbit .core .RabbitAdmin ;
32
37
import org .springframework .amqp .rabbit .core .RabbitTemplate ;
33
38
import org .springframework .amqp .rabbit .core .RabbitTemplate .ConfirmCallback ;
34
39
import org .springframework .amqp .rabbit .listener .SimpleMessageListenerContainer ;
41
46
42
47
import com .ericsson .ei .listeners .EIMessageListenerAdapter ;
43
48
import com .ericsson .ei .listeners .RMQConnectionListener ;
49
+ import com .ericsson .ei .mongo .MongoCondition ;
50
+ import com .ericsson .ei .mongo .MongoConstants ;
51
+ import com .ericsson .ei .mongo .MongoDBHandler ;
52
+ import com .fasterxml .jackson .annotation .JsonIgnore ;
53
+ import com .mongodb .BasicDBObject ;
44
54
45
55
import lombok .Getter ;
46
56
import lombok .Setter ;
@@ -68,6 +78,24 @@ public class RMQHandler {
68
78
@ Autowired
69
79
private RMQProperties rmqProperties ;
70
80
81
+ @ Getter
82
+ @ Setter
83
+ @ Value ("${spring.data.mongodb.database}" )
84
+ private String dataBaseName ;
85
+
86
+ @ Getter
87
+ @ Setter
88
+ @ Value ("${bindingkeys.collection.name}" )
89
+ private String collectionName ;
90
+
91
+ @ Setter
92
+ @ Autowired
93
+ private MongoDBHandler mongoDBHandler ;
94
+
95
+ @ Getter
96
+ @ JsonIgnore
97
+ private AmqpAdmin amqpAdmin ;
98
+
71
99
@ Bean
72
100
public ConnectionFactory connectionFactory () {
73
101
cachingConnectionFactory = new CachingConnectionFactory (rmqProperties .getHost (), rmqProperties .getPort ());
@@ -169,11 +197,12 @@ protected Binding binding() {
169
197
170
198
@ Bean
171
199
public List <Binding > bindings () {
172
- String [] bingingKeysArray = splitBindingKeys (rmqProperties .getBindingKeys ());
200
+ String [] bindingKeysArray = splitBindingKeys (rmqProperties .getBindingKeys ());
173
201
List <Binding > bindingList = new ArrayList <Binding >();
174
- for (String bindingKey : bingingKeysArray ) {
202
+ for (String bindingKey : bindingKeysArray ) {
175
203
bindingList .add (BindingBuilder .bind (externalQueue ()).to (exchange ()).with (bindingKey ));
176
204
}
205
+ deleteBindings (bindingKeysArray ,bindingList );
177
206
return bindingList ;
178
207
}
179
208
@@ -185,4 +214,61 @@ private String[] splitBindingKeys(String bindingKeys) {
185
214
String bindingKeysWithoutWhitespace = bindingKeys .replaceAll ("\\ s+" , "" );
186
215
return bindingKeysWithoutWhitespace .split ("," );
187
216
}
217
+
218
+ /**
219
+ * This method is used to delete the bindings in rabbitMQ.
220
+ * By comparing the binding keys used in the properties and binding keys stored in mongoDB.
221
+ * newBindingKeysArray is the only binding keys array.
222
+ * AMQPBindingObjectList is entire list of bindings.
223
+ * Binding key which is not present in the current AMQPBindingObjectList gets deleted and removed from mongoDB.
224
+ * @return
225
+ */
226
+ private void deleteBindings (String [] newBindingKeysArray , List <Binding > AMQPBindingObjectList ) {
227
+ // Creating BindingKeys Collection in mongoDB
228
+ ArrayList <String > allDocuments = mongoDBHandler .getAllDocuments (dataBaseName , collectionName );
229
+ ArrayList <String > existingBindingsData = new ArrayList <String >();
230
+ if (!allDocuments .isEmpty ()) {
231
+ for (String bindings : allDocuments ) {
232
+ JSONObject bindingObj = new JSONObject (bindings );
233
+ final String mongoDbBindingKey = bindingObj .getString ("bindingKeys" );
234
+ MongoCondition condition = MongoCondition .bindingKeyCondition (mongoDbBindingKey );
235
+ if (!Arrays .asList (newBindingKeysArray ).contains (mongoDbBindingKey )) {
236
+ String destinationDB = bindingObj .getString ("destination" );
237
+ String exchangeDB = bindingObj .getString ("exchange" );
238
+ // Binding the old binding key and removing from queue
239
+ Binding b = new Binding (destinationDB , DestinationType .QUEUE , exchangeDB , mongoDbBindingKey , null );
240
+ amqpAdmin = new RabbitAdmin (connectionFactory ());
241
+ amqpAdmin .removeBinding (b );
242
+ // Removing binding document from mongoDB
243
+ mongoDBHandler .dropDocument (dataBaseName , collectionName , condition );
244
+ } else {
245
+ // storing the existing key into an array.
246
+ existingBindingsData .add (mongoDbBindingKey );
247
+ }
248
+ }
249
+ }
250
+ // to store the binding keys used for rabbitMQ, in mongo db.
251
+ storeNewBindingKeys (existingBindingsData , AMQPBindingObjectList );
252
+ }
253
+
254
+ /**
255
+ * This method is used to store the binding keys used for rabbitMQ, in mongoDB.
256
+ * @return
257
+ */
258
+ private void storeNewBindingKeys (ArrayList <String > existingBindingsData , List <Binding > AMQPBindingObjectList ){
259
+ // comparing with the stored key and adding the new binding key into the mongoDB.
260
+ for (final Binding bindingKey :AMQPBindingObjectList ){
261
+ if (existingBindingsData .contains (bindingKey .getRoutingKey ())){
262
+ LOGGER .info ("Binding already present in mongoDB" );
263
+ }else {
264
+ BasicDBObject document = new BasicDBObject ();
265
+ document .put (MongoConstants .MB_DESTINATION ,bindingKey .getDestination ());
266
+ document .put (MongoConstants .MB_DESTINATIONT_TYPE , bindingKey .getDestinationType ().toString ());
267
+ document .put (MongoConstants .MB_EXCHANGE , bindingKey .getExchange ());
268
+ document .put (MongoConstants .MB_BINDING_KEYS , bindingKey .getRoutingKey ());
269
+ document .put (MongoConstants .MB_ARG , bindingKey .getArguments ().toString ());
270
+ mongoDBHandler .insertDocument (dataBaseName , collectionName , document .toString ());
271
+ }
272
+ }
273
+ }
188
274
}
0 commit comments