Example usage for java.lang.management ManagementFactory getRuntimeMXBean

List of usage examples for java.lang.management ManagementFactory getRuntimeMXBean

Introduction

In this page you can find the example usage for java.lang.management ManagementFactory getRuntimeMXBean.

Prototype

public static RuntimeMXBean getRuntimeMXBean() 

Source Link

Document

Returns the managed bean for the runtime system of the Java virtual machine.

Usage

From source file:de.avanux.smartapplianceenabler.Application.java

private void writePidFile() {
    String pidFileName = System.getProperty("sae.pidfile");
    if (pidFileName != null) {
        String name = ManagementFactory.getRuntimeMXBean().getName();
        String pid = name.split("@")[0];
        try {/*from  w ww. j  a va2s .c  o m*/
            Writer pidFile = new FileWriter(pidFileName);
            pidFile.write(pid);
            pidFile.close();
            logger.info("PID " + pid + " written to " + pidFileName);
        } catch (IOException e) {
            logger.error("Error writing PID file " + pidFileName, e);
        }
    }
}

From source file:org.apache.hadoop.metrics.APITrace.java

private static String getPid() {
    // Generally .getName() will return "UNIX-PID@MACHINE-NAME", but this is JVM specific.  If the
    // string follows this format, we just return the UNIX-PID.  Otherwise, we return the entire ID.
    String name = ManagementFactory.getRuntimeMXBean().getName();
    String parts[] = name.split("@");
    if (parts.length == 2) {
        return parts[0];
    }//from   ww  w  . j  a v  a 2s .com
    return name;
}

From source file:com.aionemu.commons.log4j.appenders.TruncateToZipFileAppender.java

/**
 * This method creates archive with file instead of deleting it.
 *
 * @param file file to truncate/*from   www .j a  v a 2  s  .  c  o  m*/
 */
protected void truncate(File file) {
    LogLog.debug("Compression of file: " + file.getAbsolutePath() + " started.");

    // Linux systems doesn't provide file creation time, so we have to hope
    // that log files
    // were not modified manually after server starup
    // We can use here Windowns-only solution but that suck :(
    if (FileUtils.isFileOlder(file, ManagementFactory.getRuntimeMXBean().getStartTime())) {
        File backupRoot = new File(getBackupDir());
        if (!backupRoot.exists() && !backupRoot.mkdirs()) {
            throw new AppenderInitializationError("Can't create backup dir for backup storage");
        }

        SimpleDateFormat df;
        try {
            df = new SimpleDateFormat(getBackupDateFormat());
        } catch (Exception e) {
            throw new AppenderInitializationError(
                    "Invalid date formate for backup files: " + getBackupDateFormat(), e);
        }
        String date = df.format(new Date(file.lastModified()));

        File zipFile = new File(backupRoot, file.getName() + "." + date + ".zip");

        ZipOutputStream zos = null;
        FileInputStream fis = null;
        try {
            zos = new ZipOutputStream(new FileOutputStream(zipFile));
            ZipEntry entry = new ZipEntry(file.getName());
            entry.setMethod(ZipEntry.DEFLATED);
            entry.setCrc(FileUtils.checksumCRC32(file));
            zos.putNextEntry(entry);
            fis = FileUtils.openInputStream(file);

            byte[] buffer = new byte[1024];
            int readed;
            while ((readed = fis.read(buffer)) != -1) {
                zos.write(buffer, 0, readed);
            }

        } catch (Exception e) {
            throw new AppenderInitializationError("Can't create zip file", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    // not critical error
                    LogLog.warn("Can't close zip file", e);
                }
            }

            if (fis != null) {
                try {
                    // not critical error
                    fis.close();
                } catch (IOException e) {
                    LogLog.warn("Can't close zipped file", e);
                }
            }
        }

        if (!file.delete()) {
            throw new AppenderInitializationError("Can't delete old log file " + file.getAbsolutePath());
        }
    }
}

From source file:com.l2jserver.service.core.logging.TruncateToZipFileAppender.java

/**
 * This method creates archive with file instead of deleting it.
 * /*from www .  j a  va  2  s.  c  om*/
 * @param file
 *            file to truncate
 */
protected void truncate(File file) {
    LogLog.debug("Compression of file: " + file.getAbsolutePath() + " started.");

    // Linux systems doesn't provide file creation time, so we have to hope
    // that log files
    // were not modified manually after server starup
    // We can use here Windowns-only solution but that suck :(
    if (FileUtils.isFileOlder(file, ManagementFactory.getRuntimeMXBean().getStartTime())) {
        File backupRoot = new File(getBackupDir());
        if (!backupRoot.exists() && !backupRoot.mkdirs()) {
            throw new Error("Can't create backup dir for backup storage");
        }

        SimpleDateFormat df;
        try {
            df = new SimpleDateFormat(getBackupDateFormat());
        } catch (Exception e) {
            throw new Error("Invalid date formate for backup files: " + getBackupDateFormat(), e);
        }
        String date = df.format(new Date(file.lastModified()));

        File zipFile = new File(backupRoot, file.getName() + "." + date + ".zip");

        ZipOutputStream zos = null;
        FileInputStream fis = null;
        try {
            zos = new ZipOutputStream(new FileOutputStream(zipFile));
            ZipEntry entry = new ZipEntry(file.getName());
            entry.setMethod(ZipEntry.DEFLATED);
            entry.setCrc(FileUtils.checksumCRC32(file));
            zos.putNextEntry(entry);
            fis = FileUtils.openInputStream(file);

            byte[] buffer = new byte[1024];
            int readed;
            while ((readed = fis.read(buffer)) != -1) {
                zos.write(buffer, 0, readed);
            }

        } catch (Exception e) {
            throw new Error("Can't create zip file", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    // not critical error
                    LogLog.warn("Can't close zip file", e);
                }
            }

            if (fis != null) {
                try {
                    // not critical error
                    fis.close();
                } catch (IOException e) {
                    LogLog.warn("Can't close zipped file", e);
                }
            }
        }

        if (!file.delete()) {
            throw new Error("Can't delete old log file " + file.getAbsolutePath());
        }
    }
}

