Java Code Examples for javax.management.MBeanServerFactory#findMBeanServer()

The following examples show how to use javax.management.MBeanServerFactory#findMBeanServer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MBeanServerFactoryBeanTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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();
	}
}
 
Example 2
Source File: JmxServiceImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: ReplicatedTree.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
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();
    }
 
Example 4
Source File: RabbitMetricsBinder.java    From summerframework with Apache License 2.0 5 votes vote down vote up
private static MBeanServer getMBeanServer() {
    List<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
    if (!mBeanServers.isEmpty()) {
        return mBeanServers.get(0);
    }
    return ManagementFactory.getPlatformMBeanServer();
}
 
Example 5
Source File: JmxUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: DefaultLoaderRepository.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 7
Source File: MEJBBean.java    From tomee with Apache License 2.0 5 votes vote down vote up
public MEJBBean() {
    final List mbeanServers = MBeanServerFactory.findMBeanServer(null);
    if (mbeanServers.size() > 0) {
        mbeanServer = (MBeanServer) mbeanServers.get(0);
    } else {
        mbeanServer = MBeanServerFactory.createMBeanServer();
    }
}
 
Example 8
Source File: QuartzSchedulerAutoConfig11Test.java    From spring-boot-starter-quartz with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example 9
Source File: DefaultLoaderRepository.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: DefaultLoaderRepository.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: DefaultLoaderRepository.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: KafkaProducerMetricsBinder.java    From summerframework with Apache License 2.0 5 votes vote down vote up
private static MBeanServer getMBeanServer() {
    List<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
    if (!mBeanServers.isEmpty()) {
        return mBeanServers.get(0);
    }
    return ManagementFactory.getPlatformMBeanServer();
}
 
Example 13
Source File: UnitTestCommand.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
private List<MBeanServer> getMBeanServers() {
    List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
    if (servers == null) {
        servers = Collections.emptyList();
    }
    return servers;
}
 
Example 14
Source File: JmxUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: DefaultLoaderRepository.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 16
Source File: Server.java    From ironjacamar with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 17
Source File: DefaultLoaderRepository.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 18
Source File: TestStaticFieldIsolation.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void testIsolation_javax_management_MBeanServerFactory_mBeanServerList() throws TenantException {
    Runnable task = () -> {
        try {
            MBeanServerFactory.findMBeanServer(null);
        } catch (Throwable t) { /* ignore */ }
    };
    testStaticFieldIsolation(MBeanServerFactory.class, task, "mBeanServerList");
}
 
Example 19
Source File: DefaultLoaderRepository.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 20
Source File: GmlcManagement.java    From gmlc with GNU Affero General Public License v3.0 5 votes vote down vote up
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");
}