Comments (10)
@anedisi correlation_id is not needed if temporary queue is used only once.
delivery_mode 1 is intentional, because persisted message has no use to me if producer dies.
@stevenklar it is not part of code you can just push to the repository. Every param must be configurable.
This could be a method in https://github.com/bschmitt/laravel-amqp/blob/master/src/Publisher.php Publisher class, e.g.:
public function rpc($routing, $message, $timeout = 0, $queueOptions, $exchangeOptions)
{
//@todo check if message is instance of Message class
//@todo use queueOptions options for queue declaration
//@todo use exchangeOptions for exchange declaration if needed, but optional
//@notice multiple messages per temp queue will not be supported
/* @var Bschmitt\Amqp\Publisher $publisher */
$publisher = app()->make('Bschmitt\Amqp\Publisher');
$publisher->connect();
$publisher->getConnection()->set_close_on_destruct();
$replyTo = $publisher->getChannel()->queue_declare(
'',
false,
false,
true,
true
);
$replyTo = $replyTo[0];
$publisher->getChannel()->queue_declare(
$queue,
false,
false,
false,
false
);
$response = false;
$publisher->getChannel()->basic_consume(
$replyTo,
'',
false,
false,
false,
false,
function ($message) use (&$response) {
$response = $message;
}
);
$publisher->getChannel()->queue_bind($queue, 'amq.direct', $queue);
$publisher->getChannel()->basic_publish(
new \Bschmitt\Amqp\Message(
$message,
[
'content_type' => 'text/plain',
'delivery_mode' => 1,
'reply_to' => $replyTo,
]
),
'amq.direct',
$queue
);
$publisher->getChannel()->wait(null, false, $timeout);
return $response;
}
Feel free to modify this and fork/push.
from laravel-amqp.
I am not planing to make a pull request for this, so feel free to push it by yourself. Tag me for review if needed :)
from laravel-amqp.
Made it! Just a few lines of code:
function queue_rpc($queue, $message, $timeout = 0) {
/* @var Bschmitt\Amqp\Publisher $publisher */
$publisher = app()->make('Bschmitt\Amqp\Publisher');
$publisher->connect();
$publisher->getConnection()->set_close_on_destruct();
$replyTo = $publisher->getChannel()->queue_declare(
'',
false,
false,
true,
true
);
$replyTo = $replyTo[0];
$publisher->getChannel()->queue_declare(
$queue,
false,
false,
false,
false
);
$response = false;
$publisher->getChannel()->basic_consume(
$replyTo,
'',
false,
false,
false,
false,
function ($message) use (&$response) {
$response = $message;
}
);
$publisher->getChannel()->queue_bind($queue, 'amq.direct', $queue);
$publisher->getChannel()->basic_publish(
new \Bschmitt\Amqp\Message(
$message,
[
'content_type' => 'text/plain',
'delivery_mode' => 1,
'reply_to' => $replyTo,
]
),
'amq.direct',
$queue
);
$publisher->getChannel()->wait(null, false, $timeout);
return $response;
}
queue_rpc('ping-queue', 'ping', 10); // -> pong
@todo add consumer example, but it is a lot simpler
from laravel-amqp.
@AidasK Could you fork this repository and open a pull request, please?
from laravel-amqp.
i think you are missing correlation_id and i would set the delivery_mode to 2,
from laravel-amqp.
fair enough for delivery_mode, but i would argue people to use correlation_id for rpc, or at least it should be the option for something like a general purpose library like this is.
from laravel-amqp.
Will test this out and submit a first proposal with the resolved todo's you mentioned.
Thank you!
@AidasK If you would like to add this yourself, please don't hesitate and open a pull request.
from laravel-amqp.
Worker example:
\Amqp::consume('worker', function (AMQPMessage $message, Consumer $consumer) {
$this->info($message->getBody(), 'v');
$correlationId = $message->has('correlation_id') ? $message->get('correlation_id') : null;
$consumer->getChannel()->basic_publish(
new AMQPMessage(
$response,
[
'content_type' => 'text/plain',
'delivery_mode' => 1,
'correlation_id' => $correlationId
]
),
'',
$message->get('reply_to')
);
}, [
'exchange' => 'amq.direct',
'exchange_type' => 'direct',
'queue_force_declare' => true,
'queue_durable' => false,
'consumer_no_ack' => true,
]);
from laravel-amqp.
For those which are interested, there is some discussion in #54
from laravel-amqp.
I have a process like this in my microservice model:
service A-> publish service B
service B -> service A
How to get the results of service B sent to A in laravel controller.
from laravel-amqp.
Related Issues (20)
- Azure Service Bus - Support HOT 2
- Broken pipe or closed connection HOT 2
- Looking for help HOT 1
- PHP 8 support HOT 2
- AmazonMQ support HOT 1
- Target class [Amqp] does not exist. HOT 1
- Option for 'x-queue-type' HOT 1
- Configure RabbitMQ - Cluster HOT 1
- How to set initial durable for exchange HOT 1
- Non-static method 'consume' should not be called statically HOT 1
- PhpAmqpLib\Exception\AMQPInvalidFrameException: Invalid frame type 21 in /var/www/rabbit/php/app-rabbit/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php:606
- Unit Tests Schema update
- Support to set queue max length
- What happen if I do not acknowledge? HOT 2
- How to use reconnection? HOT 1
- Publish multiple messages - Batch publish HOT 10
- [Question]open and close connections or channels every time when publish message HOT 1
- Publisher and Consumer should not be singletons HOT 2
- Consumer priority HOT 1
- Version 2.0.11 doesn't work on PHP bellow 7.4 version HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-amqp.