下面列出了javax.management.MBeanServerFactory#findMBeanServer ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void testCreation(boolean referenceShouldExist, String failMsg) throws Exception {
MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
bean.setRegisterWithFactory(referenceShouldExist);
bean.afterPropertiesSet();
try {
MBeanServer server = bean.getObject();
List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
boolean found = false;
for (MBeanServer candidate : servers) {
if (candidate == server) {
found = true;
break;
}
}
if (!(found == referenceShouldExist)) {
fail(failMsg);
}
}
finally {
bean.destroy();
}
}
public ReplicatedTree(String groupname, String props, long state_fetch_timeout, boolean jmx) throws Exception {
if(groupname != null)
this.groupname=groupname;
if(props != null)
this.props=props;
// this.jmx=jmx; GemStoneAddition
this.state_fetch_timeout=state_fetch_timeout;
channel=new JChannel(this.props);
channel.connect(this.groupname);
if(jmx) {
ArrayList servers=MBeanServerFactory.findMBeanServer(null);
if(servers == null || servers.size() == 0) {
throw new Exception("No MBeanServers found;" +
"\nJmxTest needs to be run with an MBeanServer present, or inside JDK 5");
}
// MBeanServer server=(MBeanServer)servers.get(0); GemStoneAddition
// JmxConfigurator.registerChannel(channel, server, "JGroups:channel=" + channel.getChannelName() , true);
}
start();
}
/**
* Locate the MBean server to use based on user input from startup.
*
* @return The MBean server to use.
*/
private MBeanServer findServer() {
if ( usePlatformServer ) {
// they specified to use the platform (vm) server
return ManagementFactory.getPlatformMBeanServer();
}
// otherwise lookup all servers by (optional) agentId.
// IMPL NOTE : the findMBeanServer call treats a null agentId to mean match all...
ArrayList<MBeanServer> mbeanServers = MBeanServerFactory.findMBeanServer( agentId );
if ( defaultDomain == null ) {
// they did not specify a domain by which to locate a particular MBeanServer to use, so chose the first
return mbeanServers.get( 0 );
}
for ( MBeanServer mbeanServer : mbeanServers ) {
// they did specify a domain, so attempt to locate an MBeanServer with a matching default domain, returning it
// if we find it.
if ( defaultDomain.equals( mbeanServer.getDefaultDomain() ) ) {
return mbeanServer;
}
}
return null;
}
/**
* Attempt to find a locally running {@code MBeanServer}. Fails if no
* {@code MBeanServer} can be found. Logs a warning if more than one
* {@code MBeanServer} found, returning the first one from the list.
* @param agentId the agent identifier of the MBeanServer to retrieve.
* If this parameter is {@code null}, all registered MBeanServers are considered.
* If the empty String is given, the platform MBeanServer will be returned.
* @return the {@code MBeanServer} if found
* @throws MBeanServerNotFoundException if no {@code MBeanServer} could be found
* @see javax.management.MBeanServerFactory#findMBeanServer(String)
*/
public static MBeanServer locateMBeanServer(@Nullable String agentId) throws MBeanServerNotFoundException {
MBeanServer server = null;
// null means any registered server, but "" specifically means the platform server
if (!"".equals(agentId)) {
List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(agentId);
if (!CollectionUtils.isEmpty(servers)) {
// Check to see if an MBeanServer is registered.
if (servers.size() > 1 && logger.isInfoEnabled()) {
logger.info("Found more than one MBeanServer instance" +
(agentId != null ? " with agent id [" + agentId + "]" : "") +
". Returning first from list.");
}
server = servers.get(0);
}
}
if (server == null && !StringUtils.hasLength(agentId)) {
// Attempt to load the PlatformMBeanServer.
try {
server = ManagementFactory.getPlatformMBeanServer();
}
catch (SecurityException ex) {
throw new MBeanServerNotFoundException("No specific MBeanServer found, " +
"and not allowed to obtain the Java platform MBeanServer", ex);
}
}
if (server == null) {
throw new MBeanServerNotFoundException(
"Unable to locate an MBeanServer instance" +
(agentId != null ? " with agent id [" + agentId + "]" : ""));
}
if (logger.isDebugEnabled()) {
logger.debug("Found MBeanServer: " + server);
}
return server;
}
private static Class<?> load(ClassLoader without, String className)
throws ClassNotFoundException {
final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);
for (MBeanServer mbs : mbsList) {
ClassLoaderRepository clr = mbs.getClassLoaderRepository();
try {
return clr.loadClassWithout(without, className);
} catch (ClassNotFoundException e) {
// OK : Try with next one...
}
}
throw new ClassNotFoundException(className);
}
private List<MBeanServer> getMBeanServers() {
List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
if (servers == null) {
servers = Collections.emptyList();
}
return servers;
}
public void start() throws Exception {
this.gmlcPropertiesManagement = GmlcPropertiesManagement.getInstance(this.name);
this.gmlcPropertiesManagement.setPersistDir(this.persistDir);
this.gmlcPropertiesManagement.start();
// Register the MBeans
boolean servFound = false;
String agentId = "jboss";
List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
if (servers != null && servers.size() > 0) {
for (MBeanServer server : servers) {
String defaultDomain = server.getDefaultDomain();
if (defaultDomain != null && defaultDomain.equals(agentId)) {
mbeanServer = server;
servFound = true;
logger.info(String.format("Found MBeanServer matching for agentId=%s", agentId));
} else {
logger.warn(String.format("Found non-matching MBeanServer with default domian = %s", defaultDomain));
}
}
}
if (!servFound) {
this.mbeanServer = ManagementFactory.getPlatformMBeanServer();
}
ObjectName gmlcPropObjNname = new ObjectName(GmlcManagement.JMX_DOMAIN + ":name=GmlcPropertiesManagement");
StandardMBean gmlcPropMxBean = new StandardMBean(this.gmlcPropertiesManagement,
GmlcPropertiesManagementMBean.class, true);
this.mbeanServer.registerMBean(gmlcPropMxBean, gmlcPropObjNname);
this.isStarted = true;
logger.info("Started GMLC Management");
}
private static Class<?> load(ClassLoader without, String className)
throws ClassNotFoundException {
final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);
for (MBeanServer mbs : mbsList) {
ClassLoaderRepository clr = mbs.getClassLoaderRepository();
try {
return clr.loadClassWithout(without, className);
} catch (ClassNotFoundException e) {
// OK : Try with next one...
}
}
throw new ClassNotFoundException(className);
}
private void testIsolation_javax_management_MBeanServerFactory_mBeanServerList() throws TenantException {
Runnable task = () -> {
try {
MBeanServerFactory.findMBeanServer(null);
} catch (Throwable t) { /* ignore */ }
};
testStaticFieldIsolation(MBeanServerFactory.class, task, "mBeanServerList");
}
private static Class<?> load(ClassLoader without, String className)
throws ClassNotFoundException {
final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);
for (MBeanServer mbs : mbsList) {
ClassLoaderRepository clr = mbs.getClassLoaderRepository();
try {
return clr.loadClassWithout(without, className);
} catch (ClassNotFoundException e) {
// OK : Try with next one...
}
}
throw new ClassNotFoundException(className);
}
/**
* Get the MBeanServer instance
* @param domain The domain
* @return The instance
*/
public static MBeanServer getMBeanServer(String domain)
{
try
{
ArrayList<MBeanServer> l = MBeanServerFactory.findMBeanServer(null);
if (l != null)
{
for (MBeanServer ms : l)
{
String[] domains = ms.getDomains();
if (domains != null)
{
for (String d : domains)
{
if (domain.equals(d))
{
return ms;
}
}
}
}
}
}
catch (SecurityException se)
{
// Ignore
}
return null;
}
private static Class<?> load(ClassLoader without, String className)
throws ClassNotFoundException {
final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);
for (MBeanServer mbs : mbsList) {
ClassLoaderRepository clr = mbs.getClassLoaderRepository();
try {
return clr.loadClassWithout(without, className);
} catch (ClassNotFoundException e) {
// OK : Try with next one...
}
}
throw new ClassNotFoundException(className);
}
/**
* Attempt to find a locally running {@code MBeanServer}. Fails if no
* {@code MBeanServer} can be found. Logs a warning if more than one
* {@code MBeanServer} found, returning the first one from the list.
* @param agentId the agent identifier of the MBeanServer to retrieve.
* If this parameter is {@code null}, all registered MBeanServers are considered.
* If the empty String is given, the platform MBeanServer will be returned.
* @return the {@code MBeanServer} if found
* @throws org.springframework.jmx.MBeanServerNotFoundException
* if no {@code MBeanServer} could be found
* @see javax.management.MBeanServerFactory#findMBeanServer(String)
*/
public static MBeanServer locateMBeanServer(String agentId) throws MBeanServerNotFoundException {
MBeanServer server = null;
// null means any registered server, but "" specifically means the platform server
if (!"".equals(agentId)) {
List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(agentId);
if (servers != null && servers.size() > 0) {
// Check to see if an MBeanServer is registered.
if (servers.size() > 1 && logger.isWarnEnabled()) {
logger.warn("Found more than one MBeanServer instance" +
(agentId != null ? " with agent id [" + agentId + "]" : "") +
". Returning first from list.");
}
server = servers.get(0);
}
}
if (server == null && !StringUtils.hasLength(agentId)) {
// Attempt to load the PlatformMBeanServer.
try {
server = ManagementFactory.getPlatformMBeanServer();
}
catch (SecurityException ex) {
throw new MBeanServerNotFoundException("No specific MBeanServer found, " +
"and not allowed to obtain the Java platform MBeanServer", ex);
}
}
if (server == null) {
throw new MBeanServerNotFoundException(
"Unable to locate an MBeanServer instance" +
(agentId != null ? " with agent id [" + agentId + "]" : ""));
}
if (logger.isDebugEnabled()) {
logger.debug("Found MBeanServer: " + server);
}
return server;
}
private static MBeanServer getMBeanServer() {
List<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
if (!mBeanServers.isEmpty()) {
return mBeanServers.get(0);
}
return ManagementFactory.getPlatformMBeanServer();
}
private static MBeanServer getMBeanServer() {
List<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
if (!mBeanServers.isEmpty()) {
return mBeanServers.get(0);
}
return ManagementFactory.getPlatformMBeanServer();
}
private static Class<?> load(ClassLoader without, String className)
throws ClassNotFoundException {
final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);
for (MBeanServer mbs : mbsList) {
ClassLoaderRepository clr = mbs.getClassLoaderRepository();
try {
return clr.loadClassWithout(without, className);
} catch (ClassNotFoundException e) {
// OK : Try with next one...
}
}
throw new ClassNotFoundException(className);
}
private static Class<?> load(ClassLoader without, String className)
throws ClassNotFoundException {
final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);
for (MBeanServer mbs : mbsList) {
ClassLoaderRepository clr = mbs.getClassLoaderRepository();
try {
return clr.loadClassWithout(without, className);
} catch (ClassNotFoundException e) {
// OK : Try with next one...
}
}
throw new ClassNotFoundException(className);
}
@Test
public void startEnvironment_test() throws SchedulerException {
assertNotNull(scheduler);
assertNotNull(schedulerFactory);
assertThat( scheduler.getSchedulerName()).isEqualTo("MyTestScheduler");
assertThat(scheduler.getSchedulerInstanceId()).isEqualTo("MyTestInstanceId");
try {
ManagementFactory.getPlatformMBeanServer();
List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
assertNotNull(servers);
assertThat(servers.size()).isGreaterThan(0);
MBeanServer server = servers.get(0);
List<String> domains = Arrays.asList(server.getDomains());
assertNotNull(domains);
assertThat(domains.size()).isGreaterThan(0);
String domain = "quartz";
assertThat(domains).doesNotContain(domain);
//wait a while until some jobs have been triggered
Thread.sleep(1000L);
assertThat(queueService.getGroups()).containsOnlyOnce(QueuedInstance.DEFAULT_GROUP, CallbackQueuedJob.GROUP);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
}
public MEJBBean() {
final List mbeanServers = MBeanServerFactory.findMBeanServer(null);
if (mbeanServers.size() > 0) {
mbeanServer = (MBeanServer) mbeanServers.get(0);
} else {
mbeanServer = MBeanServerFactory.createMBeanServer();
}
}
private static Class<?> load(ClassLoader without, String className)
throws ClassNotFoundException {
final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);
for (MBeanServer mbs : mbsList) {
ClassLoaderRepository clr = mbs.getClassLoaderRepository();
try {
return clr.loadClassWithout(without, className);
} catch (ClassNotFoundException e) {
// OK : Try with next one...
}
}
throw new ClassNotFoundException(className);
}