Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Tutorial - Hello World Queue #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions current/config/Shared/config_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,9 @@
$config[OauthConstants::PRIVATE_KEY_PATH] = 'file://';
$config[OauthConstants::PUBLIC_KEY_PATH] = 'file://';
$config[OauthConstants::ENCRYPTION_KEY] = '';

$config[QueueConstants::QUEUE_ADAPTER_CONFIGURATION] = [
'hello' => [
QueueConfig::CONFIG_QUEUE_ADAPTER => \Spryker\Client\RabbitMq\Model\RabbitMqAdapter::class,
],
];
2 changes: 2 additions & 0 deletions current/src/Pyz/Client/RabbitMq/RabbitMqConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use ArrayObject;
use Generated\Shared\Transfer\RabbitMqOptionTransfer;
use Pyz\Shared\HelloWorldQueue\HelloWorldQueueConstants;
use Spryker\Client\RabbitMq\Model\Connection\Connection;
use Spryker\Client\RabbitMq\RabbitMqConfig as SprykerRabbitMqConfig;
use Spryker\Shared\AvailabilityStorage\AvailabilityStorageConstants;
Expand Down Expand Up @@ -43,6 +44,7 @@ protected function getQueueOptions()
$queueOptionCollection->append($this->createQueueOption(CategoryPageSearchConstants::CATEGORY_SYNC_SEARCH_QUEUE, CategoryPageSearchConstants::CATEGORY_SYNC_SEARCH_ERROR_QUEUE));
$queueOptionCollection->append($this->createQueueOption(CmsPageSearchConstants::CMS_SYNC_SEARCH_QUEUE, CmsPageSearchConstants::CMS_SYNC_SEARCH_ERROR_QUEUE));
$queueOptionCollection->append($this->createQueueOption(ProductPageSearchConstants::PRODUCT_SYNC_SEARCH_QUEUE, ProductPageSearchConstants::PRODUCT_SYNC_SEARCH_ERROR_QUEUE));
$queueOptionCollection->append($this->createQueueOption(HelloWorldQueueConstants::HELLO_WOLRD_QUEUE, HelloWorldQueueConstants::HELLO_WOLRD_ERROR_QUEUE));
$queueOptionCollection->append(
$this->createQueueOption(
$this->get(LogConstants::LOG_QUEUE_NAME),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* This file is part of the Spryker Suite.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace Pyz\Shared\HelloWorldQueue;

class HelloWorldQueueConstants
{
/** @var string */
public const HELLO_WOLRD_QUEUE = 'hello';

/** @var string */
public const HELLO_WOLRD_ERROR_QUEUE = 'hello.error';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* This file is part of the Spryker Suite.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace Pyz\Yves\HelloWorldQueue\Controller;

use Generated\Shared\Transfer\QueueReceiveMessageTransfer;
use Generated\Shared\Transfer\QueueSendMessageTransfer;
use Generated\Shared\Transfer\RabbitMqConsumerOptionTransfer;
use Spryker\Yves\Kernel\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;

/**
* @method \Pyz\Yves\HelloWorldQueue\HelloWorldQueueFactory getFactory()
*/
class IndexController extends AbstractController
{
/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function indexAction(Request $request)
{
return [];
}

/**
* @return array
*/
public function sendAction(): array
{
$queueSendTransfer = new QueueSendMessageTransfer();
$queueSendTransfer->setBody('Hello, World!');

$queueClient = $this->getFactory()->getQueueClient();
$queueClient->sendMessage('hello', $queueSendTransfer);

return [
'success' => true,
];
}

/**
* @return array
*/
public function receiveAction()
{
$queueClient = $this->getFactory()->getQueueClient();

$queueReceiveMessageTransfer = $queueClient->receiveMessage('hello', $this->createReceiverOption());

return $this->getMessage($queueReceiveMessageTransfer);
}

/**
* @return array
*/
protected function createReceiverOption()
{
$rabbitmqReceiveOptionTransfer = new RabbitMqConsumerOptionTransfer();
$rabbitmqReceiveOptionTransfer->setNoAck(false); /* it prevents the queue to delete the message until we send the `acknowledging` */

return [
'rabbitmq' => $rabbitmqReceiveOptionTransfer,
];
}

/**
* @param \Generated\Shared\Transfer\QueueReceiveMessageTransfer $queueReceiveMessageTransfer
*
* @return array
*/
protected function getMessage(QueueReceiveMessageTransfer $queueReceiveMessageTransfer): array
{

$success = false;
$message = '';

if (method_exists($queueReceiveMessageTransfer->getQueueMessage(), 'getBody')) {
$message = $queueReceiveMessageTransfer->getQueueMessage()->getBody();
$success = true;
}

return [
'message' => $message,
'success' => $success,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* This file is part of the Spryker Suite.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace Pyz\Yves\HelloWorldQueue;

use Spryker\Yves\Kernel\AbstractBundleDependencyProvider;
use Spryker\Yves\Kernel\Container;

class HelloWorldQueueDependencyProvider extends AbstractBundleDependencyProvider
{
public const CLIENT_QUEUE = 'CLIENT_QUEUE';

/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
public function provideDependencies(Container $container): Container
{
$container[self::CLIENT_QUEUE] = function (Container $container) {
return $container->getLocator()->queue()->client();
};

return $container;
}
}
22 changes: 22 additions & 0 deletions current/src/Pyz/Yves/HelloWorldQueue/HelloWorldQueueFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* This file is part of the Spryker Suite.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace Pyz\Yves\HelloWorldQueue;

use Spryker\Client\Queue\QueueClientInterface;
use Spryker\Yves\Kernel\AbstractFactory;

class HelloWorldQueueFactory extends AbstractFactory
{
/**
* @return \Spryker\Client\Queue\QueueClientInterface
*/
public function getQueueClient(): QueueClientInterface
{
return $this->getProvidedDependency(HelloWorldQueueDependencyProvider::CLIENT_QUEUE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* This file is part of the Spryker Suite.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace Pyz\Yves\HelloWorldQueue\Plugin\Provider;

use Silex\Application;
use SprykerShop\Yves\ShopApplication\Plugin\Provider\AbstractYvesControllerProvider;

class HelloWorldQueueControllerProvider extends AbstractYvesControllerProvider
{
const HELLOWORLDQUEUE_INDEX = 'helloworldqueue-index';
const HELLOWORLDQUEUE_SEND = 'helloworldqueue-send';
const HELLOWORLDQUEUE_RECEIVE = 'helloworldqueue-receive';

/**
* @param \Silex\Application $app
*
* @return void
*/
protected function defineControllers(Application $app)
{
$this->createGetController(
'/hello-world-queue',
static::HELLOWORLDQUEUE_INDEX,
'HelloWorldQueue',
'Index',
'index'
);
$this->createGetController(
'/hello-world-queue/send',
static::HELLOWORLDQUEUE_SEND,
'HelloWorldQueue',
'Index',
'send'
);
$this->createGetController(
'/hello-world-queue/receive',
static::HELLOWORLDQUEUE_RECEIVE,
'HelloWorldQueue',
'Index',
'receive'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends template('page-layout-main') %}

{% block content %}
<div class="row">
<div class="small-12 columns">
<h2>Hello Spryker!</h2>
</div>
</div>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends template('page-layout-main') %}

{% block content %}
<div class="row">
<div class="small-12 columns">
<h2>{{ message }}</h2>
</div>
</div>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends template('page-layout-main') %}

{% block content %}
<div class="row">
<div class="small-12 columns">
<h2>{{ dump(success) }}</h2>
</div>
</div>
{% endblock %}
2 changes: 2 additions & 0 deletions current/src/Pyz/Yves/ShopApplication/YvesBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Pyz\Yves\ShopApplication;

use Pyz\Yves\ExampleProductSalePage\Plugin\Provider\ExampleProductSaleControllerProvider;
use Pyz\Yves\HelloWorldQueue\Plugin\Provider\HelloWorldQueueControllerProvider;
use Silex\Provider\FormServiceProvider;
use Silex\Provider\HttpFragmentServiceProvider;
use Silex\Provider\RememberMeServiceProvider;
Expand Down Expand Up @@ -206,6 +207,7 @@ protected function getControllerProviderStack($isSsl)
new CartToShoppingListWidgetControllerProvider($isSsl), #ShoppingListFeature
new ShoppingListWidgetControllerProvider($isSsl), #ShoppingListFeature
new CompanyUserInvitationPageControllerProvider($isSsl), #BulkImportCompanyUserInvitationsFeature
new HelloWorldQueueControllerProvider($isSsl),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* This file is part of the Spryker Suite.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace Pyz\Zed\HelloWorldQueue\Communication\Plugin;

use Exception;
use Spryker\Zed\Queue\Dependency\Plugin\QueueMessageProcessorPluginInterface;

class HelloWorldQueueMessageProcessorPlugin implements QueueMessageProcessorPluginInterface
{
/**
* @param array $queueMessageTransfers
*
* @return array
*/
public function processMessages(array $queueMessageTransfers)
{
foreach ($queueMessageTransfers as $queueMessageTransfer) {
try {
/* Sample Code */
echo sprintf("[x] %s \r\n", $queueMessageTransfer->getQueueMessage()->getBody());
$queueMessageTransfer->setAcknowledge(true); /* we acknowledge the message to remove it from the queue */
} catch (Exception $e) {
$queueMessageTransfer->setHasError(true); /* we mark the message as an error, it will call errorHandling() for provided queue adapter on this message */
}
}

return $queueMessageTransfers;
}

/**
* @return int
*/
public function getChunkSize()
{
/* Sample Chunk Size */
return 10;
}
}
3 changes: 3 additions & 0 deletions current/src/Pyz/Zed/Queue/QueueConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ protected function getQueueReceiverOptions()
Config::get(LogConstants::LOG_QUEUE_NAME) => [
static::RABBITMQ => $this->getRabbitMqQueueConsumerOptions(),
],
'hello' => [
static::RABBITMQ => $this->getRabbitMqQueueConsumerOptions(),
],
];
}

Expand Down
3 changes: 3 additions & 0 deletions current/src/Pyz/Zed/Queue/QueueDependencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Pyz\Zed\Queue;

use Pyz\Shared\HelloWorldQueue\HelloWorldQueueConstants;
use Pyz\Zed\HelloWorldQueue\Communication\Plugin\HelloWorldQueueMessageProcessorPlugin;
use Spryker\Shared\AvailabilityStorage\AvailabilityStorageConstants;
use Spryker\Shared\CategoryPageSearch\CategoryPageSearchConstants;
use Spryker\Shared\CategoryStorage\CategoryStorageConstants;
Expand Down Expand Up @@ -49,6 +51,7 @@ protected function getProcessorMessagePlugins(Container $container)
CmsPageSearchConstants::CMS_SYNC_SEARCH_QUEUE => new SynchronizationSearchQueueMessageProcessorPlugin(),
CategoryPageSearchConstants::CATEGORY_SYNC_SEARCH_QUEUE => new SynchronizationSearchQueueMessageProcessorPlugin(),
ProductPageSearchConstants::PRODUCT_SYNC_SEARCH_QUEUE => new SynchronizationSearchQueueMessageProcessorPlugin(),
HelloWorldQueueConstants::HELLO_WOLRD_QUEUE => new HelloWorldQueueMessageProcessorPlugin(),
];
}
}