浏览代码

php-amqplib test

visuddhinanda 3 年之前
父节点
当前提交
6618f79ca3
共有 2 个文件被更改,包括 115 次插入0 次删除
  1. 55 0
      app/Console/Commands/TestMq.php
  2. 60 0
      app/Console/Commands/TestMqWorker.php

+ 55 - 0
app/Console/Commands/TestMq.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use PhpAmqpLib\Connection\AMQPStreamConnection;
+use PhpAmqpLib\Message\AMQPMessage;
+
+
+class TestMq extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'test:mq ';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+		$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
+		$channel = $connection->channel();
+		$channel->queue_declare('hello', false, false, false, false);
+
+		$msg = new AMQPMessage('Hello World!');
+		$channel->basic_publish($msg, '', 'hello');
+		
+		echo " [x] Sent 'Hello World!'\n";
+		$channel->close();
+		$connection->close();
+        return 0;
+    }
+}

+ 60 - 0
app/Console/Commands/TestMqWorker.php

@@ -0,0 +1,60 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+
+use PhpAmqpLib\Connection\AMQPStreamConnection;
+
+class TestMqWorker extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'test:mqworker';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+		$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
+		$channel = $connection->channel();
+
+		$channel->queue_declare('hello', false, false, false, false);
+
+		echo " [*] Waiting for messages. To exit press CTRL+C\n";
+
+		$callback = function ($msg) {
+			echo ' [x] Received ', $msg->body, "\n";
+		  };
+		  
+		$channel->basic_consume('hello', '', false, true, false, false, $callback);
+		  
+		while ($channel->is_open()) {
+			  $channel->wait();
+		  }
+        return 0;
+    }
+}