PHP code example of kontoulis / rabbit-manager

1. Go to this page and download the library: Download kontoulis/rabbit-manager 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/ */

    

kontoulis / rabbit-manager example snippets


    $application->addCommands(
    	array(
    		new RabbitManager\Commands\QueueAddCommand,
    		new RabbitManager\Commands\QueueListenCommand,
    		new RabbitManager\Commands\YourCustomCommand,
    	)
    );

use RabbitManager\Libs\Broker;
use RabbitManager\Libs\Message;

// Your Class and Methods

public function publishMessage($message , $queueName = "Default")
{
    $broker = new Broker(AMPQ_HOST, AMPQ_PORT, AMPQ_USER, AMPQ_PASSWORD , AMPQ_VHOST);
    /* Makes the AMPQ message */
    $msg = new Message($queueName, ["message" => $message]);
    
    /* Sends the message */
    $broker->sendMessage($msg);
    
    $output->writeln('<info>Successfully submitted in queue</info>');
}

use RabbitManager\Libs\Broker;

public function listenToQueue($queueName = "Default" )
// Listening to queue
  $broker = new Broker();
  // Here you tell the broker which handler to call in order to parse the message
  // Use a fully qualified Namespace.
  // The broker will call the tryProcessing() method of the specified Handler
  // for every message received from the queue.
  // The handler in the package is named DefaultHandler
  // Make your own handlers according to your needs
  $broker->listenToQueue(
  	$queueName,
  	array(
  		"\\RabbitManager\\Handlers\\" . $queueName . "Handler"
  	)
  );



use RabbitManager\Libs\Handler;
use RabbitManager\Libs\Message;

class TheNameOfTheQueue extends Handler
{

	/**
	 * Tries to process the incoming message.
	 * @param Message $msg
	 * @return int One of the possible return values defined as Handler
	 * constants.
	 */
	public function tryProcessing(Message $msg)
	{
	  // TODO : Check, modify or validate the message.
	  // If the message is OK, process it
		return $this->handleSuccess($msg->getAMQPMessage()->body);

	}

	/**
	 * @param $msg
	 * @return int
	 */
	protected function handleSuccess($msg)
	{
	  // TODO : Do the processing. Store something in the db,
	  // Send a notification or eanything you are supossed to do with the received message
		echo $msg . "\n";
    
    // Returns and integer to the Broker, and the broker continues accordingly.
    // For a full list of return codes see the section bellow
		return Handler::RV_SUCCEED_CONTINUE;
	}
}

  /**
	 * Pass this message and proceed with the next
	 */
	const RV_PASS = 1;
	/**
	 * Continue and ignore the failed message
	 */
	const RV_FAILED_CONTINUE = 10;
	/**
	 * We failed to do our job with this message (e.g. failed to store it in the database),
	 * Force exit
	 */
	const RV_FAILED_STOP = 11;
	/**
	 * We failed to do our job with this message (e.g. failed to store it in the database),
	 * put it again in the queue
	 */
	const RV_FAILED_REQUEUE = 12;
	/**
	 * Keep listening to the queue after successfully parsing the message
	 */
	const RV_SUCCEED_CONTINUE = 20;
	/**
	 *  Force stop listening after successfully parsing a message
	 */
	const RV_SUCCEED_STOP = 21;
	/**
	 *
	 */
	const RV_SUCCEED = Handler::RV_SUCCEED_CONTINUE;
	/**
	 *
	 */
	const RV_FAILED = Handler::RV_FAILED_CONTINUE;
	/**
	 *
	 */
	const RV_ACK = Handler::RV_SUCCEED;
	/**
	 *
	 */
	const RV_NACK = Handler::RV_FAILED_STOP;