@@ -61,6 +61,9 @@ Add connection to `config/queue.php`:
6161 'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
6262 'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
6363 ],
64+ 'queue' => [
65+ 'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
66+ ],
6467 ],
6568
6669 /*
@@ -162,6 +165,92 @@ When you want to instruct RabbitMQ to reroute failed messages to a exchange or a
162165],
163166```
164167
168+ ### Use your own RabbitMQJob class
169+ Sometimes you have to work with messages published by another application.
170+ Those messages probably won't respect Laravel's job payload schema.
171+ The problem with these messages is that, Laravel workers won't be able to determine the actual job or class to execute.
172+
173+ You can extend the build-in ` RabbitMQJob::class ` and within the queue connection config, you can define your own class.
174+ When you specify an ` job ` key in the config, with your own class name, every message retrieved from the broker will get wrapped by your own class.
175+
176+ An example for the config:
177+
178+ ``` php
179+ 'connections' => [
180+ // ...
181+
182+ 'rabbitmq' => [
183+ // ...
184+
185+ 'options' => [
186+ 'queue' => [
187+ // ...
188+
189+ 'job' => \App\Queue\Jobs\RabbitMQJob::class,
190+ ],
191+ ],
192+ ],
193+
194+ // ...
195+ ],
196+ ```
197+
198+ An example of your own job class:
199+
200+ ``` php
201+ <?php
202+
203+ namespace App\Queue\Jobs;
204+
205+ use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
206+
207+ class RabbitMQJob extends BaseJob
208+ {
209+
210+ /**
211+ * Fire the job.
212+ *
213+ * @return void
214+ */
215+ public function fire()
216+ {
217+ $payload = $this->payload();
218+
219+ $class = WhatheverClassNameToExecute::class;
220+ $method = 'handle';
221+
222+ ($this->instance = $this->resolve($class))->{$method}($this, $payload);
223+ }
224+ }
225+
226+ ```
227+
228+ Or maybe you want to add extra properties to the payload:
229+
230+ ``` php
231+ <?php
232+
233+ namespace App\Queue\Jobs;
234+
235+ use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
236+
237+ class RabbitMQJob extends BaseJob
238+ {
239+ /**
240+ * Get the decoded body of the job.
241+ *
242+ * @return array
243+ */
244+ public function payload()
245+ {
246+ return [
247+ 'job' => 'WhatheverFullyQualifiedClassNameToExecute@handle',
248+ 'data' => json_decode($this->getRawBody(), true)
249+ ];
250+ }
251+ }
252+ ```
253+
165254## Laravel Usage
166255
167256Once you completed the configuration you can use Laravel Queue API. If you used other queue drivers you do not need to change anything else. If you do not know how to use Queue API, please refer to the official Laravel documentation: http://laravel.com/docs/queues
0 commit comments