- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 cargo maven 插件将我的 .war 部署到 Tomcat 8 实例以进行集成测试。该应用程序是一个基于 jersey 的 REST web 界面。我的问题出现在底层数据库连接池中。在集成测试中,有一个 NoInitialContextException 导致一个 NullPointerexception。
我构建了 2 个池(1 个用于应用程序,1 个用于 oacc):
public class DBCPool {
static final Logger LOGGER = LogManager.getRootLogger();
static DataSource dbs = null;
static DataSource oaccDS = null;
private DBCPool() {};
public static boolean startup() {
if (dbs != null && oaccDS != null) {
LOGGER.trace("DBCPool startup when pool != null");
return true;
} else {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Context ctxt = new InitialContext();
LOGGER.debug("Initial context is: " + ctxt.getNameInNamespace());
if (dbs == null) {
LOGGER.trace("GTM DBCPool startup");
dbs = (DataSource) ctxt.lookup("java:comp/env/jdbc/gtmdb");
LOGGER.trace("Set dbs as " + dbs.toString());
}
if (oaccDS == null) {
LOGGER.trace("OACC DBCPool startup");
oaccDS = (DataSource) ctxt.lookup("java:comp/env/jdbc/oaccdb");
}
return true;
} catch (Exception e) {
LOGGER.catching(e);
return false;
}
}
}
public static Connection getConnection() throws SQLException {
if (dbs == null) {
LOGGER.debug("Had to start DBCPool in getConnection.");
}
return dbs.getConnection();
}
现在我添加了一些日志记录以了解错误,稍后将删除。
startup() 在 contextInitialized 上调用,因此 DBCPool 在应用程序运行时设置。
作为集成测试,我写了:
@Test
public void getConnectionShouldReturnConnection() throws SQLException {
DBCPool.startup();
assertEquals(true, DBCPool.getConnection() != null);
}
DBCPool.startup() 对于测试来说是不必要的,但在这里有帮助,因为它为登录 cli 带来了底层异常。
然后我使用 cargo maven 插件将其部署到 tomcat 8 实例,并使用故障安全功能进行集成测试。这是 pom.xml 片段:
<profile>
<id>run-its</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>failsafe-it</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>failsafe-verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.5.0</version>
<configuration>
<container>
<containerId>tomcat8x</containerId>
</container>
<configuration>
<home>${project.build.directory}/catalina-base</home>
</configuration>
</configuration>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
tomcat 已启动并部署了应用程序。然后甚至设置了 DBCPool,并且在记录数据源的 toString() 时我没有得到 NullPointerException。这是启动日志,log4j部分因为太长被删掉了。
[INFO] --- cargo-maven2-plugin:1.5.0:start (start-server) @ gtm ---
[INFO] [2.ContainerStartMojo] Resolved container artifact org.codehaus.cargo:cargo-core-container-tomcat:jar:1.5.0 for container tomcat8x
[INFO] You did not specify a container home nor any installer. CARGO will automatically download your container's binaries from [http://repo1.maven.org/maven2/org/apache/tomcat/tomcat/8.0.35/tomcat-8.0.35.zip].
[INFO] [talledLocalContainer] Tomcat 8.x starting...
[INFO] [stalledLocalDeployer] Deploying [C:\Users\timki\git\gtm-code\gtm\target\gtm.war] to [C:\Users\timki\git\gtm-code\gtm\target/catalina-base/webapps]...
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:35 AM org.apache.catalina.startup.VersionLoggerListener log
[...]
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:35 AM org.apache.catalina.startup.HostConfig deployWAR
[INFO] [talledLocalContainer] INFORMATION: Deploying web application archive C:\Users\timki\git\gtm-code\gtm\target\catalina-base\webapps\gtm.war
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:36 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
[WARNING] [talledLocalContainer] WARNUNG: Name = oaccdb Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "50" for "maxActive" property, which is being ignored.
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:36 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
[WARNING] [talledLocalContainer] WARNUNG: Name = oaccdb Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "5000" for "maxWait" property, which is being ignored.
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:36 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
[WARNING] [talledLocalContainer] WARNUNG: Name = gtmLog Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "30" for "maxActive" property, which is being ignored.
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:36 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
[WARNING] [talledLocalContainer] WARNUNG: Name = gtmLog Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "5000" for "maxWait" property, which is being ignored.
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:36 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
[WARNING] [talledLocalContainer] WARNUNG: Name = gtmdb Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "50" for "maxActive" property, which is being ignored.
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:36 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
[WARNING] [talledLocalContainer] WARNUNG: Name = gtmdb Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "5000" for "maxWait" property, which is being ignored.
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:36 AM org.apache.jasper.servlet.TldScanner scanJars
[INFO] [talledLocalContainer] INFORMATION: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[INFO] [talledLocalContainer] 2016-07-17 10:07:37,164 localhost-startStop-1 DEBUG Initializing configuration XmlConfiguration[location=C:\Users\timki\git\gtm-code\gtm\target\catalina-base\webapps\gtm\WEB-INF\classes\log4j2.xml]
[INFO] [talledLocalContainer] 2016-07-17 10:07:37,171 localhost-startStop-1 DEBUG Installed script engines
[INFO] [talledLocalContainer] 2016-07-17 10:07:37,440 localhost-startStop-1 DEBUG Oracle Nashorn Version: 1.8.0_73, Language: ECMAScript, Threading: Not Thread Safe, Compile: true, Names: {nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript}
[INFO] [talledLocalContainer] 2016-07-17 10:07:37,441 localhost-startStop-1 DEBUG PluginManager 'Core' found 99 plugins
[INFO] [talledLocalContainer] 2016-07-17 10:07:37,441 localhost-startStop-1 DEBUG PluginManager 'Level' found 0 plugins
[INFO] [talledLocalContainer] 2016-07-17 10:07:37,445 localhost-startStop-1 DEBUG No scheduled items
[INFO] [talledLocalContainer] 2016-07-17 10:07:37,445 localhost-startStop-1 DEBUG PluginManager 'Lookup' found 13 plugins
[INFO] [talledLocalContainer] 2016-07-17 10:07:37,447 localhost-startStop-1 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
[...]
[INFO] [talledLocalContainer] 10:07:37.558 [localhost-startStop-1] INFO - GTM servlet context initialized
[INFO] [talledLocalContainer] 10:07:37.565 [localhost-startStop-1] DEBUG - Initial context is: java:
[INFO] [talledLocalContainer] 10:07:37.565 [localhost-startStop-1] TRACE - GTM DBCPool startup
[INFO] [talledLocalContainer] 10:07:37.565 [localhost-startStop-1] TRACE - Set dbs as org.apache.tomcat.dbcp.dbcp2.BasicDataSource@79270e48
[INFO] [talledLocalContainer] 10:07:37.565 [localhost-startStop-1] TRACE - OACC DBCPool startup
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:37 AM com.sun.jersey.api.core.PackagesResourceConfig init
[INFO] [talledLocalContainer] INFORMATION: Scanning for root resource and provider classes in the packages:
[INFO] [talledLocalContainer] de.osg.gtm.web
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:37 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
[INFO] [talledLocalContainer] INFORMATION: Root resource classes found:
[INFO] [talledLocalContainer] class de.osg.gtm.web.FrontendService
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:37 AM com.sun.jersey.api.core.ScanningResourceConfig init
[INFO] [talledLocalContainer] INFORMATION: No provider classes found.
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:37 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
[INFO] [talledLocalContainer] INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 03:25 AM'
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:38 AM org.apache.catalina.startup.HostConfig deployWAR
[INFO] [talledLocalContainer] INFORMATION: Deployment of web application archive C:\Users\timki\git\gtm-code\gtm\target\catalina-base\webapps\gtm.war has finished in 2,448 ms
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:38 AM org.apache.catalina.startup.HostConfig deployDirectory
[INFO] [talledLocalContainer] INFORMATION: Deploying web application directory C:\Users\timki\git\gtm-code\gtm\target\catalina-base\webapps\host-manager
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:38 AM org.apache.catalina.startup.HostConfig deployDirectory
[INFO] [talledLocalContainer] INFORMATION: Deployment of web application directory C:\Users\timki\git\gtm-code\gtm\target\catalina-base\webapps\host-manager has finished in 31 ms
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:38 AM org.apache.catalina.startup.HostConfig deployDirectory
[INFO] [talledLocalContainer] INFORMATION: Deploying web application directory C:\Users\timki\git\gtm-code\gtm\target\catalina-base\webapps\manager
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:38 AM org.apache.catalina.startup.HostConfig deployDirectory
[INFO] [talledLocalContainer] INFORMATION: Deployment of web application directory C:\Users\timki\git\gtm-code\gtm\target\catalina-base\webapps\manager has finished in 24 ms
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:38 AM org.apache.coyote.AbstractProtocol start
[INFO] [talledLocalContainer] INFORMATION: Starting ProtocolHandler ["http-nio-8080"]
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:38 AM org.apache.coyote.AbstractProtocol start
[INFO] [talledLocalContainer] INFORMATION: Starting ProtocolHandler ["ajp-nio-8009"]
[INFO] [talledLocalContainer] Jul 17, 2016 10:07:38 AM org.apache.catalina.startup.Catalina start
[INFO] [talledLocalContainer] INFORMATION: Server startup in 2821 ms
[INFO] [talledLocalContainer] Tomcat 8.x started on port [8080]
[INFO]
[INFO] --- maven-failsafe-plugin:2.19.1:integration-test (failsafe-it) @ gtm ---
[INFO] Failsafe report directory: C:\Users\timki\git\gtm-code\gtm\target\failsafe-reports
-------------------------------------------------------
T E S T S
从中可以看出
[INFO] [talledLocalContainer] 10:07:37.558 [localhost-startStop-1] INFO - GTM servlet context initialized
[INFO] [talledLocalContainer] 10:07:37.565 [localhost-startStop-1] DEBUG - Initial context is: java:
[INFO] [talledLocalContainer] 10:07:37.565 [localhost-startStop-1] TRACE - GTM DBCPool startup
[INFO] [talledLocalContainer] 10:07:37.565 [localhost-startStop-1] TRACE - Set dbs as org.apache.tomcat.dbcp.dbcp2.BasicDataSource@79270e48
[INFO] [talledLocalContainer] 10:07:37.565 [localhost-startStop-1] TRACE - OACC DBCPool startup
我从查找中获得了 initialContext 甚至数据源。但是,在测试中:
10:07:39.631 [main] ERROR - Catching
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662) ~[?:1.8.0_73]
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313) ~[?:1.8.0_73]
at javax.naming.InitialContext.getNameInNamespace(InitialContext.java:563) ~[?:1.8.0_73]
at de.osg.gtm.management.DBCPool.startup(DBCPool.java:34) [classes/:?]
at de.osg.gtm.management.DBCPoolIT.getConnectionShouldReturnConnection(DBCPoolIT.java:13) [test-classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_73]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_73]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_73]
at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_73]
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) [junit-4.8.2.jar:?]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) [junit-4.8.2.jar:?]
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) [junit-4.8.2.jar:?]
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) [junit-4.8.2.jar:?]
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) [junit-4.8.2.jar:?]
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) [junit-4.8.2.jar:?]
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) [junit-4.8.2.jar:?]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) [junit-4.8.2.jar:?]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) [junit-4.8.2.jar:?]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) [junit-4.8.2.jar:?]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) [junit-4.8.2.jar:?]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) [junit-4.8.2.jar:?]
at org.junit.runners.ParentRunner.run(ParentRunner.java:236) [junit-4.8.2.jar:?]
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:367) [surefire-junit4-2.19.1.jar:2.19.1]
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:274) [surefire-junit4-2.19.1.jar:2.19.1]
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238) [surefire-junit4-2.19.1.jar:2.19.1]
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:161) [surefire-junit4-2.19.1.jar:2.19.1]
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290) [surefire-booter-2.19.1.jar:2.19.1]
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242) [surefire-booter-2.19.1.jar:2.19.1]
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121) [surefire-booter-2.19.1.jar:2.19.1]
10:07:39.637 [main] DEBUG - Had to start DBCPool in getConnection.
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.684 sec <<< FAILURE! - in de.osg.gtm.management.DBCPoolIT
getConnectionShouldReturnConnection(de.osg.gtm.management.DBCPoolIT) Time elapsed: 0.656 sec <<< ERROR!
java.lang.NullPointerException
at de.osg.gtm.management.DBCPoolIT.getConnectionShouldReturnConnection(DBCPoolIT.java:14)
原来如此。我设置了 DBCPool,但随后在测试 dbs(数据库源,DBCPool 实例)时再次为 null,无法检索到 initialContext。
我想我在配置故障安全时做错了什么,因此它不会测试已部署的应用程序,因此不会获得上下文。但无论我学习了多少示例和教程,我都没有发现我的错误。我用maven tomcat插件有同样的错误,希望用cargo maven插件解决,但是没有。
在此先感谢您的帮助。机枪
最佳答案
您是否考虑过 maven 生命周期以及 cargo-maven2-plugin 何时开始工作?也许应用程序将在集成测试后部署。
如果生命周期是这样的:
Maven: clean > test > it-test > install > deploy
您的 it-suite
将在没有必要的 tomcat 容器的情况下运行。并有充分的理由失败。
也许你将你的个人资料run-its
分成:
这种方法将确保 tomcat 容器正常运行。
关于maven - Cargo Maven Tomcat NoInitialContextException 当我已经有了 Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38419401/
getConnection 异常 javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者作为小程序参数,或者在应用程序资源文件中指定类名:j
我正在为我的 EJB 编写一个客户端,在尝试执行它时,我得到以下异常: javax.naming.NoInitialContextException: Need to specify class na
我一直试图在这里找到解决方案,但我不能...我有以下代码,但出现此错误。 我错过了什么吗?谢谢:) 代码 package src; import java.sql.DriverManager; imp
这个问题在这里已经有了答案: The meaning of NoInitialContextException error (13 个答案) 关闭 6 年前。 这是我的代码: import java
有人可以告诉我为什么会出现以下错误吗? javax.naming.NoInitialContextException: Need to specify class name in environmen
我们拥有在 Servicemix 5 上完美运行的 osgi 应用程序。但由于某些原因,我们必须迁移到 Servicemix 4.5。我尝试了一下,得到了这个 Error parsing SQL Ma
我需要将 java 应用程序连接到 MBean 服务器,但我在使 JMXConnector 工作时遇到问题。 当我尝试运行此命令时,出现 NoInitialContextException: try
对于学校,他们让我们通过普通的旧 dao 和 tomcat 连接到 postgresDB。但是,给定的代码无法正常工作,我现在已经被困在这里很长一段时间了。 就这样吧。 给定的connectionda
我有一个使用 JNDI 的应用程序,首先它可以工作,在我创建一个新项目后,由于与 SVN 冲突,它现在抛出 NoInitialContextException。 这是它抛出的异常: NoInitial
我在 Tomcat 服务器上有一个带有 Java Servlet 的网络服务。在我的 Servlet 中,我使用这样的数据库池: envContext = new InitialContext();
这个问题在这里已经有了答案: The meaning of NoInitialContextException error (13 个答案) 关闭 9 年前。 如标题所示,“javax.naming
我正在使用 Apache Commons 电子邮件 API 尝试以下代码,它在上下文中抛出错误。有什么帮助吗? 导入java.util.Properties; 导入 javax.mail.Passwo
我是 Hibernate 新手。使用 Eclipse indigo 中的 Hibernate 3.0。 这里讨论了这个话题,答案没有帮助,Hibernate: javax.naming.NoIniti
尝试连接到 MySQL 数据库 MysqlDataSource mysqlDs = new MysqlDataSource(); Properties prop = new Properties();
我正在尝试使用 JNDI 连接 mysql。但它显示异常 javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者作为小程序参数,或者在应用程
我想在 Jetty 6 中使用连接池,但到目前为止它给我带来了很多麻烦。 我愿意 InitialContext context = new InitialContext(); 它没有抛出异常,但是当我
我目前面临的问题是,我将 api 打包为 WAR(使用 eclipse + maven)后无法在 Jetty 上启动。从 Eclipse 启动时工作正常。 我查看了 Jetty 的日志并发现: org
我正在尝试编写一个 JMS 应用程序,我使用 glassFish 管理页面构建了一个 ConnectionFactory 和 Qeueu,我想知道如何让我的应用程序知道服务器上构建的 jndi 以便能
我正在使用 cargo maven 插件将我的 .war 部署到 Tomcat 8 实例以进行集成测试。该应用程序是一个基于 jersey 的 REST web 界面。我的问题出现在底层数据库连接池中
我有一个 JSF 网络应用程序,我想使用嵌入式 tomcat 运行它。到目前为止它一直在工作 [包括 JDBCRealm,在以下代码片段的 context.xml 中指定],除了登录后我的代码无法实际
我是一名优秀的程序员,十分优秀!