1717package com .ericsson .ei .handlers ;
1818
1919import java .util .ArrayList ;
20+ import java .util .Arrays ;
2021import java .util .List ;
21-
22+ import org . json . JSONObject ;
2223import org .slf4j .Logger ;
2324import org .slf4j .LoggerFactory ;
2425import org .springframework .amqp .core .AcknowledgeMode ;
26+ import org .springframework .amqp .core .AmqpAdmin ;
2527import org .springframework .amqp .core .Binding ;
28+ import org .springframework .amqp .core .Binding .DestinationType ;
2629import org .springframework .amqp .core .BindingBuilder ;
2730import org .springframework .amqp .core .Queue ;
2831import org .springframework .amqp .core .TopicExchange ;
2932import org .springframework .amqp .rabbit .connection .CachingConnectionFactory ;
3033import org .springframework .amqp .rabbit .connection .ConnectionFactory ;
34+ import org .springframework .amqp .rabbit .core .RabbitAdmin ;
3135import org .springframework .amqp .rabbit .core .RabbitTemplate ;
3236import org .springframework .amqp .rabbit .core .RabbitTemplate .ConfirmCallback ;
3337import org .springframework .amqp .rabbit .listener .SimpleMessageListenerContainer ;
3438import org .springframework .amqp .rabbit .listener .adapter .MessageListenerAdapter ;
3539import org .springframework .amqp .rabbit .support .CorrelationData ;
40+ import org .springframework .beans .factory .annotation .Autowired ;
3641import org .springframework .beans .factory .annotation .Value ;
3742import org .springframework .context .annotation .Bean ;
3843import org .springframework .stereotype .Component ;
3944
4045import com .ericsson .ei .listener .EIMessageListenerAdapter ;
4146import com .fasterxml .jackson .annotation .JsonIgnore ;
47+ import com .mongodb .BasicDBObject ;
4248
4349import lombok .Getter ;
4450import lombok .Setter ;
@@ -108,9 +114,23 @@ public class RmqHandler {
108114 @ Setter
109115 @ Value ("${rabbitmq.consumerName}" )
110116 private String consumerName ;
117+
118+ @ Getter
119+ @ Setter
120+ @ Value ("${spring.data.mongodb.database}" )
121+ private String dataBaseName ;
122+
123+ @ Getter
124+ @ Setter
125+ @ Value ("${bindingkeys.collection.name}" )
126+ private String collectionName ;
111127
112128 @ Value ("${threads.maxPoolSize}" )
113129 private int maxThreads ;
130+
131+ @ Setter
132+ @ Autowired
133+ private MongoDBHandler mongoDBHandler ;
114134
115135 @ Setter
116136 @ JsonIgnore
@@ -122,6 +142,10 @@ public class RmqHandler {
122142 @ JsonIgnore
123143 private SimpleMessageListenerContainer container ;
124144
145+ @ Getter
146+ @ JsonIgnore
147+ private AmqpAdmin amqpAdmin ;
148+
125149 @ Bean
126150 public ConnectionFactory connectionFactory () {
127151 cachingConnectionFactory = new CachingConnectionFactory (host , port );
@@ -171,15 +195,75 @@ Binding binding() {
171195 }
172196
173197 @ Bean
174- public List <Binding > bindings () {
175- final String [] bingingKeysArray = splitBindingKeys (bindingKeys );
198+ public List <Binding > bindings (){
199+ final String [] bindingKeysArray = splitBindingKeys (bindingKeys );
176200 final List <Binding > bindingList = new ArrayList <>();
177- for (final String bindingKey : bingingKeysArray ) {
201+ for (final String bindingKey : bindingKeysArray ) {
178202 bindingList .add (BindingBuilder .bind (externalQueue ()).to (exchange ()).with (bindingKey ));
179203 }
204+ deleteBindings (bindingKeysArray ,bindingList );
180205 return bindingList ;
181206 }
182207
208+ /**
209+ * This method is used to delete the bindings in rabbitMQ.
210+ * By comparing the binding keys used in the properties and binding keys stored in mongoDB.
211+ * newBindingKeysArray is the only binding keys array.
212+ * AMQPBindingObjectList is entire list of bindings.
213+ * Binding key which is not present in the current AMQPBindingObjectList gets deleted and removed from mongoDB.
214+ * @return
215+ */
216+
217+ private void deleteBindings (String [] newBindingKeysArray , List <Binding > AMQPBindingObjectList ) {
218+ // Creating BindingKeys Collection in mongoDB
219+ ArrayList <String > allDocuments = mongoDBHandler .getAllDocuments (dataBaseName , collectionName );
220+ ArrayList <String > existingBindingsData = new ArrayList <String >();
221+ if (!allDocuments .isEmpty ()) {
222+ for (String bindings : allDocuments ) {
223+ JSONObject bindingObj = new JSONObject (bindings );
224+ final String mongoDbBindingKey = bindingObj .getString ("bindingKeys" );
225+ String condition = "{\" bindingKeys\" : /.*" + mongoDbBindingKey + "/}" ;
226+ if (!Arrays .asList (newBindingKeysArray ).contains (mongoDbBindingKey )) {
227+ String destinationDB = bindingObj .getString ("destination" );
228+ String exchangeDB = bindingObj .getString ("exchange" );
229+ // Binding the old binding key and removing from queue
230+ Binding b = new Binding (destinationDB , DestinationType .QUEUE , exchangeDB , mongoDbBindingKey , null );
231+ amqpAdmin = new RabbitAdmin (connectionFactory ());
232+ amqpAdmin .removeBinding (b );
233+ // Removing binding document from mongoDB
234+ mongoDBHandler .dropDocument (dataBaseName , collectionName , condition );
235+ } else {
236+ // storing the existing key into an array.
237+ existingBindingsData .add (mongoDbBindingKey );
238+ }
239+ }
240+ }
241+ // To store the new binding key into the mongoDB.
242+ storeNewBindingKeys (existingBindingsData , AMQPBindingObjectList );
243+ }
244+
245+ /**
246+ * This method is used to store the bindings of new binding key into mongoDB.
247+ * @return
248+ */
249+
250+ private void storeNewBindingKeys (ArrayList <String > existingBindingsData , List <Binding > AMQPBindingObjectList ){
251+ // comparing with the stored key and adding the new binding key into the mongoDB.
252+ for (final Binding bindingKey :AMQPBindingObjectList ){
253+ if (existingBindingsData .contains (bindingKey .getRoutingKey ())){
254+ LOGGER .info ("Binding already present in mongoDB" );
255+ }else {
256+ BasicDBObject document = new BasicDBObject ();
257+ document .put ("destination" ,bindingKey .getDestination ());
258+ document .put ("destinationType" , bindingKey .getDestinationType ().toString ());
259+ document .put ("exchange" , bindingKey .getExchange ());
260+ document .put ("bindingKeys" , bindingKey .getRoutingKey ());
261+ document .put ("arg" , bindingKey .getArguments ().toString ());
262+ mongoDBHandler .insertDocument (dataBaseName , collectionName , document .toString ());
263+ }
264+ }
265+ }
266+
183267 @ Bean
184268 public SimpleMessageListenerContainer bindToQueueForRecentEvents (
185269 final ConnectionFactory springConnectionFactory ,
0 commit comments