Compare commits
6 Commits
8320fb4a84
...
v1.2.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 6bfb91a50f | |||
| 505990e247 | |||
| 3147dde1c6 | |||
| dce2a19295 | |||
| ac735d8bf5 | |||
| 6b044074ce |
10
README.md
10
README.md
@@ -33,8 +33,9 @@ return [
|
|||||||
|
|
||||||
### 1. Creating regular events for publishing to a RabbitMQ
|
### 1. Creating regular events for publishing to a RabbitMQ
|
||||||
|
|
||||||
Create an event that implements the `Broadcast` interface. `Broadcast` interface
|
Create an event that implements the `Broadcast` interface.
|
||||||
extends `JsonSerializable` then your event should implements `jsonSerialize` method.
|
Your event should implements `JsonSerializable` interface if
|
||||||
|
default serializer is used.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
namespace App\Events;
|
namespace App\Events;
|
||||||
@@ -42,8 +43,9 @@ namespace App\Events;
|
|||||||
use Diffhead\PHP\LaravelRabbitMQ\Event\Broadcast;
|
use Diffhead\PHP\LaravelRabbitMQ\Event\Broadcast;
|
||||||
use Diffhead\PHP\LaravelRabbitMQ\Trait\BroadcastEvent;
|
use Diffhead\PHP\LaravelRabbitMQ\Trait\BroadcastEvent;
|
||||||
use Illuminate\Foundation\Events\Dispatchable;
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
|
use JsonSerializable;
|
||||||
|
|
||||||
class UserCreated implements Broadcast
|
class UserCreated implements Broadcast, JsonSerializable
|
||||||
{
|
{
|
||||||
use Dispatchable, BroadcastEvent;
|
use Dispatchable, BroadcastEvent;
|
||||||
|
|
||||||
@@ -205,6 +207,7 @@ Consumer will listen rabbitmq bus and emit mapped event from as regular laravel
|
|||||||
#
|
#
|
||||||
# --connection=default - Connection name from config
|
# --connection=default - Connection name from config
|
||||||
# --queue=default - Queue
|
# --queue=default - Queue
|
||||||
|
# --queue-auto-delete - Queue auto-delete config
|
||||||
# --exchange=amq.direct - Exchange name
|
# --exchange=amq.direct - Exchange name
|
||||||
# --exchange-type=direct - Exchange type
|
# --exchange-type=direct - Exchange type
|
||||||
# --exchange-is-default - Exchange is default, required for default exchanges
|
# --exchange-is-default - Exchange is default, required for default exchanges
|
||||||
@@ -221,6 +224,7 @@ php artisan rabbitmq:consume
|
|||||||
php artisan rabbitmq:consume \
|
php artisan rabbitmq:consume \
|
||||||
--connection=default \
|
--connection=default \
|
||||||
--queue=service.users \
|
--queue=service.users \
|
||||||
|
--queue-auto-delete \
|
||||||
--exchange=amq.direct \
|
--exchange=amq.direct \
|
||||||
--exchange-type=direct \
|
--exchange-type=direct \
|
||||||
--routing-key=user.* \
|
--routing-key=user.* \
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"description": "A laravel package for events emitting between services using RabbitMQ as message broker.",
|
"description": "A laravel package for events emitting between services using RabbitMQ as message broker.",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"version": "1.2.0",
|
"version": "1.2.1",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"laravel", "rabbitmq", "event", "emit", "microservice",
|
"laravel", "rabbitmq", "event", "emit", "microservice",
|
||||||
"pipeline", "data exchanging", "message", "broker", "php8",
|
"pipeline", "data exchanging", "message", "broker", "php8",
|
||||||
|
|||||||
686
composer.lock
generated
686
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -24,7 +24,7 @@ class Consume extends Command
|
|||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $signature = 'rabbitmq:consume {--connection=} {--queue=} {--exchange=} {--exchange-type=} {--exchange-is-default} {--routing-key=} {--tag=}';
|
protected $signature = 'rabbitmq:consume {--connection=} {--queue=} {--queue-auto-delete} {--exchange=} {--exchange-type=} {--exchange-is-default} {--routing-key=} {--tag=}';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
@@ -99,7 +99,9 @@ class Consume extends Command
|
|||||||
{
|
{
|
||||||
return new Queue(
|
return new Queue(
|
||||||
name: $params->queue->value() ?? 'default',
|
name: $params->queue->value() ?? 'default',
|
||||||
declaration: new QueueDeclaration(),
|
declaration: new QueueDeclaration(
|
||||||
|
autoDelete: $params->queueAutoDelete->toBool()
|
||||||
|
),
|
||||||
bindings: new QueueBindings($params->routingKey->toString()),
|
bindings: new QueueBindings($params->routingKey->toString()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -107,8 +109,8 @@ class Consume extends Command
|
|||||||
private function getExchange(ConsumerParameters $params): Exchange
|
private function getExchange(ConsumerParameters $params): Exchange
|
||||||
{
|
{
|
||||||
return new Exchange(
|
return new Exchange(
|
||||||
name: $params->exchange->toString() ?? 'amq.direct',
|
name: $params->exchange->toString() ?: 'amq.direct',
|
||||||
type: $params->exchangeType->toString() ?? 'direct',
|
type: $params->exchangeType->toString() ?: 'direct',
|
||||||
isDefault: $params->exchangeIsDefault->toBool(),
|
isDefault: $params->exchangeIsDefault->toBool(),
|
||||||
declaration: new ExchangeDeclaration()
|
declaration: new ExchangeDeclaration()
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ use Diffhead\PHP\Dto\Property;
|
|||||||
/**
|
/**
|
||||||
* @property \Diffhead\PHP\Dto\Property<string|null> $connection
|
* @property \Diffhead\PHP\Dto\Property<string|null> $connection
|
||||||
* @property \Diffhead\PHP\Dto\Property<string|null> $queue
|
* @property \Diffhead\PHP\Dto\Property<string|null> $queue
|
||||||
|
* @property \Diffhead\PHP\Dto\Property<bool> $queueAutoDelete
|
||||||
* @property \Diffhead\PHP\Dto\Property<string|null> $exchange
|
* @property \Diffhead\PHP\Dto\Property<string|null> $exchange
|
||||||
* @property \Diffhead\PHP\Dto\Property<string|null> $routingKey
|
* @property \Diffhead\PHP\Dto\Property<string|null> $routingKey
|
||||||
* @property \Diffhead\PHP\Dto\Property<string|null> $exchangeType
|
* @property \Diffhead\PHP\Dto\Property<string|null> $exchangeType
|
||||||
@@ -20,6 +21,7 @@ class ConsumerParameters extends Dto
|
|||||||
{
|
{
|
||||||
protected Property $connection;
|
protected Property $connection;
|
||||||
protected Property $queue;
|
protected Property $queue;
|
||||||
|
protected Property $queueAutoDelete;
|
||||||
protected Property $exchange;
|
protected Property $exchange;
|
||||||
protected Property $routingKey;
|
protected Property $routingKey;
|
||||||
protected Property $exchangeType;
|
protected Property $exchangeType;
|
||||||
|
|||||||
@@ -33,21 +33,21 @@ class PublishEvent implements ShouldQueue
|
|||||||
$config = $this->configuration->getConnection($event->getConnection());
|
$config = $this->configuration->getConnection($event->getConnection());
|
||||||
|
|
||||||
$exchange = new Exchange(
|
$exchange = new Exchange(
|
||||||
|
declaration: new ExchangeDeclaration(),
|
||||||
name: $event->getExchange(),
|
name: $event->getExchange(),
|
||||||
type: $event->getExchangeType(),
|
type: $event->getExchangeType(),
|
||||||
isDefault: $event->getExchangeIsDefault(),
|
isDefault: $event->getExchangeIsDefault(),
|
||||||
declaration: new ExchangeDeclaration()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$queue = null;
|
$queue = null;
|
||||||
|
|
||||||
if ($event->getQueue()) {
|
if ($event->getQueue()) {
|
||||||
$queue = new Queue(
|
$queue = new Queue(
|
||||||
name: $event->getQueue(),
|
|
||||||
declaration: new QueueDeclaration(),
|
declaration: new QueueDeclaration(),
|
||||||
bindings: new QueueBindings(
|
bindings: new QueueBindings(
|
||||||
routingKey: $event->getRoutingKey()
|
routingKey: $event->getRoutingKey()
|
||||||
),
|
),
|
||||||
|
name: $event->getQueue(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ namespace Diffhead\PHP\LaravelRabbitMQ\Object;
|
|||||||
class Exchange
|
class Exchange
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
private ExchangeDeclaration $declaration,
|
||||||
private string $name = 'amq.direct',
|
private string $name = 'amq.direct',
|
||||||
private string $type = 'direct',
|
private string $type = 'direct',
|
||||||
private bool $isDefault = true,
|
private bool $isDefault = true,
|
||||||
private ExchangeDeclaration $declaration,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function name(): string
|
public function name(): string
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ namespace Diffhead\PHP\LaravelRabbitMQ\Object;
|
|||||||
class Queue
|
class Queue
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private string $name = 'default',
|
|
||||||
private QueueDeclaration $declaration,
|
private QueueDeclaration $declaration,
|
||||||
private QueueBindings $bindings,
|
private QueueBindings $bindings,
|
||||||
|
private string $name = 'default',
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function name(): string
|
public function name(): string
|
||||||
|
|||||||
@@ -45,6 +45,16 @@ class EventMapper implements EventMapperInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new AssociatedEventNotFound(json_encode($payload));
|
$error = <<<ERR
|
||||||
|
Not found event.
|
||||||
|
|
||||||
|
Queue: %s
|
||||||
|
Routing key: %s
|
||||||
|
Payload: %s
|
||||||
|
ERR;
|
||||||
|
|
||||||
|
throw new AssociatedEventNotFound(
|
||||||
|
sprintf($error, $queueName, $routingKey, json_encode($payload))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user