Announcement Announcement Module
Collapse
No announcement yet.
Desktop application, strange behaviour Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Desktop application, strange behaviour

    Hi,

    I tried to use Spring (1.1) in a Desktop application, but discovered some strange behaviours:
    • * Beans have not been initalized properly
      * Jobs habe not been executed (Quartz)

    In the case of bean initialization, I have a bean with static variables. After loading the application context, these variables should be initialized, but are not. Whenever I instanciate that bean (regular way), the static variables are empty, log statements in the accessor methods are signaling, that the accessor methods are never called. The same code works in a web application. - Why I do this - It is a tracing library built with aspectj which sends email if an exception will be thrown. I initialize the MailSender with Spring.

    In the case of the jobs, the same code/application context works in a web application, but not in the desktop application. To give you an idea, have a look at the code:
    Code:
        private BeanFactory factory;
    
        /**
         * Loads the Log4J configuration file and watches for changes.
         * 
         * @since 21.09.2004
         */
        static {
            DOMConfigurator.configureAndWatch(LOG4J_PATH);
        }
    
        /**
         * The main method.
         * 
         * @param args
         *            The arguments passed to the application.
         * 
         * @since 15.09.2004
         */
        public static void main(String[] args) {
            CDRRaterCoreEngine coreEngine = new CDRRaterCoreEngine();
    
            coreEngine.loadBeans();
    
            Lifecycle cycle = new Lifecycle();
            Thread thread = new Thread(cycle);
            thread.start();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException ex) {
    
            }
            coreEngine.crash();
        }
    
        /**
         * Loads the Spring Application Context from an XML file. If the XML file
         * does not exist, we try to load the application context from the
         * classpath.
         * 
         * @throws FileNotFoundException
         *             if the application context file could not be found.
         * 
         * @since 15.09.2004
         */
        private void loadBeans() {
            try {
                InputStream stream = new FileInputStream(APPLICATION_CONTEXT_PATH);
                factory = new XmlBeanFactory(stream);
            } catch (FileNotFoundException exception) {
                ClassPathResource resource = new ClassPathResource(
                        APPLICATION_CONTEXT_NAME);
                factory = new XmlBeanFactory(resource);
            }
        }
    
        private void crash() {
            String string = null;
            string.length();
        }
    ... and the application context:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//alaska.xxx/dtd/springframework-1.1/spring-beans.dtd" >
    <beans>
         <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
            <property name="host">
                <value>ntserv02</value>
            </property>
        </bean>
        <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
            <property name="from">
                <value><!&#91;CDATA&#91;Exception Service <exception@xxx>&#93;&#93;></value>
            </property>
            <property name="subject">
                <value>Exception occured</value>
            </property>
            <property name="to">
                <value>yy@xxx</value>
            </property>
        </bean>
        <bean id="exceptionSender" class="xxx.tools.logging.ExceptionSender">
            <property name="mailSender">
                <ref bean="mailSender"></ref>
            </property>
            <property name="message">
                <ref bean="mailMessage"></ref>
            </property>
        </bean>
        <bean id="cdrrater" class="xxx.cdrrater.CdrRater"></bean>
        <bean id="scheduler"
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref local="cdrraterTrigger" />
                </list>
            </property>
        </bean>
        <bean id="cdrraterJobDetail"
            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject">
                <ref bean="cdrrater" />
            </property>
            <property name="targetMethod">
                <value>rate</value>
            </property>
        </bean>
        <bean id="cdrraterTrigger"
            class="org.springframework.scheduling.quartz.SimpleTriggerBean">
            <property name="jobDetail">
                <ref local="cdrraterJobDetail" />
            </property>
            <property name="repeatInterval">
                <value>100</value>
            </property>
        </bean>
    </beans>
    Is there a simple explication of this behaviour?

    Thanks,

    Cyrill

  • #2
    Cyrill,

    The problem here is that you are not using an ApplicationContext you are using a BeanFactory. This means that no beans are instantiated up front. To overcome this either call XmlBeanFactory.preInstantiateSingletons() or, more preferable, swap XmlBeanFactory for something like FileSystemXmlApplicationContext or ClassPathXmlApplicationContext.

    Rob

    Comment


    • #3
      Hi,

      Thanks, It is working now

      Comment

      Working...
      X