Announcement Announcement Module
Collapse
No announcement yet.
Howto create applicationlistener Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Howto create applicationlistener

    Hi to all!! (this is my first topic!)

    Sorry for my english, but I'm Italian.

    I want to create an applicationlistener to save one attribute in its namespace.
    My problem is: i don't know how to do that, on internet I didn't find nothing. :?

    Thanks all!

  • #2
    Event handling in the ApplicationContext is provided through the ApplicationEvent class and ApplicationListener interface. If a bean which implements the ApplicationListener interface is deployed into the context, every time an ApplicationEvent gets published to the ApplicationContext, that bean will be notified.


    Code:
    <bean id="emailer" class="example.EmailBean">
        <property name="blackList">
            <list>
                <value>[email protected]</value>
                <value>[email protected]</value>
                <value>[email protected]</value>
            </list>
        </property>
    </bean>
    
    <bean id="blackListListener" class="example.BlackListNotifier">
        <property name="notificationAddress">
            <value>[email protected]</value>
        </property>
    </bean>
    and then, the actual beans:

    Code:
    public class EmailBean implements ApplicationContextAware &#123;
    
        /** the blacklist */
        private List blackList;
        
        public void setBlackList&#40;List blackList&#41; &#123;
            this.blackList = blackList;
        &#125;
        
        public void setApplicationContext&#40;ApplicationContext ctx&#41; &#123;
            this.ctx = ctx;
        &#125;
        
        public void sendEmail&#40;String address, String text&#41; &#123;
            if &#40;blackList.contains&#40;address&#41;&#41; &#123;
                BlackListEvent evt = new BlackListEvent&#40;address, text&#41;;
                ctx.publishEvent&#40;evt&#41;;
                return;
            &#125;
            // send email
        &#125;
    &#125;
    
    public class BlackListNotifier implement ApplicationListener &#123;
    
        /** notification address */
        private String notificationAddress;
        
        public void setNotificationAddress&#40;String notificationAddress&#41; &#123;
            this.notificationAddress = notificationAddress;
        &#125;
    
        public void onApplicationEvent&#40;ApplicationEvent evt&#41; &#123;
            if &#40;evt instanceof BlackListEvent&#41; &#123;
                // notify appropriate person
            &#125;
        &#125;
    &#125;

    Comment


    • #3
      Thanks

      But, if I want to catch the fire event and do something???

      For example:
      -When application start create a bean for dbms connection, and save it in application context....
      -When application end destroy bean for dbms connection.

      In the end, I want to do something like that

      Thanks lot!

      Comment

      Working...
      X