PHP使用ActiveMQ实例
使用点对点(Point To Point)模型 点对点模型特点:
生产者producer.php代码: username = 'test';
$obj->password = '123456';
$queneName = "/queue/userReg";
// 4.发送一个注册消息到队列
$stomp->send($queneName,json_encode($obj));
}
} catch (StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
消费者1consumer1.php代码: $stomp = new Stomp('tcp://localhost:61613');
$stomp->subscribe('/queue/userReg'); while (true) { 消费者2consumer2.php代码: $stomp = new Stomp('tcp://localhost:61613');
$stomp->subscribe('/queue/userReg'); while (true) { 执行结果图如下: 使用发布/订阅(Publish Subscribe)模型 发布/订阅模型特点: 多个消费者都可以收到消息 能重复消费 生产者producer.php代码: username = 'test';
$obj->password = '123456';
$queneName = "/topic/userReg";
// 4.发送一个注册消息到队列
$stomp->send($queneName,json_encode($obj));
}
} catch (StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
消费者1consumer1.php代码: $stomp = new Stomp('tcp://localhost:61613');
$stomp->subscribe('/topic/userReg'); while (true) { 消费者2consumer2.php代码: $stomp = new Stomp('tcp://localhost:61613');
$stomp->subscribe('/topic/userReg'); while (true) { //判断是否有读取的信息 if ($stomp->hasFrame()) { $frame = $stomp->readFrame(); $data = json_decode($frame->body,true); var_dump($data); $stomp->ack($frame); } } 执行结果图如下: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |