-
Notifications
You must be signed in to change notification settings - Fork 0
04.Message
Messages refer to JSON objects that are added to a queue. Every message must extend the \PHPQueueManager\PHPQueueManager\Queue\Message
class and implement the \PHPQueueManager\PHPQueueManager\Queue\MessageInterface
interface.
If you do not want to customize the messages, you can create and use objects directly with \PHPQueueManager\PHPQueueManager\Queue\Message
.
$message = new \PHPQueueManager\PHPQueueManager\Queue\Message();
$message->setPayload([
'type' => 'mail',
'to_mail' => 'example@example.com',
]);
But there are nice features that you will want to customize the message classes.
namespace App\Messages;
use \PHPQueueManager\PHPQueueManager\Queue\MessageInterface;
use \PHPQueueManager\PHPQueueManager\Queue\Message;
class PaymentMessage extends Message implements MessageInterface
{
public function __construct()
{
parent::__construct();
/**
* Indicates the latest time when the message can be processed.
* This should be a string that can be parsed correctly with PHP's strtotime().
* The recommended sample value definition format is as follows;
*
* date("c", strtotime("+10 days"))
*
* Default: NULL. NULL indicates that there is no timeout period.
*/
$this->properties['ttl'] = null;
/**
* How many attempts should be made before a successful response is received?
* Defines how many times to try before writing a dead letter for the message.
*
* Default: 1
*/
$this->properties['attempt'] = 1;
}
public function retryNotification(): bool
{
/**
* The message fails and is requeued to be tried again; this method is called.
*
* This allows you to log or take other actions whenever you want.
*
* For error message see; $this->properties['error']
* To see how many attempts it is see; $this->properties['try']
*/
return true;
}
public function deadLetterNotification(): bool
{
/**
* This method is called when the message fails permanently and is put into the dead letter queue.
*
* This allows you to log or take other actions whenever you want.
*
* For error message see; $this->properties['error']
* To see how many attempts it is see; $this->properties['try']
*/
return true;
}
}
All ordinary messages are processed and consumed by workers called consumers. However, in some special cases, you may want a message to be processed and consumed entirely by a specific worker. In this case, you can use Job Message.
Unlike regular messages, Job Message classes must implement the \PHPQueueManager\PHPQueueManager\Queue\JobMessageInterface
interface.
namespace App\Messages;
use \PHPQueueManager\PHPQueueManager\Queue\JobMessageInterface;
use \PHPQueueManager\PHPQueueManager\Queue\Message;
class PaymentMessage extends Message implements JobMessageInterface
{
public function __construct()
{
parent::__construct();
$this->properties['ttl'] = null;
$this->properties['attempt'] = 1;
}
public function worker(MessageInterface $message): bool
{
try {
$payload = $message->getPayload();
sleep(2); // Send Bank Payment Request
/** @var $result Payment Status*/
if ($result) {
return true;
} else {
return false;
}
} catch (\Exception $e) {
return false;
}
}
public function retryNotification(): bool
{
return true;
}
public function deadLetterNotification(): bool
{
return true;
}
}