Skip to content

Commit

Permalink
Merge pull request #1 from tochka-developers/fix-failover
Browse files Browse the repository at this point in the history
fix failover
  • Loading branch information
lexxksb authored Aug 14, 2017
2 parents dae7b6a + c438474 commit 1648a1f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nbproject
vendor
vendor
.idea
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# Stomp protocol utils

Набор классов-утилит для взаимодействия с брокетами сообщений (ActiveMQ, RabbitMQ, ZeroMQ и т.д.) по протоколу [Stomp](https://stomp.github.io/).
Пакет решает две наиболее распространенные задачи:
- Отправка сообщения в очередь;
- Получение сообщений из очеред в режиме демона.
Логирование можно осуществлять путем подключения любого PSR-совместимого логгера.

## Requirements
php 7.x

## Установка
Для установки пакета с использованием composer:
```
```bash
composer require stomp-utils
```

## Использование

### Отправка сообщения в очередь
```Publisher::send(string $destination, string $body[, array $headers][, string $transactionId])``` - отправляет сообщение в очередь.
Параметры:
Expand Down
27 changes: 12 additions & 15 deletions src/StompClient.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<?php

/**
* @created 28.06.2017
*/
namespace Tochka\Integration\Stomp;

use \Stomp;
use \StompException;
use Tochka\Integration\Stomp\Exception\StompClientException;

/**
*
* @author Sergey Ivanov(ivanov@tochka.com)
* Class StompClient
* @package Tochka\Integration\Stomp
*/
class StompClient
{
Expand Down Expand Up @@ -40,21 +37,20 @@ public function __construct(string $connectionString, string $login, string $pw)

$pattern = "|^(([a-zA-Z0-9]+)://)+\(*([a-zA-Z0-9\.:/i,-_]+)\)*$|i";
if (preg_match($pattern, $connectionString, $matches)) {
$scheme = $matches[2];
$hostsPart = $matches[3];

if ($scheme != 'failover') {
list(, , $scheme, $hostsPart) = $matches;

if ($scheme !== 'failover') {
$hosts[] = $hostsPart;
} else {
$urls = explode(',', $hosts);
foreach ($urls as $url) {
$hosts[] = $urls;
foreach (explode(',', $hostsPart) as $url) {
$hosts[] = $url;
}
}
}

if (empty($hosts)) {
throw new StompClientException("Bad Broker URL {$connectionString}. Check used scheme!");
throw new StompClientException('Bad Broker URL ' . $connectionString . 'Check used scheme!');
}

$this->hosts = $hosts;
Expand All @@ -72,10 +68,11 @@ public function __destruct()

/**
* @return Stomp
* @throws StompClientException
*/
public function getConnection()
public function getConnection(): \Stomp
{
if (is_null($this->stomp)) {
if (null === $this->stomp) {
$this->newConnection();
}

Expand Down Expand Up @@ -134,4 +131,4 @@ private function connect($url, $login, $pw)

return null;
}
}
}

0 comments on commit 1648a1f

Please sign in to comment.