@@ -58,7 +58,7 @@ def __init__(
5858 self ._addr : Optional [str ] = uri
5959 self ._addrs : Optional [list [str ]] = uris
6060 self ._conn : BlockingConnection
61- self ._management : Management
61+ self ._managements : list [ Management ] = []
6262 self ._reconnect = reconnect
6363 self ._conf_ssl_context : Optional [SslConfigurationContext ] = ssl_context
6464 self ._ssl_domain = None
@@ -108,21 +108,22 @@ def dial(self) -> None:
108108 on_disconnection_handler = self ._on_disconnection ,
109109 )
110110
111- self ._open ()
111+ # self._open()
112112 logger .debug ("Connection to the server established" )
113113
114- def _open (self ) -> None :
115- self ._management = Management (self ._conn )
116- self ._management .open ()
117-
118114 def management (self ) -> Management :
119115 """
120116 Get the management interface for this connection.
121117
122118 Returns:
123119 Management: The management interface for performing administrative tasks
124120 """
125- return self ._management
121+ if len (self ._managements ) == 0 :
122+ management = Management (self ._conn )
123+ management .open ()
124+ self ._managements .append (management )
125+
126+ return self ._managements [0 ]
126127
127128 # closes the connection to the AMQP 1.0 server.
128129 def close (self ) -> None :
@@ -133,9 +134,9 @@ def close(self) -> None:
133134 """
134135 logger .debug ("Closing connection" )
135136 try :
136- for publisher in self ._publishers :
137+ for publisher in self ._publishers [:] :
137138 publisher .close ()
138- for consumer in self ._consumers :
139+ for consumer in self ._consumers [:] :
139140 consumer .close ()
140141 self ._conn .close ()
141142 except Exception as e :
@@ -165,8 +166,9 @@ def publisher(self, destination: str = "") -> Publisher:
165166 "destination address must start with /queues or /exchanges"
166167 )
167168 publisher = Publisher (self ._conn , destination )
169+ publisher ._set_publishers_list (self ._publishers )
168170 self ._publishers .append (publisher )
169- return self . _publishers [ self . _publishers . index ( publisher )]
171+ return publisher
170172
171173 def consumer (
172174 self ,
@@ -202,26 +204,41 @@ def consumer(
202204
203205 def _on_disconnection (self ) -> None :
204206
205- print ("disconnected" )
206-
207207 if self in self ._connections :
208208 self ._connections .remove (self )
209209
210- print ("reconnecting" )
211210 self ._conn = BlockingConnection (
212211 url = self ._addr ,
213212 urls = self ._addrs ,
214213 ssl_domain = self ._ssl_domain ,
215214 on_disconnection_handler = self ._on_disconnection ,
216215 )
217- self . _open ()
216+
218217 self ._connections .append (self )
219218
220- for index , publisher in enumerate (self ._publishers ):
221- # publisher = self._publishers.pop(index)
222- # address = publisher.address
223- self ._publishers .remove (publisher )
224- # self._publishers.insert(index, Publisher(self._conn, address))
219+ for i , management in enumerate (self ._managements ):
220+ # Update the broken connection and sender in the management
221+ self ._managements [i ].update_connection (self ._conn )
222+
223+ for i , publisher in enumerate (self ._publishers ):
224+ # Update the broken connection and sender in the publisher
225+ self ._publishers [i ].update_connection (self ._conn )
225226
226227 for i , consumer in enumerate (self ._consumers ):
227- self ._consumers .remove (consumer )
228+ # Update the broken connection and sender in the consumer
229+ self ._consumers [i ].update_connection (self ._conn )
230+
231+ @property
232+ def active_producers (self ) -> int :
233+ """Returns the number of active connections"""
234+ return len (self ._publishers )
235+
236+ @property
237+ def active_consumers (self ) -> int :
238+ """Returns the number of active connections"""
239+ return len (self ._consumers )
240+
241+ @property
242+ def active_managements (self ) -> int :
243+ """Returns the number of active connections"""
244+ return len (self ._managements )
0 commit comments