-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 86e5882
Showing
10 changed files
with
799 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
name: PHPUnit | ||
on: | ||
push: | ||
branches: | ||
- main | ||
tags: | ||
- "*.*.*" | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
Build: | ||
runs-on: 'ubuntu-latest' | ||
container: 'byjg/php:${{ matrix.php-version }}-cli' | ||
strategy: | ||
matrix: | ||
php-version: | ||
- "8.2" | ||
- "8.1" | ||
- "8.0" | ||
- "7.4" | ||
|
||
services: | ||
rabbitmq: | ||
image: rabbitmq:3-management | ||
env: | ||
RABBITMQ_DEFAULT_USER: guest | ||
RABBITMQ_DEFAULT_PASS: guest | ||
ports: | ||
- "5672:5672" | ||
options: >- | ||
--health-cmd="rabbitmq-diagnostics status" | ||
--health-interval=10s | ||
--health-timeout=5s | ||
--health-retries=5 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- run: composer install | ||
- run: ./vendor/bin/phpunit | ||
|
||
# Documentation: | ||
# runs-on: 'ubuntu-latest' | ||
# needs: Build | ||
# if: github.ref == 'refs/heads/main' | ||
# env: | ||
# DOC_GITHUB_TOKEN: '${{ secrets.DOC_TOKEN }}' | ||
# steps: | ||
# - uses: actions/checkout@v3 | ||
# - run: curl https://opensource.byjg.com/add-doc.sh | bash /dev/stdin php message-queue-client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
vendor | ||
test_consume.php | ||
test_publish.php | ||
composer.lock | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Built-in web server", | ||
"type": "php", | ||
"request": "launch", | ||
"runtimeArgs": [ | ||
"-dxdebug.mode=debug", | ||
"-dxdebug.start_with_request=yes", | ||
"-S", | ||
"localhost:8080", | ||
"app.php" | ||
], | ||
"program": "", | ||
"env": { | ||
"APP_ENV": "test", | ||
"XDEBUG_MODE": "debug,develop", | ||
"XDEBUG_CONFIG": "client_port=${port}" | ||
}, | ||
"cwd": "${workspaceRoot}/public", | ||
"port": 9003, | ||
"serverReadyAction": { | ||
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started", | ||
"uriFormat": "http://localhost:%s/sample/ping", | ||
"action": "openExternally" | ||
} | ||
}, | ||
{ | ||
"name": "Debug current Script in Console", | ||
"type": "php", | ||
"request": "launch", | ||
"program": "${file}", | ||
"cwd": "${fileDirname}", | ||
"port": 9003, | ||
"runtimeArgs": [ | ||
"-dxdebug.start_with_request=yes" | ||
], | ||
"env": { | ||
"XDEBUG_MODE": "debug,develop", | ||
"XDEBUG_CONFIG": "client_port=${port}" | ||
} | ||
}, | ||
{ | ||
"name": "PHPUnit Debug", | ||
"type": "php", | ||
"request": "launch", | ||
"program": "${workspaceFolder}/vendor/bin/phpunit", | ||
"cwd": "${workspaceFolder}", | ||
"port": 9003, | ||
"runtimeArgs": [ | ||
"-dxdebug.start_with_request=yes" | ||
], | ||
"env": { | ||
"APP_ENV": "test", | ||
"XDEBUG_MODE": "debug,develop", | ||
"XDEBUG_CONFIG": "client_port=${port}" | ||
} | ||
}, | ||
{ | ||
"name": "PHPUnit Debug (Current file)", | ||
"type": "php", | ||
"request": "launch", | ||
"program": "${workspaceFolder}/vendor/bin/phpunit", | ||
"args": [ | ||
"${file}" | ||
], | ||
"cwd": "${workspaceFolder}", | ||
"port": 9003, | ||
"runtimeArgs": [ | ||
"-dxdebug.start_with_request=yes" | ||
], | ||
"env": { | ||
"APP_ENV": "test", | ||
"XDEBUG_MODE": "debug,develop", | ||
"XDEBUG_CONFIG": "client_port=${port}" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Joao Gilberto Magalhaes | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# RabbitMQ Client | ||
|
||
It creates a simple abstraction layer to publish and consume messages from the RabbitMQ Server using the component [byjg/message-queue-client](https://github.com/byjg/message-queue-client). | ||
|
||
For details on how to use the Message Queue Client see the [documentation](https://github.com/byjg/message-queue-client) | ||
|
||
## Usage | ||
|
||
### Publish | ||
|
||
```php | ||
<?php | ||
// Register the connector and associate with a scheme | ||
ConnectorFactory::registerConnector(RabbitMQConnector::class); | ||
|
||
// Create a connector | ||
$connector = ConnectorFactory::create(new Uri("amqp://$user:$pass@$host:$port/$vhost")); | ||
|
||
// Create a queue | ||
$pipe = new Pipe("test"); | ||
$pipe->withDeadLetter(new Pipe("dlq_test")); | ||
|
||
// Create a message | ||
$message = new Message("Hello World"); | ||
|
||
// Publish the message into the queue | ||
$connector->publish(new Envelope($pipe, $message)); | ||
``` | ||
|
||
### Consume | ||
|
||
```php | ||
<?php | ||
// Register the connector and associate with a scheme | ||
ConnectorFactory::registerConnector(RabbitMQConnector::class); | ||
|
||
// Create a connector | ||
$connector = ConnectorFactory::create(new Uri("amqp://$user:$pass@$host:$port/$vhost")); | ||
|
||
// Create a queue | ||
$pipe = new Pipe("test"); | ||
$pipe->withDeadLetter(new Pipe("dlq_test")); | ||
|
||
// Connect to the queue and wait to consume the message | ||
$connector->consume( | ||
$pipe, // Queue name | ||
function (Envelope $envelope) { // Callback function to process the message | ||
echo "Process the message"; | ||
echo $envelope->getMessage()->getBody(); | ||
return Message::ACK; | ||
}, | ||
function (Envelope $envelope, $ex) { // Callback function to process the failed message | ||
echo "Process the failed message"; | ||
echo $ex->getMessage(); | ||
return Message::REQUEUE; | ||
} | ||
); | ||
``` | ||
|
||
The consume method will wait for a message and call the callback function to process the message. | ||
If there is no message in the queue, the method will wait until a message arrives. | ||
|
||
If you want to exit the consume method, just return `Message::ACK | Message::EXIT` from the callback function. | ||
|
||
Possible return values from the callback function: | ||
|
||
* `Message::ACK` - Acknowledge the message and remove from the queue | ||
* `Message::NACK` - Not acknowledge the message and remove from the queue. If the queue has a dead letter queue, the message will be sent to the dead letter queue. | ||
* `Message::REQUEUE` - Requeue the message | ||
* `Message::EXIT` - Exit the consume method | ||
|
||
|
||
## RabbitMQ Client (AMQP Protocol) | ||
|
||
The RabbitMQ connector uses the [php-amqplib](https://github.com/php-amqplib/php-amqplib) library. | ||
|
||
The standard behavior of the connector is to create an Exchange, a Queue and bind the queue to the exchange with a routing key (by default is the same as the queue name). | ||
All messages are published to the exchange and consumed from the queue. | ||
|
||
As the queue and exchange is created by the Connector it is recommended you do not use to publish/consume from existing queues. | ||
If you use an existing Queue you might get the error: | ||
|
||
```text | ||
PHP Fatal error: Uncaught PhpAmqpLib\Exception\AMQPProtocolChannelException: PRECONDITION_FAILED - Existing queue 'test' declared with other arguments in AMQPChannel.php:224 | ||
``` | ||
|
||
You can change the behavior of the connection by using the `Pipe::withProperty()` and `Message::withHeader()` methods. | ||
Some of them are used by the RabbitMQConnector by setting some default values: | ||
|
||
* `Pipe::withProperty(RabbitMQConnector::EXCHANGE)` - Set the exchange name. Default is the queue name. | ||
* `Pipe::withProperty(RabbitMQConnector::ROUTING_KEY)` - Set the routing key. Default is the queue name. | ||
* `Pipe::withProperty('x-message-ttl')` - Only affects dead letter queues. Set the time to live of the message in milliseconds. Default 3 days. | ||
* `Pipe::withProperty('x-expires')` - Only affects dead letter queues. Set the time to live of the queue in milliseconds. Default 3 days. | ||
* `Message::withHeader('content_type')` - Set the content type of the message. Default is text/plain. | ||
* `Message::withHeader('delivery_mode')` - Set the delivery mode of the message. Default is 2 (persistent). | ||
|
||
Protocols: | ||
|
||
| Protocol | URI Example | Notes | | ||
| -------- | --------------------------------------------------- | ----- | | ||
| AMQP | amqp://user:pass@host:port/vhost | Default port: 5672. | | ||
| AMQPS | amqps://user:pass@host:port/vhost?arg1=...&args=... | Default port: 5671. Args: capath*, local_cert, local_pk, verify_peer, verify_peer_name | | ||
|
||
---- | ||
[Open source ByJG](http://opensource.byjg.com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "byjg/rabbitmq-client", | ||
"description": "A minimal rabbitmq client using the components of byjg/message-queue-client", | ||
"autoload": { | ||
"psr-4": { | ||
"ByJG\\MessageQueueClient\\RabbitMQ\\": "src/" | ||
} | ||
}, | ||
"prefer-stable": true, | ||
"minimum-stability": "dev", | ||
"require": { | ||
"php": ">=5.5.0", | ||
"ext-curl": "*", | ||
"byjg/message-queue-client": "dev-main", | ||
"php-amqplib/php-amqplib": "^3.5" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "5.7.*|7.4.*|^9.5" | ||
}, | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: '3.0' | ||
|
||
services: | ||
rabbitmq: | ||
image: rabbitmq:3-management | ||
ports: | ||
- 5672:5672 | ||
- 15672:15672 | ||
environment: | ||
- RABBITMQ_DEFAULT_USER=guest | ||
- RABBITMQ_DEFAULT_PASS=guest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
To change this license header, choose License Headers in Project Properties. | ||
To change this template file, choose Tools | Templates | ||
and open the template in the editor. | ||
--> | ||
|
||
<!-- see http://www.phpunit.de/wiki/Documentation --> | ||
<phpunit bootstrap="./vendor/autoload.php" | ||
colors="false" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
convertDeprecationsToExceptions="true" | ||
stopOnFailure="false"> | ||
|
||
<php> | ||
<ini name="display_errors" value="On" /> | ||
<ini name="display_startup_errors" value="On" /> | ||
<ini name="error_reporting" value="E_ALL" /> | ||
</php> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./src</directory> | ||
</whitelist> | ||
</filter> | ||
|
||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
</phpunit> |
Oops, something went wrong.