Reputation: 21
i have been learning about kafka and laravel.
so i'm trying to implement kafka(using rdkafka) in laravel, but i have a problem
when i tried to use kafka in php, everything works fine in pure php, but i got an error when i tried to implement it in laravel as a command class
$conf = new RdKafka\Conf();
i think that's the problem, RdKafka is a dynamic extension in php.ini(i added it manually directly from php.ini) and become undefined when i tried to implement in laravel and i don't know how to solve it.
how to use dynamic extension in laravel?
here is the command class and consumer function tutorial from rdkafka
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ExampleCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:name';
/**
* 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 mixed
*/
public function handle()
{
//
$conf = new RdKafka\Conf();
$conf->set('group.id', 'myConsumerGroup');
$rk = new RdKafka\Consumer($conf);
$rk->addBrokers("127.0.0.1");
$topicConf = new RdKafka\TopicConf();
$topicConf->set('auto.commit.interval.ms', 100);
$topicConf->set('offset.store.method', 'file');
$topicConf->set('offset.store.path', sys_get_temp_dir());
$topicConf->set('auto.offset.reset', 'smallest');
$topic = $rk->newTopic("test", $topicConf);
$topic->consumeStart(0, RD_KAFKA_OFFSET_STORED);
while (true) {
$message = $topic->consume(0, 120*10000);
switch ($message->err) {
case RD_KAFKA_RESP_ERR_NO_ERROR:
var_dump($message);
break;
case RD_KAFKA_RESP_ERR__PARTITION_EOF:
echo "No more messages; will wait for more\n";
break;
case RD_KAFKA_RESP_ERR__TIMED_OUT:
echo "Timed out\n";
break;
default:
throw new \Exception($message->errstr(), $message->err);
break;
}
}
}
}
error that i got from artisan command
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'App\Console\Commands\RdKafka\Conf' not found
Upvotes: 2
Views: 1291
Reputation: 971
The problem you didn't Add this line to using namespaces so the package namespace will be like this App\Console\Commands\RdKafka\Conf
and this is wrong so you should add the below line in using namespaces:
namespace App\Console\Commands;
use Illuminate\Console\Command;
using RdKafka\Conf; // Add this line
Upvotes: 0
Reputation: 606
I am using PHPStorm and had the same problem with the IDE recognizing the namespace. I finally found out that I needed to install the stubs for the IDE to recognize the extension namespaces. The extension actually works though (\RdKafka). It's just that the IDE didn't recognize the namespace until I installed the stubs.
https://github.com/kwn/php-rdkafka-stubs
Upvotes: 3