PHPackages                             creode/magento2-messagequeue - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. creode/magento2-messagequeue

ActiveLibrary[Queues &amp; Workers](/categories/queues)

creode/magento2-messagequeue
============================

037PHP

Since Nov 14Pushed 8y ago1 watchersCompare

[ Source](https://github.com/creode/magento2-messagequeue)[ Packagist](https://packagist.org/packages/creode/magento2-messagequeue)[ RSS](/packages/creode-magento2-messagequeue/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Magento 2 Message Queue
=======================

[](#magento-2-message-queue)

A simple system for passing and processing messages in Magento 2.

Comes with classes to queue messages during normal site usage, and to process queues in cron jobs

There is only one queue, but messages can be grouped using the `setGroup()` method.

Usage
-----

[](#usage)

### Adding a message to the queue

[](#adding-a-message-to-the-queue)

```
# inject a queue factory into your constructor
public function __construct(
    \Creode\MessageQueue\Model\MessagequeueFactory $messageQueueFactory
) {
    $this->messageQueueFactory = $messageQueueFactory;
}

function createAMessage() {
    $message = $this->messageQueueFactory->create();

    # create messages with waiting status so they'll be picked up when processing the queue
    $message->setStatus(\Creode\MessageQueue\Model\Messagequeue::STATUS_WAITING)
        ->setMessage('a message for Creodes group')
        ->setGroup(\Your\Module\Cron\ProcessCreodesGroup::MESSAGE_GROUP) # it's recommended to use the class constant for message group
        ->setCreationTime(time())
        ->save();
}
```

### Processing messages in a cron job

[](#processing-messages-in-a-cron-job)

#### Your/Module/etc/crontab.xml

[](#yourmoduleetccrontabxml)

```

            */5 * * * *

```

#### Your/Module/Cron/ProcessCreodesGroup.php

[](#yourmodulecronprocesscreodesgroupphp)

When extending `Creode\MessageQueue\Cron\ProcessQueue` your cron job class only needs a single method - `processMessage`

Messages that are picked up have the status `waiting`

Messages are automatically marked as `processing` when the job picks them up. Further runs will not pick up messages that are already being processed.

The message will be marked `completed` if the `processMessage` method completes without error.

If an exception is thrown in `processMessage`, the message processing will be aborted and the message added back into the queue by setting to the `waiting` status.

Messages can fail up to 5 times, at which point they are marked `failed` and will not be processed in future runs.

```
