Amit Pal
Amit Pal

Reputation: 11052

How to call onStart method in play framework Global setting?

I am using following code:

package common;

import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import play.GlobalSettings;
import java.util.Arrays;


public class Global extends GlobalSettings {

private static Datastore datastore;


public static Datastore getDatastore() {
    return datastore;
}

@Override
public void onStart(play.Application config) {
    super.beforeStart(config);

    int mongoPort = config.configuration().getInt("mongo.port");
    MongoClient mongoClient = new MongoClient(Arrays.asList(
            new ServerAddress(config.configuration().getString("mongo.server1"), mongoPort)));

    Morphia morphia = new Morphia();
    datastore = morphia.createDatastore(mongoClient, config.configuration().getString("mongo.db"));
    datastore.ensureIndexes();
}
}

I am calling getDatastore() method by :

public void save() {
    Global.getDatastore().save(this);
}

but it throws an nullpointer exception on return datastore line. When I put debug point on onStart method it didn't even get called.

What I am doing wrong here?

Upvotes: 0

Views: 513

Answers (1)

mkurz
mkurz

Reputation: 2826

You have to set

application.global=common.Global

in your application.conf

Upvotes: 1

Related Questions