opm87 / mqseries-php
使用PHP mqseries pecl扩展,为Laravel框架提供WebSphere MQ Queue Manager客户端
v0.1.6.1
2023-12-22 07:58 UTC
Requires
- php: >=5.5.9
- laravel/framework: >=5
- psr/log: >=1.0
Requires (Dev)
- laravel/framework: >=5
- phpunit/phpunit: >=8
Suggests
- ext-mqseries: To actually use this library, you'll have to install the PECL extension mqseries
README
使用PHP mqseries pecl扩展,为Laravel提供WebSphere MQ Queue Manager客户端。这是对https://github.com/amabnl/php-mqseries的分支。
如何
- 安装WebSphere MQ客户端库。
- 安装PECL模块mqseries。从这里下载:https://pecl.php.net/package/mqseries
- 安装此库
- 连接到队列并检索消息。
安装
在控制台中输入以下命令
composer require bariton3/mqseries-php
或者通过编辑composer.json,添加以下行并运行 composer update
"require": { ...., "bariton3/mqseries-php", },
配置
在'app.php'中注册包服务提供者和外观
'providers' => [ ... MqSeries\ServiceProvider\MqSeriesServiceProvider::class, ] 'aliases' => [ ... 'MqSeries' => MqSeries\Facades\MqSeriesFacade::class, ]
使用 php artisan vendor:publish --tag=mqseries --force
发布配置文件,或者直接复制包配置文件并粘贴到 config/mqseries.php
打开配置文件 config/mqseries.php
并添加您的服务密钥
/* |---------------------------------- | Service Keys |------------------------------------ */ 'channel' => 'ADD MQ_SERIES_CHANNEL HERE', 'queue_name' => ' ADD MQ_SERIES_QUEUE_NAME HERE', 'queue_manager' => 'ADD MQ_SERIES_QUEUE_MANAGER HERE', 'ip' => ' ADD MQ_SERIES_IP HERE', 'port' => 'ADD MQ_SERIES_PORT HERE', 'options' => MQSERIES_MQCNO_STANDARD_BINDING,
如果您想为任何服务使用不同的密钥,您可以在所选网络服务的service
数组中指定它以覆盖主API密钥。
使用方法
//Open Queue: $openParams = new MqSeries\Open\Params(); $openParams->objectDescType = MQSERIES_MQOT_Q; $openParams->objectName = config('mqseries.queue_name'); $openParams->objectQMName = ''; $openParams->option = MQSERIES_MQOO_OUTPUT | MQSERIES_MQOO_INPUT_AS_Q_DEF | MQSERIES_MQOO_FAIL_IF_QUIESCING; try { $queueOpenResult = MqSeries::openQueueOnQM($openParams); } catch (MqSeries\QueueManagerConnectionFailedException $ex) { die('Exception when opening queue: ' . $ex->getCode() . ' - ' . $ex->getMessage()); } catch (ExtensionNotLoadedException $ex) { die('YOU MUST FIRST ENABLE THE mqseries PHP EXTENSION'); } catch (NoConnectionParametersException $ex) { die('YOU DID NOT PROVIDE CONNECTX PARAMS!'); } if ($queueOpenResult !== true) { die( 'SOMETHING WENT WRONG WHEN OPENING THE QUEUE: ' . sprintf( "CompCode:%d Reason:%d Text:%s\n", MqSeries::getLastCompletionCode(), MqSeries::getLastCompletionReasonCode(), MqSeries::getLastCompletionReason() ) ); } // put the message on the queue. $mqPutParams = new MqSeries\Put\Params(); $mqPutParams->gmoOptions = MQSERIES_MQPMO_NEW_MSG_ID; MqSeries::putMessageToQueue($mqPutParams, 'PING'); if (MqSeries::getLastCompletionCode() !== MQSERIES_MQCC_OK) { die(printf("put CompCode:%d Reason:%d Text:%s<br>\n", MqSeries::getLastCompletionCode(), MqSeries::etLastCompletionReasonCode(), MqSeries::getLastCompletionReason())); } //Get one message from queue: $messageContent = ''; $mqGetParams = new MqSeries\Get\Params(); $mqGetParams->gmoOptions = MQSERIES_MQGMO_FAIL_IF_QUIESCING | MQSERIES_MQGMO_WAIT | MQSERIES_MQGMO_CONVERT; $mqGetParams->gmoWaitInterval = 5000; try { $messageContent = MqSeries::getMessageFromQueue($mqGetParams); echo $messageContent."\n"; } catch (QueueIsEmptyException $ex) { echo "The queue is empty, no big deal."; if (is_string($messageContent)) { echo 'message retrieved from queue: ' . $messageContent; } else { die( 'SOMETHING WENT WRONG WHEN RETRIEVING A MESSAGE: ' . sprintf( "CompCode:%d Reason:%d Text:%s\n", MqSeries::getLastCompletionCode(), MqSeries::getLastCompletionReasonCode(), MqSeries::getLastCompletionReason() ) ); } } //Close & disconnect: MqSeries::close(); MqSeries::disconnect();