1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use PhpAmqpLib\Connection\AMQPStreamConnection;
- use PhpAmqpLib\Message\AMQPMessage;
- define('HOST', '120.55.25.252');
- define('PORT', 5672);
- define('USER', 'test');
- define('PASS', '6acbQWE13');
- define('VHOST', '/');
- //If this is enabled you can see AMQP output on the CLI
- define('AMQP_DEBUG', true);
- class test_queue extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'command:tqueue';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'tqueue';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- echo 'publish';
- $this->exchange = 'router';
- $this->queue = 'msg';
- $connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST);
- $channel = $connection->channel();
- echo $channel;
- /*
- The following code is the same both in the consumer and the producer.
- In this way we are sure we always have a queue to consume from and an
- exchange where to publish messages.
- */
- /*
- name: $queue
- passive: false
- durable: true // the queue will survive server restarts
- exclusive: false // the queue can be accessed in other channels
- auto_delete: false //the queue won't be deleted once the channel is closed.
- */
- $channel->queue_declare($this->queue, false, true, false, false);
- /*
- name: $exchange
- type: direct
- passive: false
- durable: true // the exchange will survive server restarts
- auto_delete: false //the exchange won't be deleted once the channel is closed.
- */
- $channel->exchange_declare($exchange, 'direct', false, true, false);
- $channel->queue_bind($queue, $exchange);
-
- // $messageBody = implode(' ', array_slice($argv, 1));
- $messageBody = 'msg1';
- $options = getopt("f:hp:");
- var_dump('$options');
- var_dump($options);
-
- $message = new AMQPMessage($messageBody, array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
-
- $channel->basic_publish($message, $exchange);
- $channel->close();
- $connection->close();
-
- }
- }
|