Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
.DS_Store
composer.lock
31 changes: 31 additions & 0 deletions src/Listener/CoroutineDeferListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Swoft\Amqp\Listener;

use Swoft\Amqp\Connection\ConnectionManager;
use Swoft\Bean\BeanFactory;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\SwoftEvent;

/**
* Class CoroutineDeferListener
*
* @since 2.0
*
* @Listener(event=SwoftEvent::COROUTINE_DEFER)
*/
class CoroutineDeferListener implements EventHandlerInterface
{
/**
* @param EventInterface $event
*
*/
public function handle(EventInterface $event): void
{
/* @var ConnectionManager $conManager */
$conManager = BeanFactory::getBean(ConnectionManager::class);
$conManager->release();
}
}
31 changes: 31 additions & 0 deletions src/Listener/CoroutineDestoryListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Swoft\Amqp\Listener;

use Swoft\Amqp\Connection\ConnectionManager;
use Swoft\Bean\BeanFactory;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\SwoftEvent;

/**
* Class CoroutineDestoryListener
*
* @since 2.0
*
* @Listener(event=SwoftEvent::COROUTINE_DESTROY)
*/
class CoroutineDestoryListener implements EventHandlerInterface
{
/**
* @param EventInterface $event
*
*/
public function handle(EventInterface $event): void
{
/* @var ConnectionManager $conManager */
$conManager = BeanFactory::getBean(ConnectionManager::class);
$conManager->release(true);
}
}
36 changes: 36 additions & 0 deletions src/Listener/WorkerStartListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace Swoft\Amqp\Listener;

use Swoft\Amqp\Pool;
use Swoft\Bean\BeanFactory;
use Swoft\Connection\Pool\Exception\ConnectionPoolException;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Server\ServerEvent;

/**
* Class WorkerStartListener
*
* @since 2.0
*
* @Listener(event=ServerEvent::WORK_PROCESS_START)
*/
class WorkerStartListener implements EventHandlerInterface
{
/**
* @param EventInterface $event
*
* @throws ConnectionPoolException
*/
public function handle(EventInterface $event): void
{
$pools = BeanFactory::getBeans(Pool::class);

/* @var Pool $pool */
foreach ($pools as $pool) {
$pool->initPool();
}
}
}
49 changes: 49 additions & 0 deletions src/Listener/WorkerStopAndErrorListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Swoft\Amqp\Listener;

use Swoft\Amqp\Pool;
use Swoft\Bean\BeanFactory;
use Swoft\Event\Annotation\Mapping\Subscriber;
use Swoft\Event\EventInterface;
use Swoft\Event\EventSubscriberInterface;
use Swoft\Log\Helper\CLog;
use Swoft\Server\SwooleEvent;
use Swoft\SwoftEvent;

/**
* Class WorkerStopAndErrorListener
*
* @since 2.0
*
* @Subscriber()
*/
class WorkerStopAndErrorListener implements EventSubscriberInterface
{
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
SwooleEvent::WORKER_STOP => 'handle',
SwoftEvent::WORKER_SHUTDOWN => 'handle',
];
}

/**
* @param EventInterface $event
*
*/
public function handle(EventInterface $event): void
{
$pools = BeanFactory::getBeans(Pool::class);

/* @var Pool $pool */
foreach ($pools as $pool) {
$count = $pool->close();

CLog::info('Close %d AMQP connection on %s!', $count, $event->getName());
}
}
}