【问题标题】:How to get list of JNDI Names from Weblogic Server?如何从 Weblogic Server 获取 JNDI 名称列表?
【发布时间】:2014-03-20 05:40:00
【问题描述】:

我正在使用以下方法获取在 Tomcat 服务器的 context.xml 中配置的数据源列表:

public static List<String> getDataSourcesList() {

  List<String> dataSourceList = new ArrayList<String>();
  try {
     if( initialContext == null ) {
        initialContext = new InitialContext();
     }
     NamingEnumeration<NameClassPair> list = ( ( Context )initialContext.lookup( DATASOURCE_CONTEXT ) ).list( "" );
     while( list.hasMore() ) {
        dataSourceList.add( list.next().getName() );
     }
  }
  catch( NamingException ex ) {
     Logger.getLogger( JDBCUtil.class.getName() ).log( Level.SEVERE, null, ex );
  }
  return dataSourceList;

}

但此方法不适用于 Weblogic 和 Websphere 服务器。

如何获取 Weblogic/WebSphere 服务器上配置的数据源名称列表?

有没有办法获取数据源名称列表?

【问题讨论】:

  • 我认为这就是您要寻找的:javaoraclesoa.blogspot.com/2012/09/… 您可以通过 Java 代码或 WLST 来实现。
  • 当您说“它不起作用”时,究竟会发生什么?您是否得到一个列表,但它不包括您正在寻找的对象?还是在搜索过程中抛出异常?

标签: java websphere weblogic


【解决方案1】:

对于 WebSphere Application Server,只需使用位于 PROFILE_ROOT\bin 中的 dumpNameSpace 命令行工具

您的代码稍作修改后在 WAS 8.5.5 中运行良好:

      try {
         InitialContext initialContext = new InitialContext();
         NamingEnumeration<NameClassPair> list = ((Context)initialContext.lookup( "jdbc" ) ).list( "" );
         while( list.hasMore() ) {
            System.out.println(list.next().getName() );
         }
      }
      catch( NamingException ex ) {
         ex.printStackTrace();
      }

【讨论】:

    【解决方案2】:
    package jms.queue;
    
    import java.io.PrintWriter;
    import java.io.StringWriter;
    
    import java.sql.Connection;
    
    import java.sql.Statement;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Date;
    import java.util.Hashtable;
    
    import javax.naming.Binding;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingEnumeration;
    import javax.naming.NamingException;
    
    import javax.sql.DataSource;
    
    public class CheckDB {
    
        public CheckDB() {
            super();
        }
    
        public static void main(String[] args) {
            CheckDB checkDB = new CheckDB();
            System.out.println(checkDB.listJDBCContextTable());
        }
    
        private Context getContext() throws NamingException {
            Hashtable myCtx = new Hashtable();
            myCtx.put(Context.INITIAL_CONTEXT_FACTORY,
                      "weblogic.jndi.WLInitialContextFactory");
    //        myCtx.put(Context.PROVIDER_URL, "t3://172.30.60.76:7001"); //Admin Server
            myCtx.put(Context.PROVIDER_URL, "t3://astpdsoam03.auca.corp:8001"); // SOA Cluster
            Context ctx = new InitialContext(myCtx);
            return ctx;
        }
    
        private String checkDataSource(DataSource ds) {
            try {
                Connection conn = ds.getConnection();
                Statement st = conn.createStatement();
                st.execute("select sysdate mydate from dual");
                st.getResultSet().next();
                Date mydate = st.getResultSet().getDate("mydate");
                conn.close();
                String date = mydate.toString();
                if (date.length() == 10 && date.indexOf("-") == 4 &&
                    date.lastIndexOf("-") == 7) {
                    return "OK";
                } else {
                    return "NOK";
                }
            } catch (Exception e) {
                return "NOK"; //getStackTrace(e);
            }
        }
    
        private static String getStackTrace(Throwable e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            return sw.toString();
        }
    
        private String listJDBCContextTable() {
            String output = "<table>";
            ArrayList<String> tab = new ArrayList<String>();
            String line = "";
            try {
                tab = listContext((Context)getContext().lookup("jdbc"), "", tab);
                Collections.sort(tab);
                for (int i = 0; i < tab.size(); i++) {
                    output += tab.get(i);
                }
                output += "</table>";
                return output;
            } catch (NamingException e) {
                return getStackTrace(e);
            }
        }
    
        private ArrayList<String> listContext(Context ctx, String indent,
                                              ArrayList<String> output) throws NamingException {
            String name = "";
            try {
                NamingEnumeration list = ctx.listBindings("");
                while (list.hasMore()) {
                    Binding item = (Binding)list.next();
                    String className = item.getClassName();
                    name = item.getName();
    //                System.out.println("Name : " + name);
                    if (!(item.getObject() instanceof DataSource)) {
                        //output = output+indent + className + " " + name+"\n";
                    } else {
                        output.add("<tr><td>" + name + "</td><td>" +
                                   checkDataSource((DataSource)item.getObject()) +
                                   "</td></tr>\n");
                    }
                    Object o = item.getObject();
                    if (o instanceof javax.naming.Context) {
                        listContext((Context)o, indent + " ", output);
                    }
                }
            } catch (NamingException ex) {
                output.add("<tr><td>" + name + "</td><td>" + getStackTrace(ex) +
                           "</td></tr>\n");
            }
            return output;
        }
    }
    

    【讨论】:

    • 一些解释(用文字)而不是代码转储会很有帮助。
    • 同意@alexw,请解释代码,以便提问者更彻底地掌握所涉及的概念,并在以后应用它们
    猜你喜欢
    • 2013-11-07
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多