如何将原始json消息发布到另一个应用程序队列?

mrzz3bfm  于 2022-11-08  发布在  RabbitMQ
关注(0)|答案(2)|浏览(109)

在我的情况下,我只需要从laravel应用程序发布消息到另一个symfony应用程序中创建的可用rabbitmq队列。我安装了这个包https://packagist.org/packages/vladimir-yuldashev/laravel-queue-rabbitmq,并按照文档https://laravel.com/docs/8.x/queues使laravel与rabbitmq一起工作。
我的工作类别:

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class HandleLogger implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *

     */
    public function __construct()
    {
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
    }
}

而在控制器中,我调用:

HandleLogger::dispatch('Some json message')
            ->onConnection('rabbitmq')
            ->onQueue('test-queue');

我只想发布一个原始的json消息,但是当我收到消息时,它的格式是:

{
    "uuid": "a6d302bf-74d5-45fa-96a8-ff833101b9ae",
    "displayName": "App\\Jobs\\HandleLogger",
    "job": "Illuminate\\Queue\\CallQueuedHandler@call",
    "maxTries": null,
    "maxExceptions": null,
    "failOnTimeout": false,
    "backoff": null,
    "timeout": null,
    "retryUntil": null,
    "data": {
        "commandName": "App\\Jobs\\HandleLogger",
        "command": "O:21:\"App\\Jobs\\HandleLogger\":10:{s:3:\"job\";N;s:10:\"connection\";s:8:\"rabbitmq\";s:5:\"queue\";s:10:\"test-queue\";s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:19:\"chainCatchCallbacks\";N;s:5:\"delay\";N;s:11:\"afterCommit\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}"
    },
    "id": "f58793de-0623-4972-a0f2-c6290df7fb28"
}

我找不到任何答案。

编辑:在laravel文档中,它只给出了一个例子,比如在构造函数参数中传递一个模型。

/**
 * @var string
 */
 protected $payLoad;

 public function __construct(string $payLoad)
 {
    $this->payLoad = $payLoad;
 }

但不影响:

{
    "uuid": "b402c214-9a75-4968-b628-2972dfab9edb",
    "displayName": "App\\Jobs\\HandleLogger",
    "job": "Illuminate\\Queue\\CallQueuedHandler@call",
    "maxTries": null,
    "maxExceptions": null,
    "failOnTimeout": false,
    "backoff": null,
    "timeout": null,
    "retryUntil": null,
    "data": {
        "commandName": "App\\Jobs\\HandleLogger",
        "command": "O:21:\"App\\Jobs\\HandleLogger\":11:{s:10:\"\u0000*\u0000payLoad\";s:17:\"Some json message\";s:3:\"job\";N;s:10:\"connection\";s:8:\"rabbitmq\";s:5:\"queue\";s:10:\"test-queue\";s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:19:\"chainCatchCallbacks\";N;s:5:\"delay\";N;s:11:\"afterCommit\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}"
    },
    "id": "81d1a439-b120-4187-9dc6-0b20fe2d3a66"
}
qni6mghb

qni6mghb1#

它为我工作,不需要Laravel工作类:

$queueManager = app('queue');
$queue = $queueManager->connection('rabbitmq');
$queue->pushRaw('json message', 'queue_name');
z8dt9xmd

z8dt9xmd2#

app('queue')->connection('database')->pushRaw(json_encode($user), 'provisioning_users');

相关问题