@@ -161,7 +161,87 @@ When you want to instruct RabbitMQ to reroute failed messages to a exchange or a
161161 // ...
162162],
163163```
164+ ### Use your own RabbitMQJob class
165+ Sometimes you have to work with messages published by another application.
166+ Those messages probably won't respect the laravel QueueApi payload schema.
167+ The problem with these messages is that, laravel workers won't be able to determine the actual job or class to execute.
164168
169+ You can extend the build-in ` RabbitMQJob::class ` and within the queue connection config, you can add your own class.
170+ 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.
171+
172+ An example for the config:
173+ ``` php
174+ 'connections' => [
175+ // ...
176+
177+ 'rabbitmq' => [
178+ // ...
179+
180+ 'options' => [
181+ 'queue' => [
182+ // ...
183+
184+ 'job' => \App\Queue\Jobs\RabbitMQJob::class,
185+ ],
186+ ],
187+ ],
188+
189+ // ...
190+ ],
191+ ```
192+ An example of your own job class:
193+ ``` php
194+ <?php
195+
196+ namespace App\Queue\Jobs;
197+
198+ use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
199+
200+ class RabbitMQJob extends BaseJob
201+ {
202+
203+ /**
204+ * Fire the job.
205+ *
206+ * @return void
207+ */
208+ public function fire()
209+ {
210+ $payload = $this->payload();
211+
212+ $class = WhatheverClassNameToExecute::class;
213+ $method = 'handle';
214+
215+ ($this->instance = $this->resolve($class))->{$method}($this, $payload);
216+ }
217+ }
218+
219+ ```
220+ Or maybe you want to add extra properties to the payload.
221+ ``` php
222+ <?php
223+
224+ namespace App\Queue\Jobs;
225+
226+ use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
227+
228+ class RabbitMQJob extends BaseJob
229+ {
230+
231+ /**
232+ * Get the decoded body of the job.
233+ *
234+ * @return array
235+ */
236+ public function payload()
237+ {
238+ $decoded = json_decode($this->getRawBody(), true);
239+ $decoded['job'] = $decoded['job'] ?? 'WhatheverFullyQualifiedClassNameToExecute@handle';
240+
241+ return $decoded;
242+ }
243+ }
244+ ```
165245## Laravel Usage
166246
167247Once 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