-1

The below code is working fine while the application is running in weblogic but in tomcat it is giving an error. The main problem is loading java:comp/env/jmx/runtime.

So please let me know what configuration I need to do to run the code in tomcat.

My Code:

    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());

Error in tomcat server:-

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--- -- Data source 1 --
< Resource name="jdbc/datasource1" auth="Container" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" maxActive="50" maxIdle="10" maxWait="10000" password="mel01" type="javax.sql.DataSource" url="jdbc:sqlserver://xxx:1460;databaseName=xxx" username="xxx" validationQuery="SELECT 1"/> -- Data source 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> 

1 Answers1

0

Tomcat doesn't come with a JNDI broker, unlike Weblogic. To make the code work as is, you'd need to add some JNDI provider.

I'd recommend using DI instead.

spring framework can do either (for example).

Here's an example

Kraylog
  • 7,383
  • 1
  • 24
  • 35