1. Go to this page and download the library: Download mnk1985/amqp-tasks-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/ */
mnk1985 / amqp-tasks-bundle example snippets
namespace App\Tasks;
use AmqpTasksBundle\DTO\DTOSerializerInterface;
use AmqpTasksBundle\Tasks\AbstractTask;
class TestTask extends AbstractTask
{
private const QUEUE_NAME = 'test_queue';
public function getQueueName(): string
{
return self::QUEUE_NAME;
}
public function getDTOSerializer(): DTOSerializerInterface
{
return new DTOSerializer();
}
}
namespace App\Tasks;
use AmqpTasksBundle\Exception\InvalidDTOException;
use AmqpTasksBundle\Tasks\AbstractTaskHandler;
class TestTaskHandler extends AbstractTaskHandler
{
/**
* @param TestDTO $message
* @return bool
*/
public function process(string $dto): bool
{
if (!$dto instanceof TestDTO) {
throw new InvalidDTOException();
}
if ($dto->getFieldA() && $dto->getFieldB()) {
return true;
}
return false;
}
}
namespace App\Tasks;
class TestDTO
{
private $fieldA;
private $fieldB;
public function __construct($fieldA = null, $fieldB = null)
{
$this->fieldA = $fieldA;
$this->fieldB = $fieldB;
}
public function getFieldA(): ?string
{
return $this->fieldA;
}
public function setFieldA(?string $fieldA): self
{
$this->fieldA = $fieldA;
return $this;
}
public function getFieldB(): ?int
{
return $this->fieldB;
}
public function setFieldB(?int $fieldB): self
{
$this->fieldB = $fieldB;
return $this;
}
}
namespace App\Tasks;
use AmqpTasksBundle\DTO\DTOSerializerInterface;
use AmqpTasksBundle\Exception\InvalidDTOException;
class DTOSerializer implements DTOSerializerInterface
{
/**
* @param array $dto
* @return string
*/
public function convertToString($dto): string
{
if (!is_array($dto)) {
throw new InvalidDTOException('dto should be array');
}
$dto = new TestDTO(
$dto['a'] ?? null,
$dto['b'] ?? null
);
$fields = [
'fieldA' => $dto->getFieldA(),
'fieldB' => $dto->getFieldB(),
];
return json_encode($fields);
}
/**
* @param string $data
* @return TestDTO
*/
public function createDTO(string $data)
{
$fields = json_decode($data, true);
$new = new TestDTO();
$new->setFieldA($fields['fieldA'] ?? null);
$new->setFieldB($fields['fieldB'] ?? null);
return $new;
}
}
namespace App\Controller;
use AmqpTasksBundle\Manager\TaskManagerInterface;
use App\Tasks\TestTask;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class TasksController extends Controller
{
/**
* @Route("/tasks/publish", name="publish")
*/
public function publishAction(TaskManagerInterface $manager, TestTask $task)
{
$manager->publish($task->getQueueName(), ['a' => 1, 'b' => 2]);
return new Response('published');
}
/**
* @Route("/tasks/consume", name="consume")
*/
public function consumeAction(TaskManagerInterface $manager, TestTask $task)
{
$manager->consume($task->getQueueName(), [
'iterations' => 1,
'attempts' => 2,
'delay' => 1,
'verbose' => false,
]);
return new Response('consumed');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.