[英]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.conf
( application.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命令行中的扩展名)来指定查找文件的位置:
-Dconfig.resource=/path/to/application
要指定基本名称,请使用-Dconfig.resource=/path/to/application
-Dconfig.file=/path/to/application.properties
要指定全名,请使用-Dconfig.file=/path/to/application.properties
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.