Skip to content

Commit cd3c765

Browse files
committed
Allow connections that have no consumer to be considered 'transient'.
1 parent a439f00 commit cd3c765

2 files changed

Lines changed: 27 additions & 31 deletions

File tree

‎pkg/stomp/StompContext.php‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

‎pkg/stomp/StompProducer.php‎

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Interop\Queue\Message;
1212
use Interop\Queue\Producer;
1313
use Stomp\Client;
14-
use Stomp\Exception\ConnectionException;
1514
use Stomp\Transport\Message as StompLibMessage;
1615

1716
class StompProducer implements Producer
@@ -40,23 +39,9 @@ public function send(Destination $destination, Message $message): void
4039

4140
$stompMessage = new StompLibMessage($message->getBody(), $headers);
4241

43-
try {
44-
$this->stomp->send($destination->getQueueName(), $stompMessage);
45-
} catch (ConnectionException $ex) {
46-
if (!$this->stomp->isConnected()) {
47-
throw $ex;
48-
}
49-
50-
$this->stomp->disconnect();
51-
$this->stomp->connect();
52-
53-
$this->stomp->send($destination->getQueueName(), $stompMessage);
54-
}
42+
$this->stomp->send($destination->getQueueName(), $stompMessage);
5543
}
5644

57-
/**
58-
* @return $this|Producer
59-
*/
6045
public function setDeliveryDelay(int $deliveryDelay = null): Producer
6146
{
6247
if (empty($deliveryDelay)) {
@@ -71,11 +56,6 @@ public function getDeliveryDelay(): ?int
7156
return null;
7257
}
7358

74-
/**
75-
* @throws PriorityNotSupportedException
76-
*
77-
* @return $this|Producer
78-
*/
7959
public function setPriority(int $priority = null): Producer
8060
{
8161
if (empty($priority)) {
@@ -90,9 +70,6 @@ public function getPriority(): ?int
9070
return null;
9171
}
9272

93-
/**
94-
* @return $this|Producer
95-
*/
9673
public function setTimeToLive(int $timeToLive = null): Producer
9774
{
9875
if (empty($timeToLive)) {

0 commit comments

Comments
 (0)