@@ -58,7 +58,7 @@ def __init__(
58
58
self ._addr : Optional [str ] = uri
59
59
self ._addrs : Optional [list [str ]] = uris
60
60
self ._conn : BlockingConnection
61
- self ._management : Management
61
+ self ._managements : list [ Management ] = []
62
62
self ._reconnect = reconnect
63
63
self ._conf_ssl_context : Optional [SslConfigurationContext ] = ssl_context
64
64
self ._ssl_domain = None
@@ -108,21 +108,22 @@ def dial(self) -> None:
108
108
on_disconnection_handler = self ._on_disconnection ,
109
109
)
110
110
111
- self ._open ()
111
+ # self._open()
112
112
logger .debug ("Connection to the server established" )
113
113
114
- def _open (self ) -> None :
115
- self ._management = Management (self ._conn )
116
- self ._management .open ()
117
-
118
114
def management (self ) -> Management :
119
115
"""
120
116
Get the management interface for this connection.
121
117
122
118
Returns:
123
119
Management: The management interface for performing administrative tasks
124
120
"""
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 ]
126
127
127
128
# closes the connection to the AMQP 1.0 server.
128
129
def close (self ) -> None :
@@ -133,9 +134,9 @@ def close(self) -> None:
133
134
"""
134
135
logger .debug ("Closing connection" )
135
136
try :
136
- for publisher in self ._publishers :
137
+ for publisher in self ._publishers [:] :
137
138
publisher .close ()
138
- for consumer in self ._consumers :
139
+ for consumer in self ._consumers [:] :
139
140
consumer .close ()
140
141
self ._conn .close ()
141
142
except Exception as e :
@@ -165,8 +166,9 @@ def publisher(self, destination: str = "") -> Publisher:
165
166
"destination address must start with /queues or /exchanges"
166
167
)
167
168
publisher = Publisher (self ._conn , destination )
169
+ publisher ._set_publishers_list (self ._publishers )
168
170
self ._publishers .append (publisher )
169
- return self . _publishers [ self . _publishers . index ( publisher )]
171
+ return publisher
170
172
171
173
def consumer (
172
174
self ,
@@ -202,26 +204,41 @@ def consumer(
202
204
203
205
def _on_disconnection (self ) -> None :
204
206
205
- print ("disconnected" )
206
-
207
207
if self in self ._connections :
208
208
self ._connections .remove (self )
209
209
210
- print ("reconnecting" )
211
210
self ._conn = BlockingConnection (
212
211
url = self ._addr ,
213
212
urls = self ._addrs ,
214
213
ssl_domain = self ._ssl_domain ,
215
214
on_disconnection_handler = self ._on_disconnection ,
216
215
)
217
- self . _open ()
216
+
218
217
self ._connections .append (self )
219
218
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 )
225
226
226
227
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