Skip to content
Snippets Groups Projects
Commit 176ae2c9 authored by Peter Vanhee's avatar Peter Vanhee
Browse files

Merge pull request #14 from comicrelief/example

Example
parents babaa5a8 6640a073
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,10 @@ Requirements
the file, then save it.
* update your `vendor` directory by typing `composer update`.
Example module
--------------
To test RabbitMQ from your Drupal site, enable the `rabbitmq_example` module and following the instructions from the README.
Installation
------------
......
## Example RabbitMQ module
This module shows the integration of the rabbitmq module in a custom form.
You need to add this to your `settings.php`
$settings['queue_service_queue1'] = 'queue.rabbitmq';
to make sure that `queue1` will use a rabbit message queue rather than the default database queue.
Now, make sure RabbitMQ is running in the background and go to
/rabbitmq_example
to submit an email address to the queue.
exchanges:
exchange1:
type: 'direct'
passive: false
durable: true
auto_delete: false
internal: false
nowait: false
queues:
queue1:
passive: false
durable: false
exclusive: false
auto_delete: true
nowait: false
routing_keys:
- 'exchange1.queue1'
queue2:
passive: false
durable: true
exclusive: false
auto_delete: true
nowait: false
routing_keys:
- 'exchange1.queue2'
name: RabbitMQ Example
type: module
hidden: TRUE
description: RabbitMQ Integration for Drupal
core: 8.x
package: Testing
dependencies:
- rabbitmq
rabbitmq_example_form:
path: '/rabbitmq_example'
defaults:
_form: '\Drupal\rabbitmq_example\Form\ExampleForm'
_title: 'RabbitMQ Example Form'
requirements:
_permission: 'access simple page'
<?php
/**
* @file
* Contains \Drupal\rabbitmq_example\Form\ContributeForm.
*/
namespace Drupal\rabbitmq_example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Contribute form.
*/
class ExampleForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'rabbitmq_example_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['email'] = [
'#type' => 'email',
'#title' => $this->t('Send an email address to the queue.'),
];
$form['show'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Get the data you want to send to the queue.
$data = $form_state->getValue('email');
// Get the queue config and send it to the data to the queue.
$queue_name = 'queue1';
$queue_factory = \Drupal::service('queue');
$queue = $queue_factory->get($queue_name);
$queue->createItem($data);
// Send some feedback.
drupal_set_message(
$this->t('You sent the following data to queue @queue: @email', [
'@queue' => $queue_name,
'@email' => $form_state->getValue('email'),
])
);
}
}
......@@ -71,12 +71,12 @@ abstract class QueueBase {
*/
public function __construct($name, Connection $connection,
ModuleHandlerInterface $modules, LoggerInterface $logger) {
$this->options = ['name' => $name];
// Check our active storage to find the the queue config.
$config = \Drupal::config('rabbitmq.config');
$queues = $config->get('queues');
if ($queues && isset($queues[$name])) {
$this->options = ['name' => $name];
if (isset($queues[$name])) {
$this->options += $queues[$name];
}
......@@ -84,12 +84,11 @@ abstract class QueueBase {
$this->connection = $connection;
$this->logger = $logger;
$this->modules = $modules;
// Declare any exchanges required if configured.
$exchanges = $config->get('exchanges');
if ($exchanges) {
foreach ($exchanges as $name => $exchange) {
$this->getChannel()->exchange_declare(
$this->connection->getConnection()->channel()->exchange_declare(
$name,
isset($exchange['type']) ? $exchange['type'] : 'direct',
isset($exchange['passive']) ? $exchange['passive'] : FALSE,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment