0

Say that i want my application to behave differently when running in debug mode in WebLogic - is there a flag that i can check?

ie:

if (weblogic.DEBUG) Console.writeline("running in debug mode in weblogic");
blueberryfields
  • 45,910
  • 28
  • 89
  • 168

3 Answers3

1

You can add arguments to your weblogic server to allow you to debug it remotely, specifically by setting the -Xdebug option in your startWebLogic script.

You can see a full example in the Oracle docs here

And in a similar question here: How to debug Java EE application using WebLogic 10.3

Once you have debug turned on you could pull it out of the JVM args/system properties like shown here: How to get vm arguments from inside of java application?

If you're not asking about debug but instead asking how to tell if your server is in production mode vs development mode, you can also access isProductionModeEnabled() via JMX, as described here

You can also edit the logging levels for your servers to turn debug on...

Community
  • 1
  • 1
Display Name is missing
  • 6,197
  • 3
  • 34
  • 46
  • unfortunately, the linked answers don't mention how one might find an argument passed in with the -X option (-Xdebug doesn't seem to be available through System.getProperty()) – blueberryfields Sep 09 '14 at 20:24
  • You can certainly add your own additional property value to the `startWebLogic` script itself to go along with the options like -Xdebug so you can pull out `System.getProperty('myCustomDebugOption')`. – Display Name is missing Sep 09 '14 at 21:04
  • that's too heavyweight. i'd rather just find out if weblogic is running in debug mode, without messing with any of the scripts etc... – blueberryfields Sep 09 '14 at 21:12
1

You can use the DomainRuntimeServiceMBean and ServerRuntimeMBean provided by weblogic. There is an example on the oracle website of invoking these 2 MBeans to get the status of the server. I've pasted it below. 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();
   }
}
Bistro
  • 56
  • 4
  • I wonder if the entire code was necessary since the gist is ` String state = (String) connection.getAttribute(serverRT[i], "State");` Answers should be to the point without anyone having read lot of code. – bhantol Sep 15 '14 at 17:50
  • The entire source is helpful since it is stand-alone and can be run for quick evaluation. Also, I couldn't actually get direct link to the file just the main MBean reference page – Bistro Sep 15 '14 at 18:39
  • Ok I see - the purpose of your snippet was provide some code to dump the properties rather than explain them that they can use `getAttribute(mbean, 'State')` which was burried in it. – bhantol Sep 15 '14 at 18:43
-1

You can the see debug socket log in the weblogic console like below,

Listening for transport dt_socket at address: xxxx

where xxxx is port number. By default the port number would be 8453