0

I am using JMX in Spring application with XML configuration:

<bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter">
        <property name="beans">
            <map>
                <entry key="bean:name=bean1" value-ref="bean1"/>
                <entry key="bean:name=bean2" value-ref="bean2"/>
                <entry key="bean:name=bean3" value-ref="bean3"/>
            </map>
        </property>
        <property name="notificationListenerMappings">
          <map>
             <entry key="*">
                <bean class="com.test.listener"/>
             </entry>
         </map>
        </property>
    </bean>

    <bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
        <property name="port" value="1099" />
    </bean>

    <bean id="serverConnector"
        class="org.springframework.jmx.support.ConnectorServerFactoryBean">
        <property name="objectName" value="connector:name=rmi" />
        <property name="serviceUrl"
            value="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmxrmi" />
    </bean>

I understand from various documents like instead of this XML configuration we could annotate it with @EnableMBeanExport and with @ManagedResource for the beans.

But i doubt how ConnectorServerFactoryBean gets configured with these annotations. Or is there any annotation available to configure RMI and connectorServerFactoryBean?

Also i need to know how to annotate, notificationListenerMappings configured?

P.S: I have the code working for publisher and listener under XML configuration. I am planning to move it completely on annotation based as i do not want to disturb XML configuration already in PROD.

Edited

Found the following piece of code: planning to try it:

@Bean
public RmiRegistryFactoryBean registry() {
    return new RmiRegistryFactoryBean();
}

@Bean
@DependsOn("registry")
public ConnectorServerFactoryBean connectorServer() throws MalformedObjectNameException {
    ConnectorServerFactoryBean connectorServerFactoryBean = new ConnectorServerFactoryBean();
    connectorServerFactoryBean.setObjectName("connector:name=rmi");
    connectorServerFactoryBean.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/connector");
    return connectorServerFactoryBean;
}

Edit 2: I am proceeding on above mentioned approach and I am able to configure MBeans and able to publish notifications. But unfortunately I am stuck up with configuring NotificationListener through Annotation.

I tried adding the following:

     @Bean
     @DependsOn("registry")
     public ConnectorServerFactoryBean connectorServer() throws MalformedObjectNameException {
         ConnectorServerFactoryBean connectorServerFactoryBean = new ConnectorServerFactoryBean();
         connectorServerFactoryBean.setObjectName("connector:name=rmi");
         connectorServerFactoryBean.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/connector");
         //TestListener is my NotificationListener class
         ObjectName objectName = new ObjectName("bean:name=bean1");
             connectorServerFactoryBean.getServer().addNotificationListener(objectName,
                             new TestListener(), null,null);
         return connectorServerFactoryBean;
     }

I am getting instanceNotFoundException stating bean:name=bean1 is not found. But I have configured like, @ManagedResource(objectName="bean:name=bean1") on my bean1.

Any help please on what i am missing?

DecKno
  • 293
  • 1
  • 5
  • 21

1 Answers1

1

@EnableMBeanExport has a server property, which reference the bean name of a server object.

see for example the test of this component, which use this server property : https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/test/java/org/springframework/jmx/export/annotation/EnableMBeanExportConfigurationTests.java

Jérémie B
  • 10,611
  • 1
  • 26
  • 43
  • Thank you @Jérémie B. Will that "server" really take in whatever configured in serviceConnector bean configured in my question? – DecKno Feb 05 '16 at 10:54
  • Theorically yes. It look for a MBeanServer instance – Jérémie B Feb 05 '16 at 11:02
  • After identified, as in the link you provide, the MBean server, how does Spring know which class has method to call via JMX? Is it enough have `@ManagedResource` and `@ManagedOperation` on a class? – Stefano Scarpanti Jul 26 '17 at 09:47