Search code examples
javajmxjdevelopermbeans

Getting Dump from weblogic server programatically


I have a requirement where I need to design a small app which contains a button. When the user clicks on that, it will connect to corresponding weblogic server and get the MDS dump ZIP file located in particular location downloaded for the user. I need to implement this programmatically using java language and preferably using MBeans.

I am new to this MBeans concept. Can any one help me out on how to figure out the correct MBean to get access to the dump files and download them?


Solution

  • I have already done something like this: here all my code:

    public class HeapGenerator {
    
    private HeapGenerator() {
    
    }
    
    private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
    private static long lastHeapDumpGenrationTime ;
    // field to store the hotspot diagnostic MBean
    private static volatile HotSpotDiagnosticMXBean hotspotMBean;
    
    public static void generateHeapDump(String fileName, boolean live) {
        // initialize hotspot diagnostic MBean
        initHotspotMBean();
        try {
            File dumpFile = new File(fileName);
            if (dumpFile.exists()) {
                dumpFile.delete();
            }
            hotspotMBean.dumpHeap(fileName, live);
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception exp) {
            throw new RuntimeException(exp);
        }
    }
    
    // initialize the hotspot diagnostic MBean field
    private static void initHotspotMBean() {
        if (hotspotMBean == null) {
            synchronized (HeapDumpGenerator.class) {
                if (hotspotMBean == null) {
                    hotspotMBean = getHotspotMBean();
                }
            }
        }
    }
    
    // get the hotspot diagnostic MBean from the
    // platform MBean server
    private static HotSpotDiagnosticMXBean getHotspotMBean() {
        try {
            MBeanServer server = ManagementFactory.getPlatformMBeanServer();
            HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
            return bean;
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception exp) {
            throw new RuntimeException(exp);
        }
    }
    
    public static String generateHeapDump() {
        String fileName = getFullHeapDumpFileName();
        generateHeapDump(fileName, true);
        return fileName;
    }
    
    }