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:com.taobao.adfs.util.Utilities.java

public static String getPidString() {
    if (pidString != null)
        return pidString;
    String name = ManagementFactory.getRuntimeMXBean().getName();
    return pidString = name.substring(0, name.indexOf('@'));
}

From source file:org.tinymediamanager.core.Utils.java

/**
 * gets all the JVM parameters used for starting TMM<br>
 * like -Dfile.encoding=UTF8 or others<br>
 * needed for restarting tmm :)// ww  w. j  av  a2  s.c  o  m
 * 
 * @return list of jvm parameters
 */
private static List<String> getJVMArguments() {
    RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
    List<String> arguments = new ArrayList<>(runtimeMxBean.getInputArguments());
    // fixtate some
    if (!arguments.contains("-Djava.net.preferIPv4Stack=true")) {
        arguments.add("-Djava.net.preferIPv4Stack=true");
    }
    if (!arguments.contains("-Dfile.encoding=UTF-8")) {
        arguments.add("-Dfile.encoding=UTF-8");
    }
    return arguments;
}

From source file:com.sos.VirtualFileSystem.DataElements.SOSFileListEntry.java

/**
 *
 * \brief sendTransferHistory/* w w w  . j av  a2  s  .  com*/
 *
 * \details
 * This methods sends for each file the needed informations about the transfer history
 * to the background service (JobScheduler).
 *
 * \return void
 *
 */
