序列化数据,创建SerializeJobData.php,复制以下代码进去
<?phpdeclare(strict_types=1);namespace AppJob;use HyperfUtilsStr;/** * 序列化队列数据 * Class SerializeJobData * @package AppJob */class SerializeJobData extends ProducerMessage{ public function __construct($job) { // 设置不同 pool $this->poolName = 'default'; /** * 当驱动为redis时 * use IlluminateSupportStr; * 'id' => Str::random(32),'attempts' => 0, */ if (env('QUEUE_DRIVER', 'rabbitmq') == 'rabbitmq') { $this->payload = [ 'displayName' => get_class($job), 'job' => 'IlluminateQueueCallQueuedHandler@call', 'maxTries' => isset($job->tries) ? $job->tries : null, 'timeout' => isset($job->timeout) ? $job->timeout : null, 'data' => [ 'commandName' => get_class($job), 'command' => serialize(clone $job) ] ]; } else { $this->payload = [ 'displayName' => get_class($job), 'job' => 'IlluminateQueueCallQueuedHandler@call', 'maxTries' => isset($job->tries) ? $job->tries : null, 'timeout' => isset($job->timeout) ? $job->timeout : null, 'data' => [ 'commandName' => get_class($job), 'command' => serialize(clone $job) ], 'id' => Str::random(32), 'attempts' => 0 ]; } }}
创建助手函数 注意我的内容哦 按需修改
if (!function_exists('producerPushData')) { /** * 投递信息 * @param ProducerMessageInterface $message 消息 * @param string $routingKey 默认 default * @param string $exchange 所投入的queue * @param bool $confirm 是否需要确认 * @param int $timeout 超时时间 * @return bool * @throws Throwable */ function producerPushData($message, $routingKey = 'default', $exchange = '', bool $confirm = false, int $timeout = 5) { $exchange = !empty($exchange) ? $exchange : env('RABBITMQ_EXCHANGE_NAME', 'sweetheart'); return make(Producer::class)->produce($message, $routingKey, $exchange, $confirm, $timeout); }}
使用方式 注意需要和laravel/lumen 保持同样的命名空间哦
有话要说