@@ -58,7 +58,14 @@ class Kernel extends BaseKernel
58
58
59
59
## Usage
60
60
61
- First, you have to configure a transport layer and set one to be default.
61
+ First, you have to configure a transport layer.
62
+ You can optionally configure multiple transports if you want to. One of them will automatically become the default,
63
+ based on the following:
64
+ 1 . If there is a transport named ` default ` , then it will become the default.
65
+ 2 . First one specified otherwise.
66
+
67
+ Default transport's services will be available to you in the usual Symfony container under their respective class
68
+ interfaces (see below)
62
69
63
70
``` yaml
64
71
# app/config/config.yml
@@ -67,26 +74,47 @@ enqueue:
67
74
default :
68
75
transport : " amqp:"
69
76
client : ~
77
+ some_other_transport :
78
+ transport : " amqp:"
79
+ client : ~
70
80
` ` `
71
81
72
- Once you configured everything you can start producing messages:
82
+ Once you configured everything you can start producing messages.
83
+ As stated previously, default transport services are available in container. Here we are using ` ProducerInterface` to
84
+ produce message to the `default` transport.
73
85
74
86
` ` ` php
75
87
<?php
88
+ use Enqueue\C lient\M essage;
76
89
use Enqueue\C lient\P roducerInterface;
77
90
78
91
/** @var ProducerInterface $producer **/
79
92
$producer = $container->get(ProducerInterface::class);
80
93
94
+ // If you want a different producer than default (for example the other specified in sample above) then use
95
+ // $producer = $container->get('enqueue.client.some_other_transport.producer');
81
96
82
97
// send event to many consumers
83
98
$producer->sendEvent('aFooTopic', 'Something has happened');
99
+ // You can also pass an instance of Enqueue\C lient\M essage as second argument if you need more flexibility.
100
+ $properties = [];
101
+ $headers = [];
102
+ $message = new Message('Message body', $properties, $headers);
103
+ $producer->sendEvent('aBarTopic', $message);
84
104
85
105
// send command to ONE consumer
86
106
$producer->sendCommand('aProcessorName', 'Something has happened');
87
107
` ` `
88
108
89
- To consume messages you have to first create a message processor:
109
+ To consume messages you have to first create a message processor.
110
+
111
+ Example below shows how to create a Processor that will receive messages from `aFooTopic` topic (and only that one).
112
+ It assumes that you're using default Symfony services configuration and this class is
113
+ [autoconfigured](https://symfony.com/doc/current/service_container.html#the-autoconfigure-option). Otherwise you'll
114
+ have to tag it manually. This is especially true if you're using multiple transports : if left autoconfigured, processor
115
+ will be attached to the default transport only.
116
+
117
+ Note : Topic in enqueue and topic on some transports (for example Kafka) are two different things.
90
118
91
119
` ` ` php
92
120
<?php
@@ -113,13 +141,16 @@ class FooProcessor implements Processor, TopicSubscriberInterface
113
141
}
114
142
` ` `
115
143
116
- Register it as a container service and subscribe to the topic:
144
+ Register it as a container service. Subscribe it to the topic if you are not using autowiring.
117
145
118
146
` ` ` yaml
119
147
foo_message_processor:
120
148
class: 'FooProcessor'
121
149
tags:
122
150
- { name: 'enqueue.topic_subscriber' }
151
+ # Use the variant below to attach to a specific client
152
+ # Also note that if you don't disable autoconfigure, above tag will be applied automatically for default client
153
+ # - { name: 'enqueue.topic_subsciber', client: 'some_other_transport' }
123
154
` ` `
124
155
125
156
Now you can start consuming messages :
@@ -128,6 +159,13 @@ Now you can start consuming messages:
128
159
$ ./bin/console enqueue:consume --setup-broker -vvv
129
160
` ` `
130
161
162
+ You can select a specific client for consumption :
163
+
164
+ ` ` ` bash
165
+ $ ./bin/console enqueue:consume --setup-broker --client="some_other_transport" -vvv
166
+ ` ` `
167
+
168
+
131
169
_**Note** : Add -vvv to find out what is going while you are consuming messages. There is a lot of valuable debug info there._
132
170
133
171
0 commit comments