public boolean sendTransferHistory() {
    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::sendTransferHistory";
    boolean flgRet = false;

    if (flgTransferHistoryAlreadySent == true) {
        return flgRet;
    }
    flgTransferHistoryAlreadySent = true;

    if (objOptions.SendTransferHistory.isFalse()) {
        if (flgNoDataSent == false) {
            flgNoDataSent = true;
        }
        return flgRet;
    }

    String strBackgroundServiceHostName = objOptions.scheduler_host.Value();
    if (isEmpty(strBackgroundServiceHostName)) {
        if (flgNoDataSent == false) {
            flgNoDataSent = true;
            logger.debug(Messages.getMsg("No data sent to the background service due to missing host name"));
        }
        return flgRet;
    }

    String mandator = objOptions.mandator.Value(); // 0-
    String transfer_timestamp = EMPTY_STRING;
    try {
        transfer_timestamp = sos.util.SOSDate.getCurrentTimeAsString();
    } catch (Exception e) {
    } // 1- timestamp: Zeitstempel im ISO-Format
    /**
     * this hack is tested for SUN-JVM only. No guarantee is made for other JVMs
     */
    // TODO create a getPID method in shell class
    String pid = ManagementFactory.getRuntimeMXBean().getName();
    String strA[] = pid.split("@");
    pid = strA[0];
    String ppid = System.getProperty("ppid", "0");
    String operation = objOptions.operation.Value(); // 4- operation: send|receive
    SOSConnection2OptionsAlternate objS = objOptions.getConnectionOptions().Source();
    String localhost = objS.host.Value(); // 5- local host
    String localhost_ip = objS.host.getHostAdress(); // 5-1- local host IP adresse
    String local_user = System.getProperty("user.name"); // 6- local user
    SOSConnection2OptionsAlternate objT = objOptions.getConnectionOptions().Target();
    String remote_host = objT.host.Value(); // 7- remote host
    String remote_host_ip = objT.host.getHostAdress(); // 7- remote host IP

    String remote_user = "";
    if (objT.AlternateOptionsUsed.value() == true) {
        remote_user = "(alternative) " + objT.Alternatives().user.Value(); // 8- remote host user
    } else {
        remote_user = objT.user.Value(); // 8- remote host user
    }
    String protocol = objT.protocol.Value(); // 9- protocol
    String port = objT.port.Value(); // 10- port
    String local_dir = objOptions.local_dir.Value();
    // String remote_dir = new File(this.strTargetFileName).getAbsolutePath();
    String remote_dir = objOptions.TargetDir.Value();
    String local_filename = strSourceFileName; // 13- file name
    String remote_filename = strTargetFileName; // 14- name
    if (isEmpty(remote_filename)) {
        remote_filename = "n.a.";
    }
    String fileSize = String.valueOf(lngFileSize);
    String md5 = strMD5Hash;
    String last_error_message = "";
    // last_error_message = clearCRLF(((getLogger().getError() != null && getLogger().getError().length() > 0) ? getLogger().getError()
    // : getLogger().getWarning())); // 15- last_error=|warn message
    // last_error_message = normalizedPassword(sosString.parseToString(last_error_message));
    String log_filename = objOptions.log_filename.Value();
    String jump_host = objOptions.jump_host.Value();
    String jump_host_ip = objOptions.jump_host.getHostAdress();
    String jump_port = objOptions.jump_port.Value();
    String jump_protocol = objOptions.jump_protocol.Value();
    String jump_user = objOptions.jump_user.Value();

    Properties objSchedulerOrderParameterSet = new Properties();
    objSchedulerOrderParameterSet.put("guid", guid); // 1- GUID
    objSchedulerOrderParameterSet.put("mandator", mandator); // 2- mandator: default SOS
    objSchedulerOrderParameterSet.put("transfer_timestamp", transfer_timestamp); // 3- timestamp: Zeitstempel im ISO-Format
    objSchedulerOrderParameterSet.put("pid", pid); // 4- pid= Environment PID | 0 fr Windows
    objSchedulerOrderParameterSet.put("ppid", ppid); // 5- ppid= Environment PPID | 0 fr Windows
    objSchedulerOrderParameterSet.put("operation", operation); // 6- operation: send|receive
    objSchedulerOrderParameterSet.put("localhost", localhost); // 7- local host
    objSchedulerOrderParameterSet.put("localhost_ip", localhost_ip); // 8- local host IP adresse
    objSchedulerOrderParameterSet.put("local_user", local_user); // 9- local user
    objSchedulerOrderParameterSet.put("remote_host", remote_host); // 10- remote host
    objSchedulerOrderParameterSet.put("remote_host_ip", remote_host_ip); // 11- remote host IP
    objSchedulerOrderParameterSet.put("remote_user", remote_user); // 12- remote host user
    objSchedulerOrderParameterSet.put("protocol", protocol); // 13- protocol
    objSchedulerOrderParameterSet.put("port", port); // 14- port
    objSchedulerOrderParameterSet.put("local_dir", local_dir); // 15- local dir
    objSchedulerOrderParameterSet.put("remote_dir", remote_dir); // 16- remote dir
    objSchedulerOrderParameterSet.put("local_filename", local_filename); // 17- file name
    objSchedulerOrderParameterSet.put("remote_filename", remote_filename); // 18- file name
    objSchedulerOrderParameterSet.put("file_size", fileSize); // 19 - file name
    objSchedulerOrderParameterSet.put("md5", md5); // 20

    String status = eTransferStatus.name();
    if (status.equalsIgnoreCase("transferred")) {
        status = "success";
    }
    objSchedulerOrderParameterSet.put("status", status); // 21- status=success|error
    objSchedulerOrderParameterSet.put("last_error_message", last_error_message); // 22
    objSchedulerOrderParameterSet.put("log_filename", log_filename); // 23
    objSchedulerOrderParameterSet.put("jump_host", jump_host); // 24
    objSchedulerOrderParameterSet.put("jump_host_ip", jump_host_ip); // 25
    objSchedulerOrderParameterSet.put("jump_port", jump_port); // 26
    objSchedulerOrderParameterSet.put("jump_protocol", jump_protocol); // 27
    objSchedulerOrderParameterSet.put("jump_user", jump_user); // 28
    // TODO custom-fields einbauen
    /**
     * bei SOSFTP ist es mglich "custom" Felder zu definieren, die in der Transfer History als Auftragsparameter mitgeschickt werden.
     * Damit man diese Felder identifizieren kann, werden hier Parameter defininiert, die beim Auftrag dabei sind, aber keine
     * "custom" Felder sind
     *
     * ? alternativ Metadaten der Tabelle lesen (Spalten) und mit den Auftragsparameter vergleichen
     */

    SchedulerObjectFactory objFactory = objParent.objFactory;
    if (objFactory == null) {
        objFactory = new SchedulerObjectFactory(objOptions.scheduler_host.Value(),
                objOptions.scheduler_port.value());
        objFactory.initMarshaller(Spooler.class);
        objParent.objFactory = objFactory;
        objFactory.Options().TransferMethod.Value(objOptions.Scheduler_Transfer_Method.Value());
        objFactory.Options().PortNumber.Value(objOptions.scheduler_port.Value());
        objFactory.Options().ServerName.Value(objOptions.scheduler_host.Value());
    }
    JSCmdAddOrder objOrder = objFactory.createAddOrder();
    objOrder.setJobChain(objOptions.scheduler_job_chain.Value() /* "scheduler_sosftp_history" */);
    objOrder.setTitle(SOSVfs_Title_276.get());
    Params objParams = objFactory.setParams(objSchedulerOrderParameterSet);
    objOrder.setParams(objParams);
    //      logger.debug(objOrder.toXMLString());
    objOrder.run();

    flgRet = true;
    return flgRet;
}

