我正在将我的应用程序 Web 逻辑移动到 Tomcat7

Posted

技术标签:

【中文标题】我正在将我的应用程序 Web 逻辑移动到 Tomcat7【英文标题】:I am Moving my application Web-logic to Tomcat7 【发布时间】:2015-04-22 18:43:41 【问题描述】:

当应用程序在 weblogic 中运行时,以下代码工作正常,但在 tomcat 中却出现错误。 主要问题是加载java:comp/env/jmx/runtime

所以请让我知道我需要做什么配置才能在 tomcat 中运行代码。

我的代码:

    MBeanServer server = (MBeanServer)ctx.lookup("java:comp/env/jmx/runtime");
    // Get the Platform MBean Server
    ObjectName rs = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
    HashMap map = null;
    try
    
        ObjectName domCfg = (ObjectName) server.getAttribute(rs,"DomainConfiguration");

        ObjectName[] jdbcSysResources =
        (ObjectName[]) server.getAttribute(domCfg, "JDBCSystemResources");

        map = new HashMap();
        for (int i=0 ; i<jdbcSysResources.length ; i++)
        
            ObjectName jdbcResourceBean = (ObjectName) server.getAttribute(jdbcSysResources[i],"JDBCResource");

            ObjectName driverParamsBean =(ObjectName)server.getAttribute(jdbcResourceBean,"JDBCDriverParams");
            StringBuffer jdbcParams = new StringBuffer();
            ObjectName drvPropertiesBean = (ObjectName)server.getAttribute(driverParamsBean,"Properties");
            ObjectName[] drvProperties = (ObjectName[])server.getAttribute(drvPropertiesBean,"Properties");

            for(int j=0; j<drvProperties.length; j++)
            
                String propName = (String)server.getAttribute(drvProperties[j],"Name");
                String propVal = (String)server.getAttribute(drvProperties[j],"Value");
                jdbcParams.append(propName).append("=").append(propVal).append(";");
            
            String strTest = drvProperties.toString();

            ObjectName dsnParams =(ObjectName)server.getAttribute(jdbcResourceBean,"JDBCDataSourceParams"); 
            String[] dsnParamNames = (String[])server.getAttribute(dsnParams,"JNDINames");
            map.put(dsnParamNames[0].toString(),jdbcParams.toString());

tomcat 服务器出错:-

SystemCheck - Naming Exception: javax.naming.NameNotFoundException: 
Name [jmx/runtime] is not bound in this Context. Unable to find [jmx].
04/22 01:32 SystemAvailability -> MEL check failed - null
java.lang.NullPointerException

Tomcat Context.xml--- -- 数据源 1 -- -- 数据源 2 -- . ..

    < Resource  name="jdbc/datasource2"  auth="Container" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" maxActive="50" maxIdle="10" maxWait="10000"password="xxxx" type="javax.sql.DataSource" url="jdbc:sqlserver://xxxx:1460;databaseName=xxx" username="xxx" validationQuery="SELECT 1"/>

Web.xml---

  <resource-ref>
    <res-ref-name>jdbc/datasouce1</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>CONTAINER</res-auth>
</resource-ref>

<resource-ref>
    <res-ref-name>jdbc/datasouce2</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>CONTAINER</res-auth>
</resource-ref> 

【问题讨论】:

看看***.com/questions/47676/… 【参考方案1】:

与 Weblogic 不同,Tomcat 不附带 JNDI 代理。要使代码按原样工作,您需要添加一些 JNDI 提供程序。

我建议改用 DI。

spring 框架可以做任何一个(例如)。

Here's an example

【讨论】:

这是一个遗留应用程序。我不能在这里使用 DI/Spring。所以请告诉我如何在 tomcat 中查找所有 JNDI。

以上是关于我正在将我的应用程序 Web 逻辑移动到 Tomcat7的主要内容,如果未能解决你的问题,请参考以下文章