簡體   English   中英

無法從Play框架2.4中的cron作業加載application.conf

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

我創建了一個cron作業,該作業在應用程序重新啟動期間啟動,但是當我嘗試創建數據庫連接時,我遇到了空指針異常。 我能夠使用相同的配置從其他模塊創建和使用db。

以下是我的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

試圖訪問數據庫的代碼是

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; 
    }

當我調用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)

但是,相同的代碼在沒有cron作業的情況下仍能正常工作。 我應該怎么做才能消除此錯誤

Play使用Typesafe配置庫進行配置。

我懷疑您未設置cron腳本中的當前工作目錄,因此可能找不到您的application.confapplication.properties )文件。

但是,Config很不錯,它允許您通過文件的基本名稱(在.conf | .properties | .json擴展名中選擇)或文件名(包括Java命令行中的擴展名)來指定查找文件的位置:

  • 要指定基本名稱,請使用-Dconfig.resource=/path/to/application
  • 要指定全名,請使用-Dconfig.file=/path/to/application.properties

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM