public void onRegister()

in services/minho-jmx/src/main/java/org/apache/karaf/minho/jmx/JmxService.java [44:112]


    public void onRegister(ServiceRegistry serviceRegistry) throws Exception {
        ConfigService configService = serviceRegistry.get(ConfigService.class);
        if (configService == null) {
            throw new IllegalStateException("ConfigService is not found");
        }
        LifeCycleService lifeCycleService = serviceRegistry.get(LifeCycleService.class);
        if (lifeCycleService == null) {
            throw new IllegalStateException("LifeCycleService is not found");
        }
        String rmiRegistryHost = configService.getProperty("jmx.rmiRegistryHost", "");
        int rmiRegistryPort = Integer.parseInt(configService.getProperty("jmx.rmiRegistryPort", "1099"));
        String rmiServerHost = configService.getProperty("jmx.rmiServerHost", "0.0.0.0");
        int rmiServerPort = Integer.parseInt(configService.getProperty("jmx.rmiServerPort", "44444"));

        String serviceUrl = configService.getProperty("jmx.serverUrl", "service:jmx:rmi://" + rmiServerHost + ":" + rmiServerPort + "/jndi/rmi://" + rmiRegistryHost + ":" + rmiRegistryPort + "/minho");

        boolean daemon = Boolean.parseBoolean(configService.getProperty("jmx.daemon", "true"));
        boolean threaded = Boolean.parseBoolean(configService.getProperty("jmx.threaded", "true"));
        ObjectName objectName = new ObjectName(configService.getProperty("jmx.objectName", "connector:name=rmi"));
        boolean createRmiRegistry = Boolean.parseBoolean(configService.getProperty("jmx.createRmiRegistry", "true"));
        boolean locateRmiRegistry = Boolean.parseBoolean(configService.getProperty("jmx.locateRmiRegistry", "true"));
        boolean locateExistingMBeanServerIfPossible = Boolean.parseBoolean(configService.getProperty("jmx.locateExistingMBeanServerIfPossible", "true"));

        final MBeanServerFactory mBeanServerFactory = new MBeanServerFactory();
        mBeanServerFactory.setLocateExistingServerIfPossible(locateExistingMBeanServerIfPossible);
        mBeanServerFactory.init();

        mBeanServer = mBeanServerFactory.getServer();

        final ConnectorServerFactory connectorServerFactory = new ConnectorServerFactory();
        connectorServerFactory.setCreate(createRmiRegistry);
        connectorServerFactory.setLocate(locateRmiRegistry);
        connectorServerFactory.setHost(rmiRegistryHost);
        connectorServerFactory.setPort(rmiRegistryPort);
        connectorServerFactory.setServer(mBeanServer);
        connectorServerFactory.setServiceUrl(serviceUrl);
        connectorServerFactory.setRmiServerHost(rmiServerHost);
        connectorServerFactory.setDaemon(daemon);
        connectorServerFactory.setThreaded(threaded);
        connectorServerFactory.setObjectName(objectName);
        Map<String, Object> environment = new HashMap<>();
        connectorServerFactory.setEnvironment(environment);

        lifeCycleService.onStart(() -> {
            try {
                connectorServerFactory.init();
            } catch (Throwable e) {
                log.severe("Can't init JMXConnectorServer: " + e.getMessage());
            }
        });

        lifeCycleService.onShutdown(() -> {
            if (connectorServerFactory != null) {
                try {
                    connectorServerFactory.destroy();
                } catch (Exception e) {
                    log.warning("Error destroying ConnectorServerFactory: " + e.getMessage());
                }
            }
            if (mBeanServerFactory != null) {
                try {
                    mBeanServerFactory.destroy();
                } catch (Exception e) {
                    log.warning("Error destroying MBeanServerFactory: " + e.getMessage());
                }
            }

        });
    }