bschmitt / laravel-amqp

AMQP wrapper for Laravel and Lumen to publish and consume messages
MIT License
270 stars 86 forks source link

Non-static method 'consume' should not be called statically #114

Open shwetatyagi22 opened 2 years ago

shwetatyagi22 commented 2 years ago

I am using below function to consume messages from rabbitmq queue. My IDE is giving me a warning for consume method Non-static method 'consume' should not be called statically

Amqp::consume('queue-name', function ($message, $resolver) {

   var_dump($message->body);

   $resolver->acknowledge($message);

   $resolver->stopWhenProcessed();

});
liviublidar commented 2 years ago

Hi @shwetatyagi22 ,

If you're using this with Laravel, the Consumer class can be injected to your controller function as it's registered as a provider. This should be equally injectable in a command.

use App\Http\Controllers\Controller;
use Bschmitt\Amqp\Consumer;
use Bschmitt\Amqp\Amqp;
use Illuminate\Http\Request;

class MyCoolController extends Controller {
   public function myCoolEntryPoint(Request $request, Amqp $consumer) {
     $consumer->consume('myfancyqueue', function (AMQPMessage $message, Consumer $resolver) {
            $this->doStuffToMyMessage($message);
            $resolver->acknowledge($message); //ack message
            $resolver->stopWhenProcessed();
        });
  }
}