Announcement Announcement Module
Collapse
No announcement yet.
Object retrieved from ioc turned null after JMX method invoker Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Object retrieved from ioc turned null after JMX method invoker

    I am using Spring 3.1 as standalone app.

    I have some wierd situation and I guess I am missing something.

    I have class which has method that I have exposed via JMX console. that method is invoking a method via bean.

    that bean I am initializing in advaned. the wierd thing that after i am invoking the method via the jmx console the bean instance turn to be null.

    Thats the JMX bean which is exposed:

    Code:
    public class TriggerBean implements IJmxTriggerBean
    {
        static Logger logger = Logger.getLogger(TriggerBean.class);
    
        FeedListenerBean fe = null;
    
        public void start()
        {
            try
            {
                // init();
                PropertyConfigurator.configure(FixGWConstants.LOG4J_PATH);
                ApplicationContext context = new ClassPathXmlApplicationContext(FixGWConstants.APPLICATION_CONTEXT_XML);
                fe = (FeedListenerBean) context.getBean("FeedListenerBean");
                doTheListen();
    
            }
            catch (Throwable t)
            {
                logger.error(t);
                System.out.println(t);
            }
    
        }
    
            //thats the method being exposed by jmx. pay attention that fe object has been   initialized before by the start()
        public void doTheListen()
        {
            fe.listen();
        }
    
        private void init()
        {
            Resource resource = new ClassPathResource(System.getProperty("user.dir") + "//config.properties");
            try
            {
                Properties props = PropertiesLoaderUtils.loadProperties(resource);
                String log4jPath = props.getProperty("LOG4J_PATH");
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
    applicationContext.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    
    
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
        <!-- Must for auto wiring 
        <context:annotation-config />
        -->
    
        <context:component-scan base-package="com.fixgw.beans">
        </context:component-scan>
    
    
    
        <!-- start a JMX Server -->
        <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean" />
    
        <bean id="FeedListenerBean" class="com.fixgw.beans.FeedListenerBean">
        </bean>
    
        <bean id="TriggerBean" class="com.fixgw.test.TriggerBean">
        </bean>

    I am testing it via standalone:

    Code:
    public static void main(String[] args) throws Exception
        {
    protected TriggerBean trigger = new Trigger();
    trigger.start();
    }
    When I first using start() the object fe is doing great. but after invoking doTheListen( via JMX the object fe remind null(although it has already been initialized before)

    any idea?

    thanks, ray.
    Last edited by ray.frid; Jul 2nd, 2012, 05:57 AM.

  • #2
    And why should it be set? You are creating your own instance of a bean, which in turn creates an ApplicationContext. I supose you have the TriggerBean also as a bean in thea pplication context, and you expect that that also receives the instance of fe? That obviously isn't going to work. You have 2 instances of the same bean 1 under spring control one isn't...

    Comment


    • #3
      How would you re-construct it in order to have the same instance from the ioc?

      Comment


      • #4
        Okie I understoond.. I should inject FeedListenerBean into TriggerBean(using @AutoWire) and have TriggerBean being initiated this way:

        Code:
        ApplicationContext context = new ClassPathXmlApplicationContext(FixGWConstants.APPLICATION_CONTEXT_XML);
        			trigger = (TriggerBean) context.getBean("TriggerBean");
        trigger.start();

        Comment


        • #5
          well yes and no . You should specify the start method as the init-method and that way it will be started for you (or if it is bound to the lifecycle of the container you could include the LifeCycle interface).

          Comment


          • #6
            So you basicly saying:

            1. I should use this code only once in my code in the beginning in order to load all my beans?

            Code:
            ApplicationContext context = new ClassPathXmlApplicationContext(FixGWConstants.APPLICATION_CONTEXT_XML);
            			trigger = (TriggerBean) context.getBean("TriggerBean");


            2. What if I have lots of beans and I want a separate file? I need to load more xml's

            Comment

            Working...
            X