hello,

I got the proposed POC working for
https://issues.apache.org/jira/browse/MARMOTTA-408. It seems the jira is
offline for maintenance so I will add the information here. I will copy
 the same information into the jira when it's online.

Please find the attached patch and the screenshot.

The screenshot shows how I exposed the OS and JDK run-time
information(similar to System->Statistics->System ) using a MBean from the
newly developed JMX infrastructure.


The classes;
a). org.apache.marmotta.platform.core.api.jmx.JMXService
b). org.apache.marmotta.platform.core.services.jmx.JMXServiceImpl
c). org.apache.marmotta.platform.core.services.jmx.JMXRMIRegistryController
d). org.apache.marmotta.platform.core.services.jmx.JMXMBeansRegister
e). org.apache.marmotta.platform.core.services.jmx.JMXAdapter

are responsible for #1 in the jira. The mentioned MBeanRegister API is
defined in JMXMBeansRegister (d)). It has the APIs to register and
unregister the MBeans. The other classes basically create a JMX connector
and starts it with the platform. I consumed the SystemStartupEvent to start
the JMX CDI service.


The interface and class;
a). org.apache.marmotta.platform.core.services.jmx.SystemStatisticsMBean
b). org.apache.marmotta.platform.core.services.jmx.SystemStatistics

are an example for #2. They collects the stats about the OS/JDK run time
data and register them into  #1 using MBeans so that we can view that
statistics using JConsole(or any other JMX client). This is what can be
seen in the screen shot. I just implemented this logic inside
org.apache.marmotta.platform.core.services.jmx.JMXServiceImpl itself but it
should be implemented in each place where those stats will be collected
(for example
org.apache.marmotta.platform.core.services.statistics.system.SystemStatisticsModule).

Now the next step is to identify various places, components or modules
across the platform and other places and replace them with relevant MBeans
and register them with the MBean server similar to #2 above. #1 also need
to improve so that other jmx parameters can be configured(for example SSL
connections).

Do you think I am heading in the correct direction ? I would like to start
writing my proposal based on this information once I compiled a list of
places to have new MBeans to collect respective statistics. I am aware of
JUnit and each of this new feature will be covered with a JUnit test case.
Please let me know your feedback or any comment.

Sorry for the long email. For sure if you prefer we can continue the
discussion in the jira as well.

Thank you.




On Sun, Mar 2, 2014 at 10:17 AM, Rajika Kumarasiri <
[email protected]> wrote:

> hello,
>
> Thank you for your replies. I got this to work. I am reading about the
> Weld framework as well.
>
> Thank you.
>
>
> On Sat, Mar 1, 2014 at 3:37 AM, Jakob Frank <[email protected]> wrote:
>
>> Hi Rajika,
>>
>> if you want your CDI service to startup automatically (e.g. because it
>> needs to register something somewhere) you should listen on the
>> SystemStartupEvent which is fired once the system has come up
>> completely:
>>
>> // trigger startup once configuration service is finished with
>> initialisation
>> public void initEvent(@Observes SystemStartupEvent e) {
>>     // nop;
>> }
>>
>> Best,
>> Jakob
>>
>> On 28 February 2014 12:28, Rajika Kumarasiri
>> <[email protected]> wrote:
>> > helo Sergio,
>> >
>> > Thanks, will try that. Sure will provide a documentation enhancement
>> patch
>> > too.
>> >
>> > Thank you.
>> >
>> >
>> > On Fri, Feb 28, 2014 at 2:38 AM, Sergio Fernández <
>> > [email protected]> wrote:
>> >
>> >> Hi Rajika,
>> >>
>> >>
>> >> On 28/02/14 06:23, Rajika Kumarasiri wrote:
>> >>
>> >>> I have created a new service for JMX(api and implementation) and now I
>> >>> want
>> >>> to start it with the server start up. Where should I register the
>> service
>> >>> so that JMX service will start at the server boot up time ?
>> >>>
>> >>
>> >> The services are in the end CDI services (JSR346), using the JBoss Weld
>> >> implementation.
>> >>
>> >> So you just need to add to your service class the annotation with the
>> >> necessary scope, in this case @ApplicationScope. Then the platform will
>> >> instantiate it, and you can normally use IoC with your new service in
>> the
>> >> rest of the platform.
>> >>
>> >> Further details at:
>> >>
>> >> http://marmotta.apache.org/platform
>> >> http://docs.jboss.org/weld/reference/2.1.0.Final/en-US/html/
>> >>
>> >> If you miss anything else in the documentation, contributions there are
>> >> also welcomed ;-)
>> >>
>> >> Cheers,
>> >>
>> >> --
>> >> Sergio Fernández
>> >> Senior Researcher
>> >> Knowledge and Media Technologies
>> >> Salzburg Research Forschungsgesellschaft mbH
>> >> Jakob-Haringer-Straße 5/3 | 5020 Salzburg, Austria
>> >> T: +43 662 2288 318 | M: +43 660 2747 925
>> >> [email protected]
>> >> http://www.salzburgresearch.at
>> >>
>>
>
>
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/jmx/JMXService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/jmx/JMXService.java
index 7a0de32..ff2b35f 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/jmx/JMXService.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/jmx/JMXService.java
@@ -4,4 +4,6 @@ package org.apache.marmotta.platform.core.api.jmx;
  * author : [email protected]
  */
 public interface JMXService {
+
+    public void start(int jndi, int rmi, String host);
 }
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXAdapter.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXAdapter.java
index f2e9607..26a65a8 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXAdapter.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXAdapter.java
@@ -69,7 +69,7 @@ public class JMXAdapter {
             }
             jmxURL += "/jndi/rmi://" + hostName + ":" + jndiPort + "/marmotta";
 
-            RMIRegistryController.getInstance().createLocalRegistry(jndiPort);
+            JMXRMIRegistryController.getInstance().createLocalRegistry(jndiPort);
 
             JMXServiceURL url = new JMXServiceURL(jmxURL);
 
@@ -82,7 +82,7 @@ public class JMXAdapter {
             } catch (IOException ex) {
                 log.warn("Cannot start JMXConnectorServer on " + jmxURL, ex);
             }
-            log.info("JMXConnector service started '" + jmxURL + "'.");
+            log.info("Management using JMX available from '" + jmxURL + "'.");
 
         } catch (Exception e) {
             log.error("Could not start the JMX connection", e);
@@ -93,7 +93,7 @@ public class JMXAdapter {
         if (connectorServer != null) {
             try {
                 connectorServer.stop();
-                RMIRegistryController.getInstance().removeLocalRegistry(jndiPort);
+                JMXRMIRegistryController.getInstance().removeLocalRegistry(jndiPort);
             } catch (IOException e) {
                 log.error("Error while stopping JMX connector server", e);
             }
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXMBeansRegister.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXMBeansRegister.java
index 4ad6eb8..2d5d028 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXMBeansRegister.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXMBeansRegister.java
@@ -26,8 +26,72 @@
  */
 package org.apache.marmotta.platform.core.services.jmx;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import java.lang.management.ManagementFactory;
+import java.util.Set;
+
 /**
  * author : [email protected]
  */
 public class JMXMBeansRegister {
+
+    private static final Log log = LogFactory.getLog(JMXMBeansRegister.class);
+
+    private static final JMXMBeansRegister instance = new JMXMBeansRegister();
+
+
+    public static JMXMBeansRegister getInstance() {
+        return instance;
+    }
+
+    private JMXMBeansRegister() {
+    }
+
+    public boolean registerMBean(Object mBeanInstance, String category, String id) {
+        try {
+            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+            ObjectName name = new ObjectName(getObjectName(category, id));
+            Set set = mbs.queryNames(name, null);
+            if (set != null && set.isEmpty()) {
+                mbs.registerMBean(mBeanInstance, name);
+            } else {
+                mbs.unregisterMBean(name);
+                mbs.registerMBean(mBeanInstance, name);
+            }
+            return true;
+        } catch (Exception e) {
+            log.warn("Error registering a MBean with name ' " + id +
+                    " ' and category name ' " + category + "' for JMX management", e);
+            return false;
+        }
+    }
+
+    public boolean unRegisterMBean(String category, String id) {
+        try {
+            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+            ObjectName objName = new ObjectName(getObjectName(category, id));
+            if (mbs.isRegistered(objName)) {
+                mbs.unregisterMBean(objName);
+                return true;
+            }
+        } catch (Exception e) {
+            log.warn("Error un-registering a  MBean with name ' " + id +
+                    " ' and category name ' " + category + "' for JMX management", e);
+        }
+        return false;
+    }
+
+    private String getObjectName(String category, String id) {
+
+        String jmxAgentName = System.getProperty("jmx.agent.name");
+        if (jmxAgentName == null || "".equals(jmxAgentName)) {
+            jmxAgentName = "org.apache.marmotta";
+        }
+        return jmxAgentName + ":Type=" + category + ",Name=" + id;
+    }
+
 }
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXServiceImpl.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXServiceImpl.java
index 47d02d9..f499d0d 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXServiceImpl.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/JMXServiceImpl.java
@@ -26,8 +26,56 @@
  */
 package org.apache.marmotta.platform.core.services.jmx;
 
+import org.apache.marmotta.platform.core.api.config.ConfigurationService;
+import org.apache.marmotta.platform.core.api.jmx.JMXService;
+import org.apache.marmotta.platform.core.events.SystemStartupEvent;
+import org.slf4j.Logger;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.event.Observes;
+import javax.inject.Inject;
+
+
 /**
  * author : [email protected]
  */
-public class JMXServiceImpl {
+
+@ApplicationScoped
+public class JMXServiceImpl implements JMXService {
+
+    @Inject
+    private Logger log;
+
+    @Inject
+    private ConfigurationService configurationService;
+
+    @Override
+    public void start(int jndi, int rmi, String host) {
+
+        JMXAdapter jmxAdapter = new JMXAdapter(jndi, rmi, host);
+        jmxAdapter.start();
+
+
+
+        // FIXME
+        // register something to show how it works, this registration
+        // should happen when the stats are collected, for example
+        // SystemStatisticsModule#getStatistics
+        JMXMBeansRegister.getInstance().registerMBean(new SystemStatistics(),
+                "Statistics", "Statistics");
+    }
+
+    public void initialize(@Observes SystemStartupEvent e) {
+        String host = configurationService.getStringConfiguration("jmx.host");
+        int jndiPort = configurationService.getIntConfiguration("jmx.jndi.port");
+        int rmiPort = configurationService.getIntConfiguration("jmx.rmi.port");
+
+        // FIXME
+        host = host == null ? "localhost" : host;
+        jndiPort = jndiPort == 0 ? 15000 : jndiPort;
+
+        start(jndiPort, rmiPort, host);
+    }
+
+
 }
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/SystemStatistics.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/SystemStatistics.java
index b1aff86..238325f 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/SystemStatistics.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/SystemStatistics.java
@@ -26,16 +26,31 @@
  */
 package org.apache.marmotta.platform.core.services.jmx;
 
+import java.util.LinkedHashMap;
+import java.util.Map;
+
 /**
  * author : [email protected]
  */
 public class SystemStatistics implements SystemStatisticsMBean {
 
-    public String getOS() {
-        return "Fedora";
-    }
+    @Override
+    public Map<String, String> getOSStatistics() {
+
+        // see SystemStatisticsModule#getStatistics
+
+        Map<String, String> result = new LinkedHashMap<>();
+
+        result.put("java version", System.getProperty("java.version"));
+        result.put("java vendor", System.getProperty("java.vendor"));
+        result.put("operating system", System.getProperty("os.name") + " (" + System.getProperty("os.version") + ")");
+
+        Runtime rt = Runtime.getRuntime();
+        result.put("free memory", "" + rt.freeMemory() / (1024 * 1024) + "MB");
+        result.put("total memory", "" + rt.totalMemory() / (1024 * 1024) + "MB");
+        result.put("max memory", "" + rt.maxMemory() / (1024 * 1024) + "MB");
+
+        return result;
 
-    public int getJDKVersion() {
-        return 7;
     }
 }
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/SystemStatisticsMBean.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/SystemStatisticsMBean.java
index 3c87119..f23351e 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/SystemStatisticsMBean.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/jmx/SystemStatisticsMBean.java
@@ -26,12 +26,13 @@
  */
 package org.apache.marmotta.platform.core.services.jmx;
 
+import java.util.Map;
+
 /**
  * author : [email protected]
  */
 public interface SystemStatisticsMBean {
 
-    public int getJDKVersion();
+    public Map<String, String> getOSStatistics();
 
-    public String getOS();
 }

Reply via email to