1. Go to this page and download the library: Download almatar/rabbitmq library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Almatar\RabbitMQ\Adapters\Producer;
class TestService
{
public function execute()
{
$producer = app(Producer::class);
$producer->publish(
config('rabbitmq.producers.test_producer'),
json_encode(['name' => 'John Doe', 'Age' => 7000])
);
}
}
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ProcessMessageJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function handle()
{
// Your message processing logic here
Log::info('Processing message', $this->data);
}
}
// Dispatch the job
ProcessMessageJob::dispatch(['name' => 'John Doe', 'Age' => 7000]);
use Almatar\RabbitMQ\Adapters\Consumer;
class TestConsumer extends Command
{
public function handle()
{
$consumer = app(Consumer::class);
$consumer->subscribe(
config('rabbitmq.consumers.test_consumer'),
[$this, 'consume']
);
}
public function consume(AMQPMessage $message)
{
$this->info('Message Consumed: ' . $message->getBody());
$message->ack();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.