Laravel dispatch plain json on queue

吃可爱长大的小学妹 提交于 2021-01-04 07:14:04

问题


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

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);

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;}"
    }
}

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.

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?


回答1:


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.

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.

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),
        ],
    ]);
}

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.




回答2:


There is a simple way for your purpose: First install this package for rabbit:

vladimir-yuldashev/laravel-queue-rabbitmq

and in controller:

Queue::connection('rabbitmq')->pushRaw('{you can generate a json format here}', 'queue_name');

you can generate a json and put in this command.




回答3:


Did you look at the Laravel solution for queues and jobs management ?

https://laravel.com/docs/5.5/queues

If yes, what are the reasons of your other choice ?



来源:https://stackoverflow.com/questions/55380909/laravel-dispatch-plain-json-on-queue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!