@Override public void afterPropertiesSet() { // If no server was provided then try to find one. This is useful in an environment // where there is already an MBeanServer loaded. if (this.server == null) { this.server = JmxUtils.locateMBeanServer(); } }
/** * Return the class or interface to expose for the given bean class. * This is the class that will be searched for attributes and operations * @param beanClass the bean class (might be an AOP proxy class) * @return the bean class to expose * @see JmxUtils#getClassToExpose(Class) */ protected Class<?> getClassToExpose(Class<?> beanClass) { return JmxUtils.getClassToExpose(beanClass); }
/** * Set the {@code ObjectName} used to register the {@code JMXConnectorServer} * itself with the {@code MBeanServer}, as {@code ObjectName} instance * or as {@code String}. * @throws MalformedObjectNameException if the {@code ObjectName} is malformed */ public void setObjectName(Object objectName) throws MalformedObjectNameException { this.objectName = ObjectNameManager.getInstance(objectName); }
@Test public void startupWithLocatedServer() throws Exception { ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean(); bean.afterPropertiesSet(); try { checkServerConnection(getServer()); } finally { bean.destroy(); } }
@Override public MBeanServer getMBeanServer() { WebSphereMBeanServerFactoryBean fb = new WebSphereMBeanServerFactoryBean(); fb.afterPropertiesSet(); return fb.getObject(); } };
@Bean public MBeanServerFactoryBean server() throws Exception { return new MBeanServerFactoryBean(); } }
@Test public void testSimpleMBeanThroughInheritance() throws Exception { Bar bar = new Bar(); Abc abc = new Abc(); assertTrue("Simple MBean (through inheritance) not detected correctly", JmxUtils.isMBean(bar.getClass())); assertTrue("Simple MBean (through 2 levels of inheritance) not detected correctly", JmxUtils.isMBean(abc.getClass())); }
/** * Creates lazy proxies for the {@code JMXConnector} and {@code MBeanServerConnection}. */ private void createLazyConnection() { this.connectorTargetSource = new JMXConnectorLazyInitTargetSource(); TargetSource connectionTargetSource = new MBeanServerConnectionLazyInitTargetSource(); this.connector = (JMXConnector) new ProxyFactory(JMXConnector.class, this.connectorTargetSource).getProxy(this.beanClassLoader); this.connection = (MBeanServerConnection) new ProxyFactory(MBeanServerConnection.class, connectionTargetSource).getProxy(this.beanClassLoader); }
@Test public void testIsMBeanWithStandardMBeanInherited() throws Exception { StandardMBean mbean = new StandardMBeanImpl(); assertTrue("Standard MBean not detected correctly", JmxUtils.isMBean(mbean.getClass())); }
/** * Determine whether the given bean class qualifies as an MBean as-is. * <p>The default implementation delegates to {@link JmxUtils#isMBean}, * which checks for {@link javax.management.DynamicMBean} classes as well * as classes with corresponding "*MBean" interface (Standard MBeans) * or corresponding "*MXBean" interface (Java 6 MXBeans). * @param beanClass the bean class to analyze * @return whether the class qualifies as an MBean * @see org.springframework.jmx.support.JmxUtils#isMBean(Class) */ protected boolean isMBean(@Nullable Class<?> beanClass) { return JmxUtils.isMBean(beanClass); }
@Test(expected = IllegalArgumentException.class) public void testWithNoServiceUrl() throws Exception { MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean(); bean.afterPropertiesSet(); }
/** * Called when an MBean is registered under the given {@link ObjectName}. Allows * subclasses to perform additional processing when an MBean is registered. * <p>The default implementation delegates to {@link #onRegister(ObjectName)}. * @param objectName the actual {@link ObjectName} that the MBean was registered with * @param mbean the registered MBean instance */ protected void onRegister(ObjectName objectName, Object mbean) { onRegister(objectName); }
/** * Closes the underlying {@code JMXConnector}. */ @Override public void destroy() throws IOException { if (this.connector != null && (this.connectorTargetSource == null || this.connectorTargetSource.isInitialized())) { this.connector.close(); } }
@Test public void startupWithSuppliedServer() throws Exception { //Added a brief snooze here - seems to fix occasional //java.net.BindException: Address already in use errors Thread.sleep(1); ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean(); bean.setServer(getServer()); bean.afterPropertiesSet(); try { checkServerConnection(getServer()); } finally { bean.destroy(); } }
/** * Set the {@code ObjectName} of the MBean which calls are routed to, * as {@code ObjectName} instance or as {@code String}. */ public void setObjectName(Object objectName) throws MalformedObjectNameException { this.objectName = ObjectNameManager.getInstance(objectName); }
/** * 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. * @return the {@code MBeanServer} if found * @throws MBeanServerNotFoundException if no {@code MBeanServer} could be found * @see javax.management.MBeanServerFactory#findMBeanServer */ public static MBeanServer locateMBeanServer() throws MBeanServerNotFoundException { return locateMBeanServer(null); }
@Bean public MBeanServerFactoryBean server() throws Exception { return new MBeanServerFactoryBean(); }
/** * Return the class or interface to expose for the given bean. * This is the class that will be searched for attributes and operations * (for example, checked for annotations). * @param managedBean the bean instance (might be an AOP proxy) * @return the bean class to expose * @see JmxUtils#getClassToExpose(Object) */ protected Class<?> getClassToExpose(Object managedBean) { return JmxUtils.getClassToExpose(managedBean); }