From source file:com.evolveum.midpoint.gui.api.util.WebComponentUtil.java

public static double getSystemLoad() {
    com.sun.management.OperatingSystemMXBean operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory
            .getOperatingSystemMXBean();
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    int availableProcessors = operatingSystemMXBean.getAvailableProcessors();
    long prevUpTime = runtimeMXBean.getUptime();
    long prevProcessCpuTime = operatingSystemMXBean.getProcessCpuTime();

    try {// w w w  .jav a2  s  .  co  m
        Thread.sleep(150);
    } catch (Exception ignored) {
        // ignored
    }

    operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory
            .getOperatingSystemMXBean();
    long upTime = runtimeMXBean.getUptime();
    long processCpuTime = operatingSystemMXBean.getProcessCpuTime();
    long elapsedCpu = processCpuTime - prevProcessCpuTime;
    long elapsedTime = upTime - prevUpTime;

    double cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 10000F * availableProcessors));

    return cpuUsage;
}

From source file:org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.java

private static String computeUniqueInstanceId(Configuration config) {
    final String suffix;

    if (config.has(GraphDatabaseConfiguration.UNIQUE_INSTANCE_ID_SUFFIX)) {
        suffix = LongEncoding.encode(config.get(GraphDatabaseConfiguration.UNIQUE_INSTANCE_ID_SUFFIX));
    } else {/* www . j  av a  2s .  co  m*/
        suffix = ManagementFactory.getRuntimeMXBean().getName()
                + LongEncoding.encode(INSTANCE_COUNTER.incrementAndGet());
    }

    byte[] addrBytes;
    try {
        addrBytes = Inet4Address.getLocalHost().getAddress();
    } catch (UnknownHostException e) {
        throw new JanusGraphConfigurationException("Cannot determine local host", e);
    }
    String uid = new String(Hex.encodeHex(addrBytes)) + suffix;
    for (char c : ConfigElement.ILLEGAL_CHARS) {
        uid = StringUtils.replaceChars(uid, c, '-');
    }
    return uid;
}

From source file:com.sos.JSHelper.Options.JSOptionsClass.java

public String getPid() {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::getPid";

    String pid = ManagementFactory.getRuntimeMXBean().getName();
    String strA[] = pid.split("@");
    pid = strA[0];//from ww w .j av  a  2s. c  o m

    return pid;
}

From source file:com.datatorrent.stram.cli.DTCli.java

private File copyToLocal(String[] files) throws IOException {
    File tmpDir = new File(System.getProperty("java.io.tmpdir") + "/datatorrent/"
            + ManagementFactory.getRuntimeMXBean().getName());
    tmpDir.mkdirs();/*w  ww. j  a  va2 s  . com*/
    for (int i = 0; i < files.length; i++) {
        try {
            URI uri = new URI(files[i]);
            String scheme = uri.getScheme();
            if (scheme == null || scheme.equals("file")) {
                files[i] = uri.getPath();
            } else {
                FileSystem tmpFs = FileSystem.newInstance(uri, conf);
                try {
                    Path srcPath = new Path(uri.getPath());
                    Path dstPath = new Path(tmpDir.getAbsolutePath(), String.valueOf(i) + srcPath.getName());
                    tmpFs.copyToLocalFile(srcPath, dstPath);
                    files[i] = dstPath.toUri().getPath();
                } finally {
                    tmpFs.close();
                }
            }
        } catch (URISyntaxException ex) {
            throw new RuntimeException(ex);
        }
    }

    return tmpDir;
}

From source file:com.datatorrent.stram.cli.ApexCli.java

private File copyToLocal(String[] files) throws IOException {
    File tmpDir = new File(System.getProperty("java.io.tmpdir") + "/datatorrent/"
            + ManagementFactory.getRuntimeMXBean().getName());
    tmpDir.mkdirs();/*ww  w. j  a  v a2 s .c  o m*/
    for (int i = 0; i < files.length; i++) {
        try {
            URI uri = new URI(files[i]);
            String scheme = uri.getScheme();
            if (scheme == null || scheme.equals("file")) {
                files[i] = uri.getPath();
            } else {
                try (FileSystem tmpFs = FileSystem.newInstance(uri, conf)) {
                    Path srcPath = new Path(uri.getPath());
                    Path dstPath = new Path(tmpDir.getAbsolutePath(), String.valueOf(i) + srcPath.getName());
                    tmpFs.copyToLocalFile(srcPath, dstPath);
                    files[i] = dstPath.toUri().getPath();
                }
            }
        } catch (URISyntaxException ex) {
            throw new RuntimeException(ex);
        }
    }

    return tmpDir;
}