@@ -38,10 +38,16 @@ class StompContext implements Context
3838 */
3939 private $ stompFactory ;
4040
41+ /**
42+ * @var bool
43+ */
44+ private $ transient ;
45+
4146 /**
4247 * @param BufferedStompClient|callable $stomp
48+ * @param mixed $transient
4349 */
44- public function __construct ($ stomp , string $ extensionType )
50+ public function __construct ($ stomp , string $ extensionType, $ transient = true )
4551 {
4652 if ($ stomp instanceof BufferedStompClient) {
4753 $ this ->stomp = $ stomp ;
@@ -53,6 +59,7 @@ public function __construct($stomp, string $extensionType)
5359
5460 $ this ->extensionType = $ extensionType ;
5561 $ this ->useExchangePrefix = ExtensionType::RABBITMQ === $ extensionType ;
62+ $ this ->transient = $ transient ;
5663 }
5764
5865 /**
@@ -173,6 +180,8 @@ public function createConsumer(Destination $destination): Consumer
173180 {
174181 InvalidDestinationException ::assertDestinationInstanceOf ($ destination , StompDestination::class);
175182
183+ $ this ->transient = false ;
184+
176185 return new StompConsumer ($ this ->getStomp (), $ destination );
177186 }
178187
@@ -181,6 +190,10 @@ public function createConsumer(Destination $destination): Consumer
181190 */
182191 public function createProducer (): Producer
183192 {
193+ if ($ this ->transient && true == $ this ->stomp ) {
194+ $ this ->stomp ->disconnect ();
195+ }
196+
184197 return new StompProducer ($ this ->getStomp ());
185198 }
186199
@@ -202,14 +215,20 @@ public function purgeQueue(Queue $queue): void
202215 public function getStomp (): BufferedStompClient
203216 {
204217 if (false == $ this ->stomp ) {
205- $ stomp = call_user_func ($ this ->stompFactory );
206- if (false == $ stomp instanceof BufferedStompClient) {
207- throw new \LogicException (sprintf ('The factory must return instance of BufferedStompClient. It returns %s ' , is_object ($ stomp ) ? get_class ($ stomp ) : gettype ($ stomp )));
208- }
209-
210- $ this ->stomp = $ stomp ;
218+ $ this ->stomp = $ this ->createStomp ();
211219 }
212220
213221 return $ this ->stomp ;
214222 }
223+
224+ private function createStomp (): BufferedStompClient
225+ {
226+ $ stomp = call_user_func ($ this ->stompFactory );
227+
228+ if (false == $ stomp instanceof BufferedStompClient) {
229+ throw new \LogicException (sprintf ('The factory must return instance of BufferedStompClient. It returns %s ' , is_object ($ stomp ) ? get_class ($ stomp ) : gettype ($ stomp )));
230+ }
231+
232+ return $ stomp ;
233+ }
215234}
0 commit comments