Ohad
Ohad

Reputation: 1631

callbacks in mongo db java asynch driver

I am trying to understand how the asynch java driver for mongodb works from this example:

collection.insertOne(doc, new SingleResultCallback<Void>() {
    @Override
    public void onResult(final Void result, final Throwable t) {
         System.out.println("Inserted!");
}
});
  1. its said that SingleResultCallback is a functional interface. what does it mean?
  2. when onResult will be called?
  3. does any access to the database is with this callback structure ? why do we need it?

Upvotes: 0

Views: 566

Answers (1)

Alexey R.
Alexey R.

Reputation: 8676

  1. Functional interface is a concept introduced in Java 8. Basically it is an interface with only one method's declaration (except the default and static methods). See details in What is use of Functional Interface in Java 8?.

  2. According to mongodb documentation the method is invoked when the operation is competed.

  3. Within this structure you have result instance that you can operate with if it is not null.

Upvotes: 1

Related Questions