mq类----1
MQ.php
- <?php
- /**
- * Created by PhpStorm.
- * User: brady
- * Date: 2017/12/6
- * Time: 14:42
- *
- * amqp协议操作类,可以访问rabbitMQ
- * 需先安装php_amqp扩展
- *
- */
- class MQ
- {
- //配置
- public $configs = array();
- //交换机名称
- public $exchange_name = '';
- //队列名称
- public $queue_name = '';
- //路由名称 注意 如果用同一个路由绑定交换机,当推送的时候,会同时推送到这几个key上 $q = new AMQPQueue($channel); $q->setName('queue3'); $q->setName('queue2'); $q->bind('exchange',$routingkey);
- public $route_key = '';
- //是否持久化 默认true
- public $durable = true;
- /*
- * 是否自动删除
- * exchange is deleted when all queues have finished using it
- * queue is deleted when last consumer unsubscribes
- *
- */
- public $auto_delete = false;
- //镜像队列,打开后消息会在节点之间复制,有master和slave的概念
- public $mirror = false;
- //连接
- private $_conn = NULL;
- //交换机对象
- private $_exchange = NULL;
- //信道对象
- private $_channel = NULL;
- //队列对象
- private $_queue = NULL;
- /**
- * MQ constructor.
- * @configs array('host'=>$host,'port'=>5672,'username'=>$username,'password'=>$password,'vhost'=>'/')
- */
- public function __construct($configs=array(),$exchange_name='',$queue_name='',$route_key='')
- {
- $this->exchange_name = $exchange_name;
- $this->queue_name = $queue_name;
- $this->route_key = $route_key;
- $this->set_configs($configs);
- }
- /**
- * @desc 配置设置
- * @param $configs
- */
- public function set_configs($configs)
- {
- if(empty($configs) || !is_array($configs)){
- throw new Exception("your config is not array");
- }
- if(empty($configs['host']) || empty($configs['username']) || empty($configs['password'])) {
- throw new Exception("your config is error");
- }
- if(empty($configs['vhost'])){
- $configs['vhost'] = '/';
- }
- if(empty($configs['port'])){
- $configs['port'] = '5672';
- }
- $configs['login'] = $configs['username'];
- unset($configs['username']);
- $this->configs = $configs;
- }
- /**
- * 设置是否持久化
- * @param $durable
- */
- public function set_durable($durable)
- {
- $this->durable = $durable;
- }
- /**
- * 设置是否自动删除
- * @param $auto_delete boolean
- */
- public function set_auto_delete($auto_delete)
- {
- $this->auto_delete = $auto_delete;
- }
- /**
- * 设置是否镜像
- * @param $mirror
- */
- public function set_mirror($mirror)
- {
- $this->mirror = $mirror;
- }
- /**
- * 连接初始化
- */
- public function init()
- {
- //没有连接对象,进行连接 有不管 就不用每次都连接和初始化
- if(!$this->_conn){
- $this->_conn = new AMQPConnection($this->configs);
- $this->_conn->connect();
- $this->init_exchange_queue_route();
- }
- }
- /**
- * 初始化 交换机 队列名 路由
- */
- public function init_exchange_queue_route()
- {
- if(empty($this->exchange_name) || empty($this->queue_name) || empty($this->route_key)){
- throw new Exception("rabbitMQ exchage_name or queue_name or route_key is empty, please check is",'500');
- }
- //channel
- $this->_channel = new AMQPChannel($this->_conn);//创建channel
- //exchange
- $this->_exchange = new AMQPExchange($this->_channel);//创建交换机
- $this->_exchange->setName($this->exchange_name);//设置交换机名字
- $this->_exchange->setType(AMQP_EX_TYPE_DIRECT);//交换机方式为direct
- if($this->durable) {
- $this->_exchange->setFlags(AMQP_DURABLE);//是否持久化
- }
- if($this->auto_delete){
- $this->_exchange->setFlags(AMQP_AUTODELETE);//是否自动删除
- }
- $this->_exchange->declareExchange();//申请交换机
- //queue
- $this->_queue = new AMQPQueue($this->_channel);
- $this->_queue->setName($this->queue_name);
- if($this->durable){
- $this->_queue->setFlags(AMQP_DURABLE);
- }
- if($this->auto_delete){
- $this->_queue->setFlags(AMQP_AUTODELETE);
- }
- if($this->mirror){
- $this->_queue->setArgument('x-ha-policy','all');
- }
- $this->_queue->declareQueue();//申请queue
- //绑定交换机
- $this->_queue->bind($this->exchange_name,$this->route_key);
- }
- //关闭连接
- public function close()
- {
- if($this->_conn){
- $this->_conn->disconnect();
- }
- }
- //断开连接
- public function __destruct()
- {
- // TODO: Implement __destruct() method.
- $this->close();
- }
- //生产消息
- public function send($msg)
- {
- $this->init();
- if(!$this->_conn){
- throw new Exception("connect RabbitMQ failed when send message");
- }
- if(is_array($msg)) {
- $msg = json_encode($msg);
- } else {
- $msg = trim(strval($msg));
- }
- return $this->_exchange->publish($msg,$this->route_key);
- }
- //消费消息 自动应答模式
- public function run_auto($funcation_name,$auto_ack = true)
- {
- $this->init();
- if(!$funcation_name || !$this->_queue) {
- throw new Exception("auto ack lose function_name or this->_queue");
- }
- while(true){
- if($auto_ack){
- $this->_queue->consume($funcation_name,AMQP_AUTOACK);
- } else {
- $this->_queue->consume($funcation_name);
- }
- }
- }
- }
my_pub.php
- <?php
- /**
- * Created by PhpStorm.
- * User: brady
- * Date: 2017/12/6
- * Time: 14:35
- */
- require_once "MQ.php";
- $configs = array(
- 'host'=>'192.168.33.30',
- 'port'=>5672,
- 'username'=>'title',
- 'password'=>'title',
- 'vhost'=>'/'
- );
- $number = 2;
- $config = [
- 'exchange_name'=>'brady',
- 'queue_name'=>"queue_".$number,
- 'route_key'=>"route_".$number
- ];
- $mq = new MQ($configs,$config['exchange_name'],$config['queue_name'],$config['route_key']);
- for($i=0;$i<10000;$i++){
- $res = $mq->send(date("Y-m-d H:i:s"));
- }
my_consumer.php
- <?php
- /**
- * Created by PhpStorm.
- * User: brady
- * Date: 2017/12/6
- * Time: 19:41
- */
- require_once "MQ.php";
- set_time_limit(0);
- $configs = array(
- 'host'=>'192.168.33.30',
- 'port'=>5672,
- 'username'=>'title',
- 'password'=>'title',
- 'vhost'=>'/'
- );
- $number = 2;
- $config = [
- 'exchange_name'=>'brady',
- 'queue_name'=>"queue_".$number,
- 'route_key'=>"route_".$number
- ];
- $mq = new MQ($configs,$config['exchange_name'],$config['queue_name'],$config['route_key']);
- //用类的方式
- /*class A{
- function processMessage($envelope, $queue) {
- $msg = $envelope->getBody();
- $envelopeID = $envelope->getDeliveryTag();
- $pid = posix_getpid();
- file_put_contents("log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);
- $queue->ack($envelopeID);
- }
- }
- $a = new A();*/
- class B{
- protected $_envelope;
- protected $_queue;
- public function __construct($envelope,$queue)
- {
- $this->_queue = $queue;
- $this->_envelope = $envelope;
- }
- public function test()
- {
- $msg = $this->_envelope->getBody();
- $envelopeID = $this->_envelope->getDeliveryTag();
- $pid = posix_getpid();
- file_put_contents("log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);
- $this->_queue->ack($envelopeID);
- }
- }
- //用函数的方式 直接在回调里面处理,也可以在回调里面,再load新的model 事务在model里面处理
- function processMessage($envelope, $queue) {
- /* $msg = $envelope->getBody();
- $envelopeID = $envelope->getDeliveryTag();
- $pid = posix_getpid();
- file_put_contents("log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);
- $queue->ack($envelopeID); //如果设置为AMQP_AUTOACK 那么不需要该行也可以自动应答*/
- $b = new B($envelope,$queue);
- $b->test();
- }
- //$s = $ra->run(array($a,'processMessage'),false);
- $s = $mq->run_auto("processMessage",false);
ci里面回调函数可以传对象 比如$this $this->tb_users 或者赋值给一个变量后传
- public function test_get()
- {
- $obj = $this;
- set_time_limit(0);
- require_once APPPATH."/third_party/mq/RabbitMQ.php";
- $configs = array(
- 'host'=>'192.168.33.30',
- 'port'=>5672,
- 'username'=>'title',
- 'password'=>'title',
- 'vhost'=>'/'
- );
- $number = 2;
- $config = [
- 'exchange_name'=>'brady',
- 'queue_name'=>"queue_".$number,
- 'route_key'=>"route_".$number
- ];
- $ra = new RabbitMQ($configs,$config['exchange_name'],$config['queue_name'],$config['route_key']);
- $ra->run(array($obj,'processMessage'),false);
- }
后面再写篇用get手动获取和响应的ack
mq类----1的更多相关文章
- mq类----2
手动应答方式 使用get my_consumer.php 消费者 生产者和上一篇 一样 <?php /** * Created by PhpStorm. * User: brady * Dat ...
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- MSMQ消息队列 用法
引言 接下来的三篇文章是讨论有关企业分布式开发的文章,这三篇文章筹划了很长时间,文章的技术并不算新,但是文章中使用到的技术都是经过笔者研究实践后总结的,正所谓站在巨人的肩膀上,笔者并不是巨人,但也希望 ...
- 企业数据总线(ESB)和注册服务管理(dubbo)的区别
企业数据总线(ESB)和注册服务管理(dubbo)的区别 转载 2015年11月04日 09:05:14 7607 企业数据总线(ESB)和注册服务管理(dubbo)的区别 2015-03-09 0 ...
- Hadoop/Spark生态圈里的新气象
令人惊讶的是,Hadoop在短短一年的时间里被重新定义.让我们看看这个火爆生态圈的所有主要部分,以及它们各自具有的意义. 对于Hadoop你需要了解的最重要的事情就是 ,它不再是原来的Hadoop. ...
- 电商技术中企业数据总线ESB和注册服务管理的区别
一.概述 1.什么是ESB 就是企业数据总线的意思,他的核心功能就是兼容各种协议接口,可以将数据在各种协议之间进行流转,并且可以针对数据格式进行编排转换. 异构系统,功能繁多,复杂 代表性的项目有:J ...
- 【编码】封装RedisPubSub工具
基本介绍 核心原理:利用Redis的List列表实现,发布事件对应rpush,订阅事件对应lpop 问题一:Redis不是自带Pub/Sub吗? redis自带的pub/sub有两个问题: 1.如果发 ...
- RabbitMQ,想说爱你不容易(附详细安装教程)
前言 本文讲述的只是主要是 RabbitMQ 的入门知识,学习本文主要可以掌握以下知识点: MQ 的发展史 AMQP 协议 Rabbit MQ 的安装 Rabbit MQ 在 Java API 中的使 ...
- IBM WebSphere MQ的C#工具类以及源码(net)
简单的介绍一下MQ常用的对象 Queue Manager 队列管理器 主要负责管理队列.通道等,类似与Oracle中的Oracle实例的概念,在一台服务器中可以定义多个Queue Manager. Q ...
随机推荐
- Spring.Net 能为我们做点什么
本文内容 概述 背景 模块 使用场景 入门应用 Spring.NET 相关项目 本文正式开始前,我以目前所能想到的.此时此刻能想到的,先简单说下,为什么会有像 Spring.Net 这样的东西.首先, ...
- 洛谷 P1345 [USACO5.4]奶牛的电信Telecowmunication
题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...
- 随记:UWP开发中怎么使当前页面拓展到标题栏
public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); CoreAp ...
- itextsharp-5.2.1-修正无法签名大文件问题
PDF文件格式几乎是所有开发平台或者业务系统都热爱的一种文档格式. 目前有很多优秀的开源PDF组件和类库.主要平时是使用.NET和Java开发,所以比较偏好使用iText,当然,它本身就很强大.iTe ...
- Unity3D中使用Projector生成阴影
在Unity3D中使用Projector实现动态阴影 无意中看见一篇博客叙述使用Projector实现动态阴影可以在移动平台拥有非常好的性能,遂按照其想法实现了一遍,发现其中竟有许多细节,写下这篇博客 ...
- 在DataGridView控件中启用换行
实现效果: 知识运用: DataGridView控件公共属性DefaultCellStyle的WrapMode属性 public DataGridViewTriState WrapMode { ge ...
- 剑指offer64 数据流中的中位数
priority_queue优先级队列,他的模板声明带有三个参数,priority_queue<Type, Container, Functional> Type 为数据类型, Conta ...
- SQL Server数据库字段类型说明
SQL Server数据库字段类型说明 目前Sql Server 数据库一共有X个字段类型,大体分为9类,分别是字符串类型.二进制码字符串数据类型.Unincode字符串数据.整数类型.精确数据类型. ...
- 【转】MFC右键显示菜单之LoadMenu()
如何在界面内单击右键弹出自己设置的菜单选项? 步骤如下: 1.在资源MENU里添加一个菜单资源,命名为IDR_POP_MENU. 2.在自己添加的菜单中添加事件,如事件1,事件2,事件3,分别添加响应 ...
- Angular - angularjs2 一些报错的概览(数据为json格式)
{"Unterminated string literal.": "未终止的字符串文本.","Identifier expected.": ...