The developer needs to easily create asynchronous functionality, such as sending an email apart from the main request, and does not want to maintain the infrastructure needed to do that.
While traditional PHP applications have been written synchronously, modern scalability requirements and demand for intensive data or functionality have pushed traditional development techniques to, and perhaps beyond, its proper limits.
To help solve this problem we built the Zend Job Queue which allows you to execute jobs asynchronously to offload that processing to another process or even another machine. Jobs are typically defined as a URL that contains the functionality you intended to execute and called using the Job Queue API. The asynchronous code is written the same way you would write any other code since it's called as a script. The API call for Zend Server (more details) is quite simple.
$q->createHttpJob(
'http://localhost/sendemail.php',
array(
'email' => $_POST['email']
)
);The script at http://localhost/sendemail.php is relatively simply
$params = ZendJobQueue::getCurrentJobParams();
if (isset($params['email'])) {
mail($params['email'], 'Welcome', 'Welcome to my nightmare');
ZendJobQueue::setCurrentJobStatus(ZendJobQueue::OK);
}
If you prefer a more structured approach you are free to do so as well. You can either create your own dispatching system or use this one on GitHub. More details are available on ESchrade. Building an asynchronous task can be done using the following code:
use com\zend\jobqueue\JobAbstract;
class SendEmail extends JobAbstract
{
private $to;
private $message;
public function __construct($to, $message)
{
$this->to = $to;
$this->message = $message;
}
public function job()
{
$mail = new \Zend_Mail();
$mail->addTo($this->to);
$mail->setBodyHtml($this->message);
$mail->send();
}
}
The code is executed in the mainline script as follows:
$job = new SendEmail('you@yourdomain', 'Welcome here!');
$response = $job->execute();

