Skip to content
广告❤️成为赞助商

Command Listener

Command listener: When the Mongo driver executes command statements, messages are published before execution starts, after successful execution, and after a failure.

Note:

When using the command listener, it needs to be registered as a SpringBean.

com.mongoplus.listener.Listener

  • This interface is the listener interface; all listeners should implement this interface and its methods.
  • commandStarted: Handles command start events.
  • commandSucceeded: Handles command success events.
  • commandFailed: Handles command failure events.
  • getOrder: Specifies the listener order.

Example

First, implement the Listener interface and register the class as a SpringBean
java
// Register as SpringBean
@Component
public class CustomListener implements Listener {}
Then, implement the Listener methods to listen to commands
java
// Register as SpringBean
@Component
public class CustomListener implements Listener {
    @Override
    public void commandStarted(CommandStarted commandStarted) {
        System.out.println(JSON.toJSONString(commandStarted));
    }

    @Override
    public void commandSucceeded(CommandSucceeded commandSucceeded) {
        System.out.println(JSON.toJSONString(commandSucceeded));
    }

    @Override
    public void commandFailed(CommandFailed commandFailed) {
        System.out.println(JSON.toJSONString(commandFailed));
    }

    @Override
    public int getOrder() {
        return 0;
    }
}