Announcement Announcement Module
Collapse
No announcement yet.
How to set up a JMX timer service via Spring? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to set up a JMX timer service via Spring?

    I've combed the web and see no mention of this. With the JMX API, you'd extend the Timer class (javax.management.timer.Timer), set the timer period, register the timer bean with the MBean server, set the notification(s), and register listeners.

    This is a lot of activity. Do I still need to do it "manually", or is there a Spring Framework mechanism with which to do this?

    Thanks!

  • #2
    This is actually a great feature and I did think about adding support for the various JMX services out of the box. Please create a feature request and I'll see what I can do.

    Rob

    Comment


    • #3
      Hi Rob,

      Thanks for the offer! I'll make you a deal: I'll tell you what I've done in exchange for asking a question or two. ;>

      I created the Timer bean and an "do something" bean that I registered with the Timer. Both of these beans are instantiated and registered with the MBean server via Spring. The Timer bean adds a notification and starts itself via a method specified in the Spring bean attribute 'init-method'. The "do-something" bean registers itself with the timer as a listener via the same mechanism. So far so good.

      I've noticed two things though, that I'm curious about.

      (1) I had to write an interface for my Timer bean, as it is extends javax.management.timer.Timer (which implements 'TimerMBean'), and Spring was exposing only the super class' methods (the public TimerMBean methods), but not my sub-class public methods. I did not have to create an interface (the normal non-Spring JMX API technique) for any of the other JMX beans -- Spring found and exposed these public methods without an interface. So it appears that Spring will use (expose) an interfaces' public methods if it finds one, even in cases where the results were not what one would expect.

      (2) I am relying on the Spring bean attribute 'init-method' to have Spring call methods to do some necessary setup activity. I've discovered through logging that these methods are consistently called twice. Is this Spring doing this? The app server is Tomcat 5.5.9. In 'spring-config.xml' I'm defining the beans, and then listing them to be exported (registered with the MBean server) like so:

      <bean id="exporter"
      class="org.springframework.jmx.export.MBeanExporte r"
      depends-on="mbeanServer">
      <property name="beans">
      <map>
      <entry ... />
      </map>
      </property>
      <property name="server" ref="mbeanServer"/>
      </bean>

      Is there something here that would cause the init-method's method to be called twice as the app server starts up?

      Thanks for any illumination, Rob.

      - Mark

      Comment


      • #4
        Sorry for the delay - work has been hectic. In answer to question 1 this is because Spring sees that the bean is already a valid MBean and doesn't do any further processing on it. It may well be an idea to allow you to choose which MBeans you want to expose as is and which ones you want Spring to work their magic on.

        Question 2 sounds like a more generic issue. Can you post the offending configuration and bean class?

        Rob

        Comment


        • #5
          Did you ever figure out why the bean was being initialized twice? I am experiencing the same behavior.

          Comment


          • #6
            Should work using MBeanExporterListener. Example code:


            Code:
            public class TimerMBeanExporterListener implements MBeanExporterListener {	
                    ....	
            	public void mbeanRegistered(ObjectName objectName) {
            		if (objectName.getCanonicalName().equals(TIMER_MBEAN_OBJECT_NAME)) {
            			timer.addNotificationListener(timerListener, null, null);
            			timer.addNotification("expiration", "expiration", null, new Date(), interval);
            			timer.start();
            		}
            	}
            
            	public void mbeanUnregistered(ObjectName objectName) {
            		//do nothing, timer is already stopped
            	}
            
            }
            Configuration:

            Code:
            	<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
            		<property name="beans">
            			<map>
            				<entry>
            					<key>
            						<util:constant static-field="eu.praha.iskr.po.co.cache.TimerMBeanExporterListener.TIMER_MBEAN_OBJECT_NAME"/>	
            					</key>
            					<ref bean="timer"/>
            				</entry>				 
            			</map>
            		</property>
            		<property name="listeners">
            			<list><ref bean="exporterListener"/></list>
            		</property>
            	</bean>
            		
            	<bean id="exporterListener" class="eu.praha.iskr.po.co.cache.TimerMBeanExporterListener">
            		<property name="interval" value="500"/>
            		<property name="timer" ref="timer"/>
            		<property name="timerListener" ref="evictionNotificationListener"/>
            	</bean>	
            	
            	<bean id="timer" class="javax.management.timer.Timer"/>
            Simon

            Comment


            • #7
              Hello,
              Thanks for providing this information related to timer.
              Actually i am looking for this and i suddenly found your post.

              Comment


              • #8
                the information is very useful,i will take it.thanks!

                Comment


                • #9
                  I use its Service but some days i have some issues about it.I read all the post about JMX timer service via Spring and get some ideas to solve my problems ....Thanks to share posts .....................

                  Comment


                  • #10
                    This is actually a great feature and I did think about adding support for the various JMX services out of the box. Please create a feature request and I'll see what I can do.

                    Comment

                    Working...
                    X