Laravel在队列上调度普通json [英] Laravel dispatch plain json on queue
问题描述
我总体上有2个简单的问题.我目前正在Laravel中研究一些事件处理,并希望使用RabbitMQ作为我的事件存储.为此,我安装了此软件包,以以下内容开头: https://github.com/php-enqueue/enqueue-dev
I have 2 simple questions overall. Im currently looking into some event handling in Laravel and would like to use RabbitMQ as my event store. Therefor i installed this package to start with: https://github.com/php-enqueue/enqueue-dev
首先,我注册了它,然后将消息推送到RabbitMQ:
To get started i registered it and i am able to push messages on to RabbitMQ:
$job = (new Sendemail())->onQueue('email')->onConnection('interop');
dispatch($job);
但是问题是Laravel在队列中推送了某种格式,我不知道如何更改它.消息示例为:
The problem however is that Laravel pushes a certain format on the queue and i can't figure out how to change that. An example message would be:
{
"job":"Illuminate\\\\Queue\\\\CallQueuedHandler@call",
"data":{
"command":"O:29:\\"Acme\\Jobs\\FooJob\\":4:{s:11:\\"fooBar\\";s:7:\\"abc-123\\";s:5:\\"queue\\";N;s:5:\\"delay\\";N;s:6:\\"\\u0000*\\u0000job\\";N;}"
}
}
问题是,我该如何更改?这样做的主要原因是,用户方甚至不是一个无法解释PHP序列化模型的PHP应用程序.为此,我正在寻找一种方法来推送普通的JSON对象.
So the question is, how can i change this? The main reason on this is that the consumer side is not even a PHP application which also can not interpret the PHP serialized model. Therefor im looking for a way to push a plain JSON object instead.
另一方面,我还想了解您如何构建自定义侦听器?对于听众来说,同样的事情也会发生.Laravel尝试读取该方法,但是当我推送纯JSON时,它将永远无法工作.是否没有办法在某个主题上注册处理程序,并在处理程序本身内进一步处理消息的有效负载?
From the other hand i would also like to understand how you could build a custom listener? For the listener the same thing happens. Laravel tries to read the method but when i push plain JSON this will never work. Isn't there a way to register a handler on a topic and do further handling of the payload of the message within the handler itself?
推荐答案
有一个 laravel-queue与您链接的php-enqueue库一起使用的库,以使其与 Laravel的内置队列兼容Florian提到的系统.
There's a laravel-queue library that works with the php-enqueue library you linked to make it compatible with Laravel's built in queue system that Florian mentioned.
默认情况下,它仍将使用序列化的对象,但是我认为可以覆盖它.如果您查看 Queue.php,createObjectPayload()在核心Laravel框架中的第130行上进行,这就是要对作业进行序列化的地方.
By default, it will still use a serialized object, but I think that can be overridden. If you look in Queue.php, createObjectPayload() on line 130 in the core Laravel Framework, that's where the job is being serialized.
如果在laravel-queue库中扩展Queue类,则应该能够将createObjectPayload更改为如下所示:
If you extend the Queue class in the laravel-queue library, you should be able to change createObjectPayload to look something like this:
protected function createObjectPayload($job, $queue)
{
$payload = $this->withCreatePayloadHooks($queue, [
'displayName' => $this->getDisplayName($job),
'job' => 'Illuminate\Queue\CallQueuedHandler@call',
'maxTries' => $job->tries ?? null,
'timeout' => $job->timeout ?? null,
'timeoutAt' => $this->getJobExpiration($job),
'data' => [
'commandName' => $job,
'command' => $job,
],
]);
return array_merge($payload, [
'data' => [
'commandName' => get_class($job),
'command' => json_encode(clone $job),
],
]);
}
那应该对工作数据进行JSON编码,而不是对其进行序列化.您甚至可以完全删除编码,因为我认为它已经在链的某个位置进行了JSON编码.
That should JSON encode the job data instead of serializing it. You may even be able to remove the encoding altogether, as I think it's already JSON encoded somewhere up the chain.
这篇关于Laravel在队列上调度普通json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!