|
13 | 13 | use Proklung\RabbitMQ\RabbitMq\DequeuerAwareInterface;
|
14 | 14 | use Proklung\RabbitMQ\RabbitMq\Producer;
|
15 | 15 | use Proklung\RabbitMq\Utils\BitrixSettingsDiAdapter;
|
| 16 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
16 | 17 | use Symfony\Component\DependencyInjection\Definition;
|
17 | 18 | use Symfony\Component\DependencyInjection\Reference;
|
18 | 19 |
|
@@ -114,6 +115,7 @@ public function load() : void
|
114 | 115 | $this->loadProducers();
|
115 | 116 | $this->loadConsumers();
|
116 | 117 | $this->loadAnonConsumers();
|
| 118 | + $this->loadBatchConsumers(); |
117 | 119 | $this->loadRpcClients();
|
118 | 120 | $this->loadRpcServers();
|
119 | 121 |
|
@@ -496,6 +498,82 @@ private function loadAnonConsumers() : void
|
496 | 498 | }
|
497 | 499 | }
|
498 | 500 |
|
| 501 | + /** |
| 502 | + * @return void |
| 503 | + */ |
| 504 | + private function loadBatchConsumers() : void |
| 505 | + { |
| 506 | + foreach ($this->config['batch_consumers'] as $key => $consumer) { |
| 507 | + $this->registerCallbackAsService($consumer['callback']); |
| 508 | + |
| 509 | + $definition = new Definition('%rabbitmq.batch_consumer.class%'); |
| 510 | + |
| 511 | + if (!isset($consumer['exchange_options'])) { |
| 512 | + $consumer['exchange_options'] = $this->getDefaultExchangeOptions(); |
| 513 | + } |
| 514 | + |
| 515 | + $definition |
| 516 | + ->setPublic(true) |
| 517 | + ->addTag('rabbitmq.base_amqp') |
| 518 | + ->addTag('rabbitmq.batch_consumer') |
| 519 | + ->addMethodCall('setTimeoutWait', array($consumer['timeout_wait'])) |
| 520 | + ->addMethodCall('setPrefetchCount', array($consumer['qos_options']['prefetch_count'])) |
| 521 | + ->addMethodCall('setCallback', array(array(new Reference($consumer['callback']), 'batchExecute'))) |
| 522 | + ->addMethodCall('setExchangeOptions', array($this->normalizeArgumentKeys($consumer['exchange_options']))) |
| 523 | + ->addMethodCall('setQueueOptions', array($this->normalizeArgumentKeys($consumer['queue_options']))) |
| 524 | + ->addMethodCall('setQosOptions', array( |
| 525 | + $consumer['qos_options']['prefetch_size'], |
| 526 | + $consumer['qos_options']['prefetch_count'], |
| 527 | + $consumer['qos_options']['global'] |
| 528 | + )) |
| 529 | + ; |
| 530 | + |
| 531 | + if (isset($consumer['idle_timeout_exit_code'])) { |
| 532 | + $definition->addMethodCall('setIdleTimeoutExitCode', array($consumer['idle_timeout_exit_code'])); |
| 533 | + } |
| 534 | + |
| 535 | + if (isset($consumer['idle_timeout'])) { |
| 536 | + $definition->addMethodCall('setIdleTimeout', array($consumer['idle_timeout'])); |
| 537 | + } |
| 538 | + |
| 539 | + if (isset($consumer['graceful_max_execution'])) { |
| 540 | + $definition->addMethodCall( |
| 541 | + 'setGracefulMaxExecutionDateTimeFromSecondsInTheFuture', |
| 542 | + array($consumer['graceful_max_execution']['timeout']) |
| 543 | + ); |
| 544 | + } |
| 545 | + |
| 546 | + if (!$consumer['auto_setup_fabric']) { |
| 547 | + $definition->addMethodCall('disableAutoSetupFabric'); |
| 548 | + } |
| 549 | + |
| 550 | + if ($consumer['keep_alive']) { |
| 551 | + $definition->addMethodCall('keepAlive'); |
| 552 | + } |
| 553 | + |
| 554 | + $this->injectConnection($definition, $consumer['connection']); |
| 555 | + |
| 556 | + if ($consumer['enable_logger']) { |
| 557 | + $this->injectLogger($definition); |
| 558 | + } |
| 559 | + |
| 560 | + $this->container->setDefinition(sprintf('rabbitmq.%s_batch', $key), $definition); |
| 561 | + } |
| 562 | + } |
| 563 | + |
| 564 | + /** |
| 565 | + * @param Definition $definition |
| 566 | + * |
| 567 | + * @return void |
| 568 | + */ |
| 569 | + private function injectLogger(Definition $definition) |
| 570 | + { |
| 571 | + $definition->addTag('monolog.logger', array( |
| 572 | + 'channel' => 'phpamqplib' |
| 573 | + )); |
| 574 | + $definition->addMethodCall('setLogger', array(new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))); |
| 575 | + } |
| 576 | + |
499 | 577 | /**
|
500 | 578 | * Регистрация класса сервисом.
|
501 | 579 | *
|
|
0 commit comments