简体   繁体   English

无法从Play框架2.4中的cron作业加载application.conf

[英]Not able to load application.conf from cron job in play framework 2.4

I have created a cron job that start during application restart but when i tried to create db connection i am geeting null pointer exception. 我创建了一个cron作业,该作业在应用程序重新启动期间启动,但是当我尝试创建数据库连接时,我遇到了空指针异常。 I am able to create and use db from other module using same configuration. 我能够使用相同的配置从其他模块创建和使用db。

Below is my Application.conf 以下是我的Application.conf

db.abc.driver=com.mysql.jdbc.Driver
db.abc.url="jdbc:mysql://localhost:3306/db_name?useSSL=false"
db.abc.username=root
db.abc.password=""
db.abc.autocommit=false
db.abc.isolation=READ_COMMITTED

And code that tried to access db is 试图访问数据库的代码是

public class SchduleJob extends AbstractModule{
    @Override
    protected void configure() {
        bind(JobOne.class)
        .to(JobOneImpl.class)
        .asEagerSingleton();
    }    }

@ImplementedBy(JobOneImpl.class)
public interface JobOne {}

@Singleton
public class JobOneImpl implements JobOne {
    final ActorSystem actorSystem = ActorSystem.create("name");
    final ActorRef alertActor = actorSystem.actorOf(AlertActor.props);

    public JobOneImpl() {
        scheduleJobs();
    }

    private Cancellable scheduleJobs() {
        return actorSystem.scheduler().schedule(
                Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay 0 milliseconds
                Duration.create(6, TimeUnit.MINUTES),     //Frequency 30 minutes
                alertActor,
                "alert",
                actorSystem.dispatcher(),
                null
                );
    }
}

public class AlertActor  extends UntypedActor{

public static Props props = Props.create(AlertActor.class);
final ActorSystem actorSystem = ActorSystem.create("name");
final ActorRef messageActor = actorSystem.actorOf(MessageActor.props());

@Override
public void onReceive(Object message) throws Exception {
    if(message != null && message instanceof String) {
        RequestDAO requestDAO = new RequestDAO();
        try {
            List<DBRow> rows = requestDAO.getAllRow();  
        } catch(Exception exception) {
            exception.printStackTrace();
        }
    }
}

} }

public class RequestDAO {
public List<DBRow> getAllRow() throws Exception {
        List<DBRow> rows = new ArrayList<DBRow>();
        Connection connection = null;    
        try {
            connection = DB.getDataSource("abc").getConnection();
            connection.setAutoCommit(false);
} catch(Exception exception) {
            exception.printStackTrace();
            if(connection != null) {
                connection.rollback();
            } else {
                System.out.println("in else***********");
            }
            return null;
        } finally {
            if(connection != null)
                connection.close();
        }
        return schools; 
    }

When i am calling method getAllRow() of RequestDAO class it's throwing 当我调用RequestDAO类的getAllRow()方法时,它会抛出

java.lang.NullPointerException
    at play.api.Application$$anonfun$instanceCache$1.apply(Application.scala:235)
    at play.api.Application$$anonfun$instanceCache$1.apply(Application.scala:235)
    at play.utils.InlineCache.fresh(InlineCache.scala:69)
    at play.utils.InlineCache.apply(InlineCache.scala:55)
    at play.api.db.DB$.db(DB.scala:22)
    at play.api.db.DB$.getDataSource(DB.scala:41)
    at play.api.db.DB.getDataSource(DB.scala)
    at play.db.DB.getDataSource(DB.java:33)

But same code is working without cron job. 但是,相同的代码在没有cron作业的情况下仍能正常工作。 What should i do to remove this error 我应该怎么做才能消除此错误

Play uses the Typesafe config library for configuration. Play使用Typesafe配置库进行配置。

I suspect your current working directory from the cron script isn't set, so it's probably not finding your application.conf ( application.properties ) file. 我怀疑您未设置cron脚本中的当前工作目录,因此可能找不到您的application.confapplication.properties )文件。

However, Config is nice in that it allows you to specify where to look for the file, either by its base name (to choose among .conf | .properties | .json extensions) or the filename including the extension on the java command line: 但是,Config很不错,它允许您通过文件的基本名称(在.conf | .properties | .json扩展名中选择)或文件名(包括Java命令行中的扩展名)来指定查找文件的位置:

  • To specify the base name, use -Dconfig.resource=/path/to/application 要指定基本名称,请使用-Dconfig.resource=/path/to/application
  • To specify the full name, use -Dconfig.file=/path/to/application.properties 要指定全名,请使用-Dconfig.file=/path/to/application.properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 play 框架中的 application.conf 文件中读取属性? - How to read a property from the application.conf file in play framework? Play Framework + Spring:从application.conf注入URL - Play Framework + Spring: Injecting URL from application.conf 如何在Glassfish上部署Play Framework 2.4应用程序并阅读application.conf - How to deploy play framework 2.4 application on glassfish and read application.conf 无法访问Play框架中的Cloud Foundry自动配置(application.conf文件) - Not able to access cloud foundry auto configuration in play framework (application.conf file) 在GlobalSettings onStart(Play Framework)中访问application.conf - Accessing application.conf in GlobalSettings onStart (Play Framework) Play Framework 2.7.0 Config,无需通过application.conf进行常规配置即可进行测试 - Play Framework 2.7.0 Config for tests without general configurations from application.conf 使用 Play 从 java 类访问 application.conf 属性! 2.0 - Accessing the application.conf properties from java class with Play! 2.0 Java Play 覆盖 application.conf 中的端口 - Java Play overwrites port in application.conf 在Play Java应用中标记化application.conf - tokenize application.conf in play java app Akka 不加载 application.conf - Akka doesn't load application.conf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM