javax.management.MBeanServerFactory#findMBeanServer ( )源码实例Demo

下面列出了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();
	}
}
 
源代码2 项目: gemfirexd-oss   文件: ReplicatedTree.java
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();
    }
 
源代码3 项目: lams   文件: JmxServiceImpl.java
/**
 * 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;
}
 
源代码4 项目: spring-analysis-note   文件: JmxUtils.java
/**
 * 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;
}
 
源代码5 项目: jdk8u_jdk   文件: DefaultLoaderRepository.java
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);
}
 
源代码6 项目: camel-spring-boot   文件: UnitTestCommand.java
private List<MBeanServer> getMBeanServers() {
    List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
    if (servers == null) {
        servers = Collections.emptyList();
    }
    return servers;
}
 
源代码7 项目: gmlc   文件: GmlcManagement.java
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");
}
 
源代码8 项目: dragonwell8_jdk   文件: DefaultLoaderRepository.java
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");
}
 
源代码10 项目: jdk8u-jdk   文件: DefaultLoaderRepository.java
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);
}
 
源代码11 项目: ironjacamar   文件: Server.java
/**
 * 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;
}
 
源代码12 项目: openjdk-jdk9   文件: DefaultLoaderRepository.java
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);
}
 
源代码13 项目: spring4-understanding   文件: JmxUtils.java
/**
 * 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;
}
 
源代码14 项目: summerframework   文件: RabbitMetricsBinder.java
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();
}
 
源代码16 项目: hottub   文件: DefaultLoaderRepository.java
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);
}
 
源代码17 项目: jdk8u-dev-jdk   文件: DefaultLoaderRepository.java
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);
	}
}
 
源代码19 项目: tomee   文件: MEJBBean.java
public MEJBBean() {
    final List mbeanServers = MBeanServerFactory.findMBeanServer(null);
    if (mbeanServers.size() > 0) {
        mbeanServer = (MBeanServer) mbeanServers.get(0);
    } else {
        mbeanServer = MBeanServerFactory.createMBeanServer();
    }
}
 
源代码20 项目: jdk8u-jdk   文件: DefaultLoaderRepository.java
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);
}