Skip to content

Commit d9a4006

Browse files
author
Alessandro Niciforo
committed
Add support for using the /topic prefix instead of /exchange. RabbitMQ needs the /exchange prefix for publishing on an exchange, while ActiveMQ needs the /topic prefix for publishing on a topic
1 parent 72b339f commit d9a4006

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎pkg/stomp/StompContext.php‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@ class StompContext implements Context
2323
*/
2424
private $stomp;
2525

26+
/**
27+
* @var bool
28+
*/
29+
private $useExchangePrefix;
30+
2631
/**
2732
* @var callable
2833
*/
2934
private $stompFactory;
3035

3136
/**
3237
* @param BufferedStompClient|callable $stomp
38+
* @param bool $useExchangePrefix
3339
*/
34-
public function __construct($stomp)
40+
public function __construct($stomp, $useExchangePrefix = true)
3541
{
3642
if ($stomp instanceof BufferedStompClient) {
3743
$this->stomp = $stomp;
@@ -40,6 +46,8 @@ public function __construct($stomp)
4046
} else {
4147
throw new \InvalidArgumentException('The stomp argument must be either BufferedStompClient or callable that return BufferedStompClient.');
4248
}
49+
50+
$this->useExchangePrefix = $useExchangePrefix;
4351
}
4452

4553
/**
@@ -84,7 +92,7 @@ public function createTopic(string $name): Topic
8492
{
8593
if (0 !== strpos($name, '/')) {
8694
$destination = new StompDestination();
87-
$destination->setType(StompDestination::TYPE_EXCHANGE);
95+
$destination->setType($this->useExchangePrefix ? StompDestination::TYPE_EXCHANGE : StompDestination::TYPE_TOPIC);
8896
$destination->setStompName($name);
8997

9098
return $destination;

‎pkg/stomp/Tests/StompContextTest.php‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function testCreateQueueShouldCreateDestinationIfNameIsFullDestinationStr
7979
$this->assertEquals('/amq/queue/name/routing-key', $destination->getQueueName());
8080
}
8181

82-
public function testShouldCreateTopicInstance()
82+
public function testShouldCreateTopicInstanceWithExchangePrefix()
8383
{
8484
$context = new StompContext($this->createStompClientMock());
8585

@@ -91,6 +91,18 @@ public function testShouldCreateTopicInstance()
9191
$this->assertSame(StompDestination::TYPE_EXCHANGE, $topic->getType());
9292
}
9393

94+
public function testShouldCreateTopicInstanceWithTopicPrefix()
95+
{
96+
$context = new StompContext($this->createStompClientMock(), false);
97+
98+
$topic = $context->createTopic('the name');
99+
100+
$this->assertInstanceOf(StompDestination::class, $topic);
101+
$this->assertSame('/topic/the name', $topic->getQueueName());
102+
$this->assertSame('/topic/the name', $topic->getTopicName());
103+
$this->assertSame(StompDestination::TYPE_TOPIC, $topic->getType());
104+
}
105+
94106
public function testCreateTopicShouldCreateDestinationIfNameIsFullDestinationString()
95107
{
96108
$context = new StompContext($this->createStompClientMock());

0 commit comments

Comments
 (0)