From source file:com.amazonaws.services.simpleworkflow.flow.worker.DecisionTaskPoller.java

public DecisionTaskPoller(AmazonSimpleWorkflow service, String domain, String taskListToPoll,
        DecisionTaskHandler decisionTaskHandler) {
    this.service = service;
    this.domain = domain;
    this.taskListToPoll = taskListToPoll;
    this.decisionTaskHandler = decisionTaskHandler;
    identity = ManagementFactory.getRuntimeMXBean().getName();
}

From source file:org.apache.servicecomb.foundation.vertx.VertxUtils.java

public static Vertx init(String name, VertxOptions vertxOptions) {
    if (vertxOptions == null) {
        vertxOptions = new VertxOptions();
    }//from  ww  w  .  ja v  a 2 s  .  c o m

    boolean isDebug = ManagementFactory.getRuntimeMXBean().getInputArguments().toString().contains("jdwp");
    if (isDebug) {
        vertxOptions.setBlockedThreadCheckInterval(BLOCKED_THREAD_CHECK_INTERVAL);
        LOGGER.info("in debug mode, disable blocked thread check.");
    }

    configureVertxFileCaching();
    return new VertxImplEx(name, vertxOptions);
}

From source file:io.servicecomb.foundation.vertx.VertxUtils.java

public static Vertx init(String name, VertxOptions vertxOptions) {
    if (vertxOptions == null) {
        vertxOptions = new VertxOptions();
    }//from w ww .j a v  a 2 s .  c o m

    boolean isDebug = ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("jdwp") >= 0;
    if (isDebug) {
        vertxOptions.setBlockedThreadCheckInterval(BLOCKED_THREAD_CHECK_INTERVAL);
        LOGGER.info("in debug mode, disable blocked thread check.");
    }

    configureVertxFileCaching();
    return new VertxImplEx(name, vertxOptions);
}

From source file:org.kiji.bento.tools.MiniClusterTool.java

/**
 * Gets the pid for this process. This won't be portable on non-UNIX systems.
 *
 * @return The pid of this JVM./* www.  j  a v  a 2s.  c  o  m*/
 */
private int getPid() {
    String processString = ManagementFactory.getRuntimeMXBean().getName();
    return Integer.valueOf(processString.split("@")[0]);
}

From source file:net.voidfunction.rm.master.AdminServlet.java

private String pageIndex(HttpServletRequest request) throws FileNotFoundException {
    Template tpl = new Template(new File(templateDir + "index.tpl"));
    tpl.assign("HOST", node.getPublicIP());

    // Status info
    tpl.assign("UPTIME", getDuration(ManagementFactory.getRuntimeMXBean().getUptime()));
    tpl.assign("WORKERCOUNT", String.valueOf(node.getWorkerDirectory().getWorkerCount()));
    tpl.assign("FILECOUNT", String.valueOf(node.getFileRepository().getFileCount()));

    // Files//from ww  w. j  a  va2 s  . co m
    Collection<RMFile> files = node.getFileRepository().getFileObjects();
    if (files.size() == 0) {
        tpl.assign("FILEID", "No files.");
        tpl.assign("FILENAME", "");
        tpl.assign("FILETYPE", "");
        tpl.assign("FILESIZE", "");
        tpl.assign("DOWNURL", "");
        tpl.assign("DOWNTXT", "");
        tpl.assign("DELETEURL", "");
        tpl.assign("DELETETXT", "");
        tpl.parse("main.file");
    }
    for (RMFile file : files) {
        tpl.assign("FILEID", file.getId());
        tpl.assign("FILENAME", file.getName());
        tpl.assign("FILETYPE", file.getMimetype());
        tpl.assign("FILESIZE", String.valueOf(file.getSize()));
        tpl.assign("DOWNURL", "http://" + request.getLocalAddr() + ":"
                + node.getConfig().getInt("port.http", 8080) + "/files/" + file.getId() + "/" + file.getName());
        tpl.assign("DOWNTXT", "Download");
        tpl.assign("DELETEURL", "?page=delete&id=" + file.getId());
        tpl.assign("DELETETXT", "Delete");
        tpl.parse("main.file");
    }

    tpl.parse("main");
    return tpl.out();
}

From source file:de.bayern.gdi.App.java

/**
 * As of Java 9 the method <code>ProcessHandle.current().pid()</code>
 * can be used.//from   w w  w  . ja va  2s  .  co m
 * This is a workaround for Java 8 which should work for Oracle and
 * OpenJDK.
 *
 * @param fallback the given id returned in case PID can not determined
 * @return the PID (process identifier) for the JVM running this class
 */
private static String getProcessId(final String fallback) {
    // Note: may fail in some JVM implementations
    // therefore fallback has to be provided

    // something like '<pid>@<hostname>', at least in SUN / Oracle JVMs
    final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
    final int index = jvmName.indexOf('@');

    if (index < 1) {
        // part before '@' empty (index = 0) / '@' not found (index = -1)
        return fallback;
    }

    try {
        return Long.toString(Long.parseLong(jvmName.substring(0, index)));
    } catch (NumberFormatException e) {
        // ignore
    }
    return fallback;
}