定制 elielelie/laravel-activemq 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

elielelie/laravel-activemq

Composer 安装命令:

composer require elielelie/laravel-activemq

包简介

Laravel ActiveMQ driver

README 文档

README

Total Downloads Latest Stable Version License

This package enables usage of ActiveMQ (Stomp) driver for queueing natively inside Laravel.

Installation

composer require elielelie/laravel-activemq

In order to connect it to your queue you need to change queue connection driver in .env file:

QUEUE_CONNECTION=activemq

Variables:

ACTIVEMQ_PROTOCOL      protocol (defaults to TCP)
ACTIVEMQ_HOST          broker host (defaults to 127.0.0.1)
ACTIVEMQ_PORT          port where STOMP is exposed in your broker (defaults to 61613)
ACTIVEMQ_USERNAME      broker username (defaults to admin)
ACTIVEMQ_PASSWORD      broker password (defaults to admin)

You can subscribe to queues to read from or to write to with:

ACTIVEMQ_QUEUE=...
default;email 

You can see all other available .env variables, their defaults and usage explanation within the config file

You can publish the configuration file by running:

$ php artisan vendor:publish --provider="Elielelie\ActiveMQ\ActiveMQServiceProvider"

Usage

Dispatching Jobs

You can dispatch jobs to ActiveMQ just like any other Laravel queue driver:

use App\Jobs\ProcessOrder;

// Dispatch to the default queue
ProcessOrder::dispatch($order);

// Dispatch to a specific queue
ProcessOrder::dispatch($order)->onQueue('orders');

Delayed Jobs (Scheduling)

The driver supports delayed jobs using the later method or delay() helper:

// Delay job execution by 10 minutes
ProcessOrder::dispatch($order)->delay(now()->addMinutes(10));

Message Persistence

By default, messages are sent according to the ACTIVEMQ_PERSISTENT environment variable.

  • To ensure messages survive a broker restart, set ACTIVEMQ_PERSISTENT=true.
  • If you need to override headers for a specific job, you can implement the HasHeaders contract in your Job class.

Health Check

You can verify the connection to the ActiveMQ broker using the healthCheck method on the connection:

use Illuminate\Support\Facades\Queue;

$isConnected = Queue::connection('activemq')->healthCheck();

if (!$isConnected) {
    // Handle connection failure
}

Failed jobs

For the sake of simplicity and brevity ActiveMQJob class is defined in a way to utilize Laravel tries and backoff properties out of the box (official documentation).

Upon failure, jobs will retry 5 times before being written to failed_jobs table.

Each subsequent attempt will be tried in attempt^2 seconds, meaning if it is a third attempt, it will retry in 9s after the previous job failure.

Note that job properties by default have precedence over CLI commands, thus with these defaults in place the flags --tries and --backoff will be overridden.

You can turn off this behavior with following .env variables:

  • ACTIVEMQ_AUTO_TRIES - defaults to true. Set to false to revert to Laravel default of 0 retries.
  • ACTIVEMQ_AUTO_BACKOFF - defaults to true. Set to false to revert to Laravel default of 0s backoff.
  • ACTIVEMQ_BACKOFF_MULTIPLIER - defaults to 2. Does nothing if ACTIVEMQ_AUTO_BACKOFF is turned off. Increase to make even bigger interval between two failed jobs.

Job will be re-queued to the queue it came from.

Laravel Horizon Support

This package now supports Laravel Horizon for advanced queue management with multiple workers and auto-scaling capabilities.

Configuration

To enable Horizon support, add these environment variables:

ACTIVEMQ_HORIZON_ENABLED=true
ACTIVEMQ_HORIZON_ISOLATE=true

Horizon Configuration Example

Add this configuration to your config/horizon.php:

'environments' => [
    'production' => [
        'activemq-default' => [
            'connection' => 'activemq',
            'queue' => ['default'],
            'balance' => 'simple',
            'processes' => 3,
        ],
        'activemq-emails' => [
            'connection' => 'activemq', 
            'queue' => ['email'],
            'balance' => 'auto',
            'processes' => 2,
            'maxProcesses' => 5,
        ],
        'activemq-notifications' => [
            'connection' => 'activemq',
            'queue' => ['notifications'],
            'balance' => 'simple', 
            'processes' => 1,
        ],
    ],
],

Multiple Queue Setup

You can define multiple queues in your environment configuration:

ACTIVEMQ_QUEUE=default;email;notifications

Each supervisor in Horizon can process specific queues, allowing for:

  • Worker Isolation: Different workers for different queue types
  • Auto-Scaling: Automatic worker scaling based on queue load
  • Priority Processing: Different process counts and strategies per queue

Balancing Strategies

  • simple: Fixed number of workers distributed evenly
  • auto: Dynamic worker allocation based on queue load
  • false: Process queues in order without balancing

Logs

Logs are turned off by default. You can include them by setting env key ACTIVEMQ_LOGS=true.

In case you want to change the default log manager, it can be done in the log-activemq config file. The new log manager must extend Illuminate\Log\LogManager.

elielelie/laravel-activemq 适用场景与选型建议

elielelie/laravel-activemq 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.06k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 03 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「queue」 「php」 「stomp」 「activeMQ」 「driver」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 elielelie/laravel-activemq 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 elielelie/laravel-activemq 我们能提供哪些服务?
定制开发 / 二次开发

基于 elielelie/laravel-activemq 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-04