绑定(....类).asEagerSingleton()在UntypedActor Play 2.5 java中不起作用

时间:2016-10-11 21:17:40

标签: playframework

我正在使用Play 2.5 Java。

当我运行与#34; activator dist"打包的应用程序时,急切的加载对我不起作用。或使用"激活剂testProd"命令。它只适用于"激活器运行"。

我的单例类在Module类中被绑定为急切的单例。

public class Module extends AbstractModule {

    @Override
    public void configure() {
        bind(DataStaxUtil.class).asEagerSingleton();
    }
}

在actor类中,数据库会话返回null。

public class AttendeeActor extends UntypedActor {

    private static Session session = DataStaxUtil.getSession();
    public static Props props = Props.create(AttendeeActor.class);

    @Override
    public void onReceive(Object msg) throws Throwable {
    ...

"激活器testProd"

发生错误
[info] application - DataStaxUtil->getSession session=null
Uncaught error from thread [application-akka.actor.default-dispatcher-2] shutting down JVM since 'akka.jvm-exit-on-fatal-error' is enabled for ActorSystem[application]
java.lang.ExceptionInInitializerError
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at akka.util.Reflect$.instantiate(Reflect.scala:44)
    at akka.actor.NoArgsReflectConstructor.produce(IndirectActorProducer.scala:105)
    at akka.actor.Props.newActor(Props.scala:213)
    at akka.actor.ActorCell.newActor(ActorCell.scala:562)
    at akka.actor.ActorCell.create(ActorCell.scala:588)
    at akka.actor.ActorCell.invokeAll$1(ActorCell.scala:461)
    at akka.actor.ActorCell.systemInvoke(ActorCell.scala:483)
    at akka.dispatch.Mailbox.processAllSystemMessages(Mailbox.scala:282)
    at akka.dispatch.Mailbox.run(Mailbox.scala:223)
    at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.lang.NullPointerException
    at actors.AttendeeActor.<clinit>(AttendeeActor.java:29)
    ... 19 more

添加了DataStaxUtil.java:

package services;

import java.util.concurrent.CompletableFuture;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

import play.Logger;
import play.inject.ApplicationLifecycle;

@Singleton
public class DataStaxUtil {

    private static final Config config = ConfigFactory.load();
    private static final String cassandraHost =     config.getString("datastax.url"); 
    private static final int cassandraPost= config.getInt("datastax.port");
    private static final String keyspace = config.getString("datastax.keyspace");
    private static Cluster cluster;
    private static Session session;

    @Inject
    private DataStaxUtil(ApplicationLifecycle lifecycle) {
        cluster = Cluster.builder()
            .addContactPoint(cassandraHost)
            .withPort(cassandraPost)
            .withCredentials(config.getString("datastax.username"), config.getString("datastax.password"))
            .build();

        session = cluster.connect(keyspace);

        Logger.info("DataStaxUtil->DataStaxUtil session=" + session);

        lifecycle.addStopHook(() -> {
            session.close();
            cluster.close();
            return CompletableFuture.completedFuture(null);
        });
    }

    public static Session getSession() {
        Logger.info("DataStaxUtil->getSession session=" + session);

        return session;     
    }
}

我该怎么做才能排除故障?谢谢!

0 个答案:

没有答案