PHP code example of printed / rabbitmq-queue-bundle
1. Go to this page and download the library: Download printed/rabbitmq-queue-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace AppBundle\Queue\Payload;
use Printed\Bundle\Queue\Queue\AbstractQueuePayload;
use Symfony\Component\Validator\Constraints as Assert;
/**
* {@inheritdoc}
*/
class ExampleQueuePayload extends AbstractQueuePayload
{
/**
* @Assert\NotNull()
* @Assert\Type(type="integer")
*/
protected $data;
/**
* {@inheritdoc}
*/
public static function getExchangeName(): string
{
return 'example_exchange';
}
// public function getData()
// public function setData()
// ..
}
class MyServiceClass
{
private $queueTaskDispatcher;
public function __construct(QueueTaskDispatcher $queueTaskDispatcher)
{
$this->queueTaskDispatcher = $queueTaskDispatcher;
}
public function doSomething()
{
// do something..
$payload = new ExampleQueuePayload;
$payload->setData($myData);
$this->queueTaskDispatcher->dispatch($payload);
}
}
namespace AppBundle\Queue\Consumer;
use AppBundle\Queue\Payload\ExampleQueuePayload;
use Printed\Bundle\Queue\Exception\Consumer\QueueFatalErrorException;
use Printed\Bundle\Queue\Queue\AbstractQueueConsumer;
use Printed\Bundle\Queue\Queue\AbstractQueuePayload;
use Symfony\Component\HttpFoundation\Request;
/**
* {@inheritdoc}
*/
class ExampleQueueConsumer extends AbstractQueueConsumer
{
/**
* {@inheritDoc}
*/
public static function getSubscribedServices(): array
{
return array_merge(parent::getSubscribedServices(), [
'some_service' => MySomeService::class,
]);
}
/**
* {@inheritdoc}
*
* @param ExampleQueuePayload $payload
*/
public function run(AbstractQueuePayload $payload): bool
{
// You hava access to all the standard things ..
$someService = $this->locator->get('some_service');
$usefulParameter = $this->containerParameters->get('my_app.useful_parameter');
$this->em->persist($entity);
$this->logger->info('Oh noes');
// Setting a task response data.
$this->task->setResponseData(['something' => true]);
// Do lots of things ..
// And a few more things ..
return self::TASK_COMPLETE;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.