You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2016/06/02 17:23:21 UTC

tomee git commit: TOMEE-1826 CounterRepository auto registration

Repository: tomee
Updated Branches:
  refs/heads/master 475f7dfa5 -> 76ca07abb


TOMEE-1826 CounterRepository auto registration


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/76ca07ab
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/76ca07ab
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/76ca07ab

Branch: refs/heads/master
Commit: 76ca07abb077d68afde47156c93d389837d4ab73
Parents: 475f7df
Author: Romain manni-Bucau <rm...@gmail.com>
Authored: Thu Jun 2 19:22:58 2016 +0200
Committer: Romain manni-Bucau <rm...@gmail.com>
Committed: Thu Jun 2 19:22:58 2016 +0200

----------------------------------------------------------------------
 .../server/cxf/transport/util/CxfUtilTest.java  | 71 ++++++++++++++++++++
 .../server/cxf/transport/util/CxfUtil.java      | 29 +++++++-
 2 files changed, 97 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/76ca07ab/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/transport/util/CxfUtilTest.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/transport/util/CxfUtilTest.java b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/transport/util/CxfUtilTest.java
new file mode 100644
index 0000000..caa4970
--- /dev/null
+++ b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/transport/util/CxfUtilTest.java
@@ -0,0 +1,71 @@
+/*
+ *     Licensed to the Apache Software Foundation (ASF) under one or more
+ *     contributor license agreements.  See the NOTICE file distributed with
+ *     this work for additional information regarding copyright ownership.
+ *     The ASF licenses this file to You under the Apache License, Version 2.0
+ *     (the "License"); you may not use this file except in compliance with
+ *     the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *     Unless required by applicable law or agreed to in writing, software
+ *     distributed under the License is distributed on an "AS IS" BASIS,
+ *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *     See the License for the specific language governing permissions and
+ *     limitations under the License.
+ */
+package org.apache.openejb.server.cxf.transport.util;
+
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.loader.IO;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.ContainerProperties;
+import org.apache.openejb.testing.EnableServices;
+import org.apache.openejb.testing.RandomPort;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectInstance;
+import javax.management.ObjectName;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.net.URL;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+@EnableServices("jaxrs")
+@ContainerProperties({
+        @ContainerProperties.Property(name = "openejb.cxf.monitoring.jmx", value = "true"),
+        @ContainerProperties.Property(name = "openejb.cxf.monitoring.jmx.clear-on-undeploy", value = "true")
+})
+@Classes(cdi = true, innerClassesAsBean = true, context = "test")
+@RunWith(ApplicationComposer.class)
+public class CxfUtilTest {
+    @RandomPort("http")
+    private URL root;
+
+    @Test
+    public void checkMonitoring() throws IOException, MalformedObjectNameException {
+        // need a call to get something
+        IO.slurp(new URL(root.toExternalForm() + "test/monitoring/cxf"));
+        final Set<ObjectInstance> mbeans = ManagementFactory.getPlatformMBeanServer().queryMBeans(new ObjectName("*:*,type=Performance.Counter.Server"), null);
+        assertNotNull(mbeans);
+        assertEquals(2, mbeans.size());
+    }
+
+    @Path("monitoring/cxf")
+    public static class Endpoint {
+        @GET
+        @Produces(MediaType.TEXT_PLAIN)
+        public String get() {
+            return "ok";
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/76ca07ab/server/openejb-cxf-transport/src/main/java/org/apache/openejb/server/cxf/transport/util/CxfUtil.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-transport/src/main/java/org/apache/openejb/server/cxf/transport/util/CxfUtil.java b/server/openejb-cxf-transport/src/main/java/org/apache/openejb/server/cxf/transport/util/CxfUtil.java
index a7a7689..7c481f9 100644
--- a/server/openejb-cxf-transport/src/main/java/org/apache/openejb/server/cxf/transport/util/CxfUtil.java
+++ b/server/openejb-cxf-transport/src/main/java/org/apache/openejb/server/cxf/transport/util/CxfUtil.java
@@ -16,7 +16,6 @@
  */
 package org.apache.openejb.server.cxf.transport.util;
 
-import java.util.concurrent.atomic.AtomicInteger;
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.binding.BindingFactory;
@@ -30,12 +29,14 @@ import org.apache.cxf.feature.Feature;
 import org.apache.cxf.interceptor.Interceptor;
 import org.apache.cxf.interceptor.InterceptorProvider;
 import org.apache.cxf.management.InstrumentationManager;
+import org.apache.cxf.management.counters.CounterRepository;
 import org.apache.cxf.management.jmx.InstrumentationManagerImpl;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.transport.http.HttpDestinationFactory;
 import org.apache.openejb.OpenEJBRuntimeException;
 import org.apache.openejb.assembler.classic.OpenEjbConfiguration;
 import org.apache.openejb.assembler.classic.ServiceInfo;
+import org.apache.openejb.assembler.classic.event.AssemblerBeforeApplicationDestroyed;
 import org.apache.openejb.assembler.classic.event.AssemblerDestroyed;
 import org.apache.openejb.assembler.classic.util.ServiceConfiguration;
 import org.apache.openejb.assembler.classic.util.ServiceInfos;
@@ -55,6 +56,7 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.logging.Level;
 
@@ -269,7 +271,8 @@ public final class CxfUtil {
         }
 
         // activate jmx, by default isEnabled() == false in InstrumentationManagerImpl
-        if ("true".equalsIgnoreCase(systemInstance.getProperty("openejb.cxf.jmx", "true"))) {
+        final boolean hasMonitoring = hasMonitoring(systemInstance);
+        if (hasMonitoring || "true".equalsIgnoreCase(systemInstance.getProperty("openejb.cxf.jmx", "true"))) {
             final InstrumentationManager mgr = bus.getExtension(InstrumentationManager.class);
             if (InstrumentationManagerImpl.class.isInstance(mgr)) {
                 bus.setExtension(LocalMBeanServer.get(), MBeanServer.class); // just to keep everything consistent
@@ -287,9 +290,11 @@ public final class CxfUtil {
 
                 // failed when bus was constructed or even if passed we switch the MBeanServer
                 manager.init();
-                manager.register();
             }
         }
+        if (hasMonitoring) {
+            new CounterRepository().setBus(bus);
+        }
 
         final ServiceConfiguration configuration = new ServiceConfiguration(systemInstance.getProperties(),
                 systemInstance.getComponent(OpenEjbConfiguration.class).facilities.services);
@@ -320,6 +325,10 @@ public final class CxfUtil {
         systemInstance.fireEvent(new BusCreated(bus));
     }
 
+    private static boolean hasMonitoring(final SystemInstance systemInstance) {
+        return "true".equalsIgnoreCase(systemInstance.getProperty("openejb.cxf.monitoring.jmx", "false"));
+    }
+
     private static class ClientAwareBusHandler implements InvocationHandler {
         @Override
         public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
@@ -352,5 +361,19 @@ public final class CxfUtil {
             }
             systemInstance.removeObserver(this);
         }
+
+        public void destroy(@Observes final AssemblerBeforeApplicationDestroyed ignored) {
+            final SystemInstance systemInstance = SystemInstance.get();
+            final Bus bus = getBus();
+
+            // avoid to leak, we can enhance it to remove endpoints by app but not sure it does worth the effort
+            // alternative can be a bus per app but would enforce us to change some deeper part of our config/design
+            if ("true".equalsIgnoreCase(systemInstance.getProperty("openejb.cxf.monitoring.jmx.clear-on-undeploy", "true"))) {
+                final CounterRepository repo = bus.getExtension(CounterRepository.class);
+                if (repo != null) {
+                    repo.getCounters().clear();
+                }
+            }
+        }
     }
 }