라이브러리

[PHP] MongoDBDriverMonitoringCommandSubscriber::commandFailed - 실패한 명령에 대한 알림 방법




MongoDB Driver Monitoring Command Subscriber

MongoDB Driver Monitoring Command Subscriber는 MongoDB Driver의 Command Subscriber 중 하나입니다. 이 클래스는 MongoDB Command를 처리하는 동안 발생하는 오류를 감지하고 처리하는 역할을 합니다. 이 클래스는 `MongoDBDriverMonitoringCommandSubscriber` 클래스로 정의되어 있습니다.

Command Failed 이벤트

`commandFailed` 메소드는 MongoDB Command를 처리하는 동안 발생하는 오류를 감지할 때 호출됩니다. 이 메소드는 `MongoDBDriverMonitoringCommandSubscriber` 클래스의 메소드 중 하나로, `MongoDBDriverMonitoringCommandSubscriber` 클래스를 상속하는 클래스에서 오버라이드할 수 있습니다.

예제

아래 예제는 `MongoDBDriverMonitoringCommandSubscriber` 클래스를 상속하는 `MyCommandSubscriber` 클래스를 정의하고, `commandFailed` 메소드를 오버라이드하는 방법을 보여줍니다.

#hostingforum.kr
php

use MongoDBDriverCommand;

use MongoDBDriverMonitoringCommandSubscriber;

use MongoDBDriverMonitoringCommandFailedEvent;

use MongoDBDriverMonitoringCommandStartedEvent;

use MongoDBDriverMonitoringCommandSucceededEvent;



class MyCommandSubscriber implements CommandSubscriber

{

    public function commandFailed(CommandFailedEvent $event)

    {

        // Command Failed 이벤트가 발생했을 때 호출됩니다.

        // $event->getCommand() : Command 객체를 반환합니다.

        // $event->getException() : Exception 객체를 반환합니다.

        $command = $event->getCommand();

        $exception = $event->getException();



        echo "Command Failed: " . $command->getCmd() . "
";

        echo "Exception: " . $exception->getMessage() . "
";

    }



    public function commandStarted(CommandStartedEvent $event)

    {

        // Command Started 이벤트가 발생했을 때 호출됩니다.

        // $event->getCommand() : Command 객체를 반환합니다.

        $command = $event->getCommand();



        echo "Command Started: " . $command->getCmd() . "
";

    }



    public function commandSucceeded(CommandSucceededEvent $event)

    {

        // Command Succeeded 이벤트가 발생했을 때 호출됩니다.

        // $event->getCommand() : Command 객체를 반환합니다.

        $command = $event->getCommand();



        echo "Command Succeeded: " . $command->getCmd() . "
";

    }

}



사용 방법

아래 예제는 `MyCommandSubscriber` 클래스를 사용하는 방법을 보여줍니다.

#hostingforum.kr
php

use MongoDBDriverManager;

use MongoDBDriverMonitoringCommandSubscriber;



$manager = new Manager("mongodb://localhost:27017");



$subscriber = new MyCommandSubscriber();



$manager->addCommandSubscriber($subscriber);



// MongoDB Command를 실행합니다.

$command = new Command(array("find" => "mycollection"));

$manager->executeCommand($command);



이 예제에서는 `MyCommandSubscriber` 클래스를 사용하여 MongoDB Command를 처리하는 동안 발생하는 오류를 감지하고 처리합니다. `commandFailed` 메소드는 Command Failed 이벤트가 발생했을 때 호출되며, `$event->getCommand()`와 `$event->getException()`을 사용하여 Command 객체와 Exception 객체를 얻을 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 395 페이지

검색

게시물 검색