Merged branch 'jetty-9.4.x' into 'master'.
This commit is contained in:
commit
f488bd4118
|
@ -30,8 +30,10 @@ import javax.management.MBeanRegistrationException;
|
|||
import javax.management.MBeanServer;
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import org.eclipse.jetty.util.annotation.ManagedObject;
|
||||
import org.eclipse.jetty.util.component.Container;
|
||||
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
||||
import org.eclipse.jetty.util.component.Destroyable;
|
||||
import org.eclipse.jetty.util.component.Dumpable;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
|
@ -39,7 +41,8 @@ import org.eclipse.jetty.util.log.Logger;
|
|||
/**
|
||||
* Container class for the MBean instances
|
||||
*/
|
||||
public class MBeanContainer implements Container.InheritedListener, Dumpable
|
||||
@ManagedObject("The component that registers beans as MBeans")
|
||||
public class MBeanContainer implements Container.InheritedListener, Dumpable, Destroyable
|
||||
{
|
||||
private final static Logger LOG = Log.getLogger(MBeanContainer.class.getName());
|
||||
private final static ConcurrentMap<String, AtomicInteger> __unique = new ConcurrentHashMap<>();
|
||||
|
@ -254,6 +257,7 @@ public class MBeanContainer implements Container.InheritedListener, Dumpable
|
|||
return ContainerLifeCycle.dump(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy()
|
||||
{
|
||||
_beans.values().stream()
|
||||
|
|
|
@ -0,0 +1,116 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.jmx;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
||||
import org.eclipse.jetty.util.thread.QueuedThreadPool;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MBeanContainerLifeCycleTest
|
||||
{
|
||||
private ContainerLifeCycle container;
|
||||
private MBeanServer mbeanServer;
|
||||
|
||||
@Before
|
||||
public void prepare() throws Exception
|
||||
{
|
||||
container = new ContainerLifeCycle();
|
||||
mbeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
MBeanContainer mbeanContainer = new MBeanContainer(mbeanServer);
|
||||
container.addBean(mbeanContainer);
|
||||
container.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void dispose() throws Exception
|
||||
{
|
||||
container.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddBeanRegistersMBeanRemoveBeanUnregistersMBean() throws Exception
|
||||
{
|
||||
// Adding a bean to the container should register the MBean.
|
||||
QueuedThreadPool bean = new QueuedThreadPool();
|
||||
container.addBean(bean);
|
||||
|
||||
String pkg = bean.getClass().getPackage().getName();
|
||||
Set<ObjectName> objectNames = mbeanServer.queryNames(ObjectName.getInstance(pkg + ":*"), null);
|
||||
Assert.assertEquals(1, objectNames.size());
|
||||
|
||||
// Removing the bean should unregister the MBean.
|
||||
container.removeBean(bean);
|
||||
objectNames = mbeanServer.queryNames(ObjectName.getInstance(pkg + ":*"), null);
|
||||
Assert.assertEquals(0, objectNames.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStoppingContainerDoesNotUnregistersMBeans() throws Exception
|
||||
{
|
||||
QueuedThreadPool bean = new QueuedThreadPool();
|
||||
container.addBean(bean, true);
|
||||
|
||||
String pkg = bean.getClass().getPackage().getName();
|
||||
Set<ObjectName> objectNames = mbeanServer.queryNames(ObjectName.getInstance(pkg + ":*"), null);
|
||||
Assert.assertEquals(1, objectNames.size());
|
||||
|
||||
container.stop();
|
||||
|
||||
objectNames = mbeanServer.queryNames(ObjectName.getInstance(pkg + ":*"), null);
|
||||
Assert.assertEquals(1, objectNames.size());
|
||||
|
||||
// Remove the MBeans to start clean on the next test.
|
||||
objectNames.forEach(objectName ->
|
||||
{
|
||||
try
|
||||
{
|
||||
mbeanServer.unregisterMBean(objectName);
|
||||
}
|
||||
catch (Throwable ignored)
|
||||
{
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDestroyingContainerUnregistersMBeans() throws Exception
|
||||
{
|
||||
QueuedThreadPool bean = new QueuedThreadPool();
|
||||
container.addBean(bean, true);
|
||||
|
||||
String pkg = bean.getClass().getPackage().getName();
|
||||
Set<ObjectName> objectNames = mbeanServer.queryNames(ObjectName.getInstance(pkg + ":*"), null);
|
||||
Assert.assertEquals(1, objectNames.size());
|
||||
|
||||
container.stop();
|
||||
container.destroy();
|
||||
|
||||
objectNames = mbeanServer.queryNames(ObjectName.getInstance(pkg + ":*"), null);
|
||||
Assert.assertEquals(0, objectNames.size());
|
||||
}
|
||||
}
|
|
@ -18,39 +18,26 @@
|
|||
|
||||
package org.eclipse.jetty.jmx;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import com.acme.Managed;
|
||||
import org.eclipse.jetty.util.component.Container;
|
||||
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import com.acme.Managed;
|
||||
|
||||
public class MBeanContainerTest
|
||||
{
|
||||
|
||||
private MBeanContainer mbeanContainer;
|
||||
|
||||
private MBeanServer mbeanServer;
|
||||
|
||||
private String domain;
|
||||
|
||||
private String beanName;
|
||||
|
||||
private Managed managed;
|
||||
|
||||
private ObjectName objectName;
|
||||
|
||||
private Integer mbeanCount;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
|
@ -68,7 +55,7 @@ public class MBeanContainerTest
|
|||
beanName = mbeanContainer.makeName(beanName);
|
||||
|
||||
// then
|
||||
assertEquals("Bean name should be mngd_bean","mngd_bean",beanName);
|
||||
Assert.assertEquals("Bean name should be mngd_bean", "mngd_bean", beanName);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -79,11 +66,11 @@ public class MBeanContainerTest
|
|||
|
||||
// when
|
||||
objectName = mbeanContainer.findMBean(managed);
|
||||
assertNotNull(objectName);
|
||||
Assert.assertNotNull(objectName);
|
||||
|
||||
// then
|
||||
assertEquals("Bean must be added",managed,mbeanContainer.findBean(objectName));
|
||||
assertNull("It must return null as there is no bean with the name null",mbeanContainer.findBean(null));
|
||||
Assert.assertEquals("Bean must be added", managed, mbeanContainer.findBean(objectName));
|
||||
Assert.assertNull("It must return null as there is no bean with the name null", mbeanContainer.findBean(null));
|
||||
}
|
||||
|
||||
private Managed getManaged()
|
||||
|
@ -92,33 +79,33 @@ public class MBeanContainerTest
|
|||
beanName = mbeanContainer.makeName(beanName);
|
||||
Managed managed = new Managed();
|
||||
managed.setManaged(beanName);
|
||||
mbeanContainer.beanAdded(null,managed);
|
||||
mbeanContainer.beanAdded(null, managed);
|
||||
return managed;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMBeanContainer()
|
||||
{
|
||||
assertNotNull("Container shouldn't be null",mbeanContainer);
|
||||
Assert.assertNotNull("Container shouldn't be null", mbeanContainer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMBeanServer()
|
||||
{
|
||||
assertEquals("MBean server Instance must be equal",mbeanServer,mbeanContainer.getMBeanServer());
|
||||
Assert.assertEquals("MBean server Instance must be equal", mbeanServer, mbeanContainer.getMBeanServer());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDomain()
|
||||
{
|
||||
// given
|
||||
domain = "Test";
|
||||
String domain = "Test";
|
||||
|
||||
// when
|
||||
mbeanContainer.setDomain(domain);
|
||||
|
||||
// then
|
||||
assertEquals("Domain name must be Test",domain,mbeanContainer.getDomain());
|
||||
Assert.assertEquals("Domain name must be Test", domain, mbeanContainer.getDomain());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -131,7 +118,7 @@ public class MBeanContainerTest
|
|||
objectName = mbeanContainer.findMBean(managed);
|
||||
|
||||
// then
|
||||
assertTrue("Bean must have been registered",mbeanServer.isRegistered(objectName));
|
||||
Assert.assertTrue("Bean must have been registered", mbeanServer.isRegistered(objectName));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -139,20 +126,20 @@ public class MBeanContainerTest
|
|||
{
|
||||
// given
|
||||
setBeanAdded();
|
||||
mbeanCount = mbeanServer.getMBeanCount();
|
||||
Integer mbeanCount = mbeanServer.getMBeanCount();
|
||||
|
||||
// when
|
||||
mbeanContainer.beanAdded(null,null);
|
||||
mbeanContainer.beanAdded(null, null);
|
||||
|
||||
// then
|
||||
assertEquals("Mbean count must not change after beanAdded(null, null) call",mbeanCount,mbeanServer.getMBeanCount());
|
||||
Assert.assertEquals("MBean count must not change after beanAdded(null, null) call", mbeanCount, mbeanServer.getMBeanCount());
|
||||
}
|
||||
|
||||
private void setBeanAdded()
|
||||
{
|
||||
managed = new Managed();
|
||||
Container container = new ContainerLifeCycle();
|
||||
mbeanContainer.beanAdded(container,managed);
|
||||
mbeanContainer.beanAdded(container, managed);
|
||||
mbeanServer = mbeanContainer.getMBeanServer();
|
||||
}
|
||||
|
||||
|
@ -163,17 +150,17 @@ public class MBeanContainerTest
|
|||
setUpBeanRemoved();
|
||||
|
||||
// when
|
||||
mbeanContainer.beanRemoved(null,managed);
|
||||
mbeanContainer.beanRemoved(null, managed);
|
||||
|
||||
// then
|
||||
assertNull("Bean shouldn't be registed with container as we removed the bean",mbeanContainer.findMBean(managed));
|
||||
Assert.assertNull("Bean shouldn't be registered with container as we removed the bean", mbeanContainer.findMBean(managed));
|
||||
}
|
||||
|
||||
private void setUpBeanRemoved()
|
||||
{
|
||||
managed = new Managed();
|
||||
mbeanContainer.beanRemoved(null,managed);
|
||||
mbeanContainer.beanAdded(null,managed);
|
||||
mbeanContainer.beanRemoved(null, managed);
|
||||
mbeanContainer.beanAdded(null, managed);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -187,51 +174,51 @@ public class MBeanContainerTest
|
|||
mbeanContainer.getMBeanServer().unregisterMBean(objectName);
|
||||
|
||||
// then
|
||||
assertFalse("Bean must not have been registered as we unregisted the bean",mbeanServer.isRegistered(objectName));
|
||||
Assert.assertFalse("Bean must not have been registered as we unregistered the bean", mbeanServer.isRegistered(objectName));
|
||||
// this flow covers InstanceNotFoundException. Actual code just eating
|
||||
// the exception. i.e Actual code just printing the stacktrace, whenever
|
||||
// an exception of type InstanceNotFoundException occurs.
|
||||
mbeanContainer.beanRemoved(null,managed);
|
||||
mbeanContainer.beanRemoved(null, managed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDump()
|
||||
{
|
||||
assertNotNull("Dump operation shouldn't return null if operation is success",mbeanContainer.dump());
|
||||
Assert.assertNotNull("Dump operation shouldn't return null if operation is success", mbeanContainer.dump());
|
||||
}
|
||||
|
||||
private void setUpDestoy()
|
||||
private void setUpDestroy()
|
||||
{
|
||||
managed = new Managed();
|
||||
mbeanContainer.beanAdded(null,managed);
|
||||
mbeanContainer.beanAdded(null, managed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDestroy() throws Exception
|
||||
{
|
||||
// given
|
||||
setUpDestoy();
|
||||
setUpDestroy();
|
||||
|
||||
// when
|
||||
mbeanContainer.destroy();
|
||||
objectName = mbeanContainer.findMBean(managed);
|
||||
|
||||
// then
|
||||
assertFalse("Unregisted bean- managed",mbeanContainer.getMBeanServer().isRegistered(objectName));
|
||||
Assert.assertFalse("Unregistered bean - managed", mbeanContainer.getMBeanServer().isRegistered(objectName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDestroyInstanceNotFoundException() throws Exception
|
||||
{
|
||||
// given
|
||||
setUpDestoy();
|
||||
setUpDestroy();
|
||||
objectName = mbeanContainer.findMBean(managed);
|
||||
|
||||
// when
|
||||
mbeanContainer.getMBeanServer().unregisterMBean(objectName);
|
||||
|
||||
// then
|
||||
assertFalse("Unregisted bean- managed",mbeanContainer.getMBeanServer().isRegistered(objectName));
|
||||
Assert.assertFalse("Unregistered bean - managed", mbeanContainer.getMBeanServer().isRegistered(objectName));
|
||||
// this flow covers InstanceNotFoundException. Actual code just eating
|
||||
// the exception. i.e Actual code just printing the stacktrace, whenever
|
||||
// an exception of type InstanceNotFoundException occurs.
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
|
||||
package org.eclipse.jetty.util.component;
|
||||
|
||||
import org.eclipse.jetty.util.annotation.ManagedObject;
|
||||
import org.eclipse.jetty.util.annotation.ManagedOperation;
|
||||
|
||||
/**
|
||||
* <p>A Destroyable is an object which can be destroyed.</p>
|
||||
|
@ -25,7 +27,9 @@ package org.eclipse.jetty.util.component;
|
|||
* resources over multiple start/stop cycles. A call to destroy will release all
|
||||
* resources and will prevent any further start/stop cycles from being successful.</p>
|
||||
*/
|
||||
@ManagedObject
|
||||
public interface Destroyable
|
||||
{
|
||||
@ManagedOperation(value = "Destroys this component", impact = "ACTION")
|
||||
void destroy();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue