如何使用heroku的webapp-runner.jar运行cbioportal tomcat war

时间:2015-12-11 01:17:33

标签: java maven tomcat heroku jndi

我一直在关注Heroku(https://devcenter.heroku.com/articles/java-webapp-runner)的教程,学习如何使用webapp-runner.jar运行war文件。现在我想在heroku上运行cbioportal(https://github.com/cbioportal/cbioportal)。我已设法将webapp-runner.jar添加为依赖项,请参阅:https://github.com/inodb/cbioportal/tree/heroku

当我从repo目录运行以下命令时:

java -Djava.naming.factory.initial=org.apache.naming.java.javaURLContextFactory \
    -jar portal/target/dependency/webapp-runner.jar --port 9099 portal/target/portal

我收到这样的错误:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityMapper' defined in class path resource [applicationContext-business.xml]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext-business.xml]: Cannot resolve reference to bean 'businessDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'businessDataSource' defined in class path resource [applicationContext-business.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/mydb] is not bound in this Context. Unable to find [java:comp].

我已经尝试传递我本地Tomcat安装的context.xml并直接在文件中设置mysql连接字符串,但无济于事。

1 个答案:

答案 0 :(得分:3)

我认为您需要在context.xml文件中将DB URL注册为JNDI资源,可能是这样的:

<Context>
  <Resource name="jdbc/cbioportal" auth="Container" type="javax.sql.DataSource"
               maxTotal="100" maxIdle="30" maxWaitMillis="10000"
               username="user" password="pass" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/dbname"/>
</Context>

然后您需要将该上下文XML作为选项添加到java命令中,如下所示:

$ java ... \
    -jar portal/target/dependency/webapp-runner.jar \
    --context-xml context.xml --port 9099 \
    portal/target/portal

对于url中的context.xml,您必须从$ heroku config获取并将其转换为jdbc网址。它在运行时以$JDBC_DATABASE_URL形式提供,但我不确定如何动态地将其导入context.xml。

出于这个原因,我认为如果可能的话,你最好不要使用JNDI。你可以直接在你的应用程序中配置DB参数吗?

我从this line in your config

获得了JNDI名称

有关Tomcat的JNDI的更多信息,请参阅the Tomcat docs

有关webapp-runner选项的更多信息,请参阅project readme