0

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?
Alexey R.
  • 8,057
  • 2
  • 11
  • 27
Ohad
  • 1,563
  • 2
  • 20
  • 44

1 Answers1

1
  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.

Alexey R.
  • 8,057
  • 2
  • 11
  • 27
  • when the operation is completed - you mean when the insert operation is completed? – Ohad Aug 30 '17 at 06:34
  • @Ohad exactly. Check this API documentation http://api.mongodb.com/java/current/com/mongodb/async/client/MongoCollection.html#insertOne-TDocument-com.mongodb.async.SingleResultCallback- – Alexey R. Aug 30 '17 at 10:49