@@ -62,7 +62,7 @@ def __init__(
6262 uri: Single node connection URI
6363 uris: List of URIs for multi-node setup
6464 ssl_context: SSL configuration for secure connections
65- on_disconnection_handler: Callback for handling disconnection events
65+ reconnect: Ability to automatically reconnect in case of disconnections from the server
6666
6767 Raises:
6868 ValueError: If neither uri nor uris is provided
@@ -76,10 +76,10 @@ def __init__(
7676 self ._addr : Optional [str ] = uri
7777 self ._addrs : Optional [list [str ]] = uris
7878 self ._conn : BlockingConnection
79- self ._management : Management
8079 self ._conf_ssl_context : Union [
8180 PosixSslConfigurationContext , WinSslConfigurationContext , None
8281 ] = ssl_context
82+ self ._managements : list [Management ] = []
8383 self ._reconnect = reconnect
8484 self ._ssl_domain = None
8585 self ._connections = [] # type: ignore
@@ -158,7 +158,7 @@ def dial(self) -> None:
158158 on_disconnection_handler = self ._on_disconnection ,
159159 )
160160
161- self ._open ()
161+ # self._open()
162162 logger .debug ("Connection to the server established" )
163163
164164 def _win_store_to_cert (
@@ -185,7 +185,12 @@ def management(self) -> Management:
185185 Returns:
186186 Management: The management interface for performing administrative tasks
187187 """
188- return self ._management
188+ if len (self ._managements ) == 0 :
189+ management = Management (self ._conn )
190+ management .open ()
191+ self ._managements .append (management )
192+
193+ return self ._managements [0 ]
189194
190195 # closes the connection to the AMQP 1.0 server.
191196 def close (self ) -> None :
@@ -196,9 +201,9 @@ def close(self) -> None:
196201 """
197202 logger .debug ("Closing connection" )
198203 try :
199- for publisher in self ._publishers :
204+ for publisher in self ._publishers [:] :
200205 publisher .close ()
201- for consumer in self ._consumers :
206+ for consumer in self ._consumers [:] :
202207 consumer .close ()
203208 self ._conn .close ()
204209 except Exception as e :
@@ -228,8 +233,9 @@ def publisher(self, destination: str = "") -> Publisher:
228233 "destination address must start with /queues or /exchanges"
229234 )
230235 publisher = Publisher (self ._conn , destination )
236+ publisher ._set_publishers_list (self ._publishers )
231237 self ._publishers .append (publisher )
232- return self . _publishers [ self . _publishers . index ( publisher )]
238+ return publisher
233239
234240 def consumer (
235241 self ,
@@ -265,26 +271,38 @@ def consumer(
265271
266272 def _on_disconnection (self ) -> None :
267273
268- print ("disconnected" )
269-
270274 if self in self ._connections :
271275 self ._connections .remove (self )
272276
273- print ("reconnecting" )
274277 self ._conn = BlockingConnection (
275278 url = self ._addr ,
276279 urls = self ._addrs ,
277280 ssl_domain = self ._ssl_domain ,
278281 on_disconnection_handler = self ._on_disconnection ,
279282 )
280- self . _open ()
283+
281284 self ._connections .append (self )
282285
283- for index , publisher in enumerate (self ._publishers ):
284- # publisher = self._publishers.pop(index)
285- # address = publisher.address
286- self ._publishers .remove (publisher )
287- # self._publishers.insert(index, Publisher(self._conn, address))
286+ for i , management in enumerate (self ._managements ):
287+ # Update the broken connection and sender in the management
288+ self ._managements [i ]._update_connection (self ._conn )
289+
290+ for i , publisher in enumerate (self ._publishers ):
291+ # Update the broken connection and sender in the publisher
292+ self ._publishers [i ]._update_connection (self ._conn )
288293
289294 for i , consumer in enumerate (self ._consumers ):
290- self ._consumers .remove (consumer )
295+ # Update the broken connection and sender in the consumer
296+ self ._consumers [i ]._update_connection (self ._conn )
297+
298+ @property
299+ def active_producers (self ) -> int :
300+ """Returns the number of active publishers"""
301+ return len (self ._publishers )
302+
303+ @property
304+ def active_consumers (self ) -> int :
305+ """Returns the number of active consumers"""
306+ return len (self ._consumers )
307+
308+
0 commit comments