PHP使用消息中间件实例
使用点对点(点对点)模型
点对点模型特点:
只有一个消费者可以接收到消息不能重复消费生产者producer.php代码:
?phptry { //1 .建立连接$ Stump=new Stump(' TCP ://47)。52 .119 .21:61613 ');//2.实例化类$ obj=new Stdclass();//3.获取数据对于($ I=0;$ i3 $ I){ $ obj-username=' test ';$ obj-password=' 123456 ';$ queneName='/queue/UserReG ';//4.发送一个注册消息到队列$stomp-send($queneName,JSON _ encode($ obj));} } catch(stomp异常$ e){ die('连接失败: ' .$ e-Getmessay());}消费者1consumer1.php代码:
?PHP $ Stump=new Stump(' TCP ://localhost :61613 ');$ stomp-subscribe('/queue/UserReG ');while (true) { //判断是否有读取的信息if($ stomp-hasFrame()){ $ frame=$ stomp-readFrame();$data=json_decode($frame-body,true);var _ dump($ data);$ stomp-ack($ frame);}}消费者2consumer2.php代码:
?PHP $ Stump=new Stump(' TCP ://localhost :61613 ');$ stomp-subscribe('/queue/UserReG ');while (true) { //判断是否有读取的信息if($ stomp-hasFrame()){ $ frame=$ stomp-readFrame();$data=json_decode($frame-body,true);var _ dump($ data);$ stomp-ack($ frame);}}执行结果图如下:
使用发布/订阅(发布订阅)模型
发布/订阅模型特点:
多个消费者都可以收到消息能重复消费生产者producer.php代码:
?phptry { //1 .建立连接$ Stump=new Stump(' TCP ://47)。52 .119 .21:61613 ');//2.实例化类$ obj=new Stdclass();//3.获取数据对于($ I=0;$ I 3;$ I){ $ obj-username=' test ';$ obj-password=' 123456 ';$ queneName='/topic/UserReG ';//4.发送一个注册消息到队列$stomp-send($queneName,JSON _ encode($ obj));} } catch(stomp异常$ e){ die('连接失败: ' .$ e-Getmessay());}消费者1consumer1.php代码:
?PHP $ Stump=new Stump(' 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);}}消费者2consumer2.php代码:
?PHP $ Stump=new Stump(' 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);}}执行结果图如下:
版权声明:PHP使用消息中间件实例是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。