All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.apexes.commons.lang.ext.JseProcessIdentifier Maven / Gradle / Ivy

There is a newer version: 2.1.6
Show newest version
/*
 * Copyright (c) 2018, apexes.net. All rights reserved.
 *
 *          http://www.apexes.net
 */
package net.apexes.commons.lang.ext;

import net.apexes.commons.lang.ProcessIdentifier;

import java.security.SecureRandom;

/**
 * @author HeDYn
 */
public class JseProcessIdentifier implements ProcessIdentifier {

    private final short processId;

    public JseProcessIdentifier() {
        this.processId = myPid();
    }

    @Override
    public short processId() {
        return processId;
    }

    public static short myPid() {
        short processId;
        try {
            String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
            if (processName.contains("@")) {
                processId = (short) Integer.parseInt(processName.substring(0, processName.indexOf('@')));
            } else {
                processId = (short) java.lang.management.ManagementFactory.getRuntimeMXBean().getName().hashCode();
            }
        } catch (Throwable t) {
            processId = (short) new SecureRandom().nextInt();
        }
        return processId;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy