Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add partition selector feature #122

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions EnvelopeItem/PartitionStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Enqueue\MessengerAdapter\EnvelopeItem;

use Interop\Queue\Message;
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
use Symfony\Component\Messenger\Stamp\StampInterface;

class PartitionStamp implements StampInterface
{
private int $partition;

public function getPartition(): int
{
return $this->partition;
}

public function setPartition(int $partition): self
{
$this->partition = $partition;

return $this;
}
}
6 changes: 6 additions & 0 deletions QueueInteropTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Enqueue\AmqpTools\RabbitMqDelayPluginDelayStrategy;
use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy;
use Enqueue\MessengerAdapter\EnvelopeItem\InteropMessageStamp;
use Enqueue\MessengerAdapter\EnvelopeItem\PartitionStamp;
use Enqueue\MessengerAdapter\EnvelopeItem\TransportConfiguration;
use Enqueue\MessengerAdapter\Exception\MissingMessageMetadataSetterException;
use Enqueue\MessengerAdapter\Exception\SendingMessageFailedException;
Expand Down Expand Up @@ -168,6 +169,11 @@ public function send(Envelope $envelope): Envelope
$producer->setTimeToLive($this->options['timeToLive']);
}

$partitionStamp = $envelope->last(PartitionStamp::class);
if (method_exists($interopMessage, 'setPartition') && !is_null($partitionStamp)) {
$interopMessage->setPartition($partitionStamp->getPartition());
}

try {
$producer->send($topic, $interopMessage);
} catch (InteropQueueException $e) {
Expand Down
21 changes: 21 additions & 0 deletions Tests/EnvelopeItem/PartitionStampTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Enqueue\MessengerAdapter\Tests\EnvelopeItem;

use Enqueue\MessengerAdapter\EnvelopeItem\PartitionStamp;
use PHPUnit\Framework\TestCase;

class PartitionStampTest extends TestCase
{
public function testPartitionSetter()
{
$partitionStamp = (new PartitionStamp())->setPartition(1);
$this->assertInstanceOf(PartitionStamp::class, $partitionStamp);
}

public function testPartitionGetter()
{
$partitionStamp = (new PartitionStamp())->setPartition(1);
$this->assertEquals(1, $partitionStamp->getPartition());
}
}