我怎么知道我的应用程序是否在调试模式的weblogic中运行?

时间:2014-09-09 19:53:59

标签: java debugging weblogic

假设我希望我的应用程序在WebLogic中以调试模式运行时表现不同 - 是否有可以检查的标志?

即:

if (weblogic.DEBUG) Console.writeline("running in debug mode in weblogic");

3 个答案:

答案 0 :(得分:1)

您可以向weblogic服务器添加参数,以允许您远程调试它,特别是通过在-Xdebug脚本中设置startWebLogic选项。

您可以在Oracle文档here

中查看完整示例

在这里的类似问题中:How to debug Java EE application using WebLogic 10.3

打开调试后,您可以将其从JVM args / system属性中拉出来,如下所示:How to get vm arguments from inside of java application?

如果您没有询问调试,而是询问如何判断您的服务器是否处于生产模式与开发模式,您还可以通过JMX访问isProductionModeEnabled(),如所述here

您还可以编辑服务器的日志记录级别以打开调试...

答案 1 :(得分:1)

您可以使用weblogic提供的DomainRuntimeServiceMBean和ServerRuntimeMBean。 oracle网站上有一个示例,它调用这两个MBean来获取服务器的状态。我把它贴在下面。 Source can be found here

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
public class PrintServerState {
   private static MBeanServerConnection connection;
   private static JMXConnector connector;
   private static final ObjectName service;
   // Initializing the object name for DomainRuntimeServiceMBean
   // so it can be used throughout the class.
   static {
      try {
         service = new ObjectName(
            "com.bea:Name=DomainRuntimeService,Type=weblogic.management.
             mbeanservers.domainruntime.DomainRuntimeServiceMBean");
      }catch (MalformedObjectNameException e) {
         throw new AssertionError(e.getMessage());
      }
   }
   /*
   * Initialize connection to the Domain Runtime MBean Server
   */
   public static void initConnection(String hostname, String portString, 
      String username, String password) throws IOException,
      MalformedURLException { 
      String protocol = "t3";
      Integer portInteger = Integer.valueOf(portString);
      int port = portInteger.intValue();
      String jndiroot = "/jndi/";
      String mserver = "weblogic.management.mbeanservers.domainruntime";
      JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,
         port, jndiroot + mserver);
      Hashtable h = new Hashtable();
      h.put(Context.SECURITY_PRINCIPAL, username);
      h.put(Context.SECURITY_CREDENTIALS, password);
      h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
         "weblogic.management.remote");
      connector = JMXConnectorFactory.connect(serviceURL, h);
      connection = connector.getMBeanServerConnection();
   }
   /* 
   * Print an array of ServerRuntimeMBeans.
   * This MBean is the root of the runtime MBean hierarchy, and
   * each server in the domain hosts its own instance.
   */
   public static ObjectName[] getServerRuntimes() throws Exception {
      return (ObjectName[]) connection.getAttribute(service,
         "ServerRuntimes");
   }
   /* 
   * Iterate through ServerRuntimeMBeans and get the name and state
   */
   public void printNameAndState() throws Exception {
      ObjectName[] serverRT = getServerRuntimes();
      System.out.println("got server runtimes");
      int length = (int) serverRT.length;
      for (int i = 0; i < length; i++) {
         String name = (String) connection.getAttribute(serverRT[i],
            "Name");
         String state = (String) connection.getAttribute(serverRT[i],
            "State");
         System.out.println("Server name: " + name + ".   Server state: "
            + state);
      }
   }
   public static void main(String[] args) throws Exception {
      String hostname = args[0];
      String portString = args[1];
      String username = args[2];
      String password = args[3];
      PrintServerState s = new PrintServerState();
      initConnection(hostname, portString, username, password);
      s.printNameAndState();
      connector.close();
   }
}

答案 2 :(得分:-1)

您可以在weblogic控制台中查看调试套接字日志,如下所示

在地址:xxxx

侦听传输dt_socket

其中xxxx是端口号。默认情况下,端口号为8453