Simple Java event-Event framework

Source: Internet
Author: User
Tags lenovo


Code: Http://git.oschina.net/jmpp/CommonEvent

Write yourself a simple Java event framework. Currently has the following features:

1. By inheriting the event class, users can customize events.

2. Issue an event via EventService's FireEvent (event e).

3. Monitor a class of events by implementing the Ieventhandler interface. EventHandler can be dynamically registered to Eventserver, or it can be configured in a configuration file.

4. By implementing the Ieventconsumedcallback interface, notifies the event issuer when the event is processed. Ieventconsumedcallback can be specified at fireevent or unspecified.

Both the 5.fireEvent and consume event are asynchronous and are handled by the thread pool consume event.


The class diagram is as follows:


Code: Http://git.oschina.net/jmpp/CommonEvent (contains test code)


Test:

1. Customizing the event

Import com.lenovo.commonevent.event;/** * Project:      commonevent * FileName:     Testevent.java * @Description: TODO * @author:      jmpp  * createdate:   March 16, 2015 PM 5:57:12 * Copyright:    Copyright (C) 2014-2015 *       Company Lenovo Ltd. * All rights Reserved, designed by Lenovo CIC. *//** * Class TestEvent Implementation Description: TODO class Implementation Description *  * @author JMPP March 16, 2015 PM 5:57:12 */public Class TestEvent extends Event {
   
    public testevent () {        super (TestEvent.class.getSimpleName ());    }}
   


2. Define a EventHandler

Import Java.util.date;import com.lenovo.commonevent.event;import com.lenovo.commonevent.ieventhandler;/** * Project :      commonevent * FileName:     Testeventhandler.java * @Description: TODO * @author:      jmpp * @version      V1.0  * CreateDate:   March 16, 2015 afternoon 6:00:34 * Copyright:    Copyright (C) 2014-2015 * Company       Lenovo Ltd. * All right S Reserved, designed by Lenovo CIC. *//** * Class Testeventhandler Implementation Description: TODO class Implementation Description *  * @author JMPP March 16, 2015 PM 6:00:34 */public Class Testeventhandler imple ments Ieventhandler {    /**     * @author JMPP March 16, 2015 PM 6:00:48     */    @Override public    Object OnEvent (Event event) {        System.out.println ("on Event  " + event.getid () + "Type:" + event.gettype ());        return new Date ();}    }


3. Define Eventconsumedcallback

import Java.util.date;import Com.lenovo.commonevent.event;import Com.lenovo.commonevent.eventservice;import com.lenovo.commonevent.ieventconsumedcallback;/** * project:commonevent * FileName:TestEventInvoker.java * @Des Cription:todo * @author: JMPP * @version V1.0 * createdate:2015 year March 16 afternoon 6:03:47 * Copyright:copyright (C 2014-2015 * Company Lenovo Ltd. * All rights Reserved, designed by Lenovo CIC. *//** * Class Testeventinvoker Implementation Description: TODO class Implementation Description * * @author JMPP March 16, 2015 PM 6:03:47 */public Class Testeventinvoker Implemen TS Ieventconsumedcallback {/** * @author jmpp March 16, 2015 PM 6:04:02 */@SuppressWarnings ("deprecation") @O Verride public void oneventfinished (event event, Object result) {System.out.println ("event callback" + EVENT.G    Etid () + "at" + ((Date) result). toLocaleString ()); }}


4. Test call trigger Event-〉 processing event (Handle), callback callback

Import Java.util.date;import Com.lenovo.commonevent.event;import Com.lenovo.commonevent.eventservice;import com.lenovo.commonevent.ieventconsumedcallback;/** * project:commonevent * FileName:TestEventInvoker2.java * @De Scription:todo * @author: JMPP * @version V1.0 * createdate:2015 year March 16 afternoon 6:03:47 * Copyright:copyright ( C) 2014-2015 * Company Lenovo Ltd. * All rights Reserved, designed by Lenovo CIC.    *//** * Class TestEventInvoker2 Implementation Description: TODO class Implementation Description * * @author JMPP March 16, 2015 afternoon 6:03:47 */public class TestEventInvoker2 {        public static void Main (String args[]) throws Exception {eventservice.init (null);        Eventservice.registereventhandler (TestEvent.class.getSimpleName (), New Testeventhandler ());            for (int i = 0; i < i++) {TestEvent event = new TestEvent ();        Eventservice.fireevent (event, New Testeventinvoker ());        } thread.sleep (5000);    Eventservice.stop (); }}


Simple Java event-Event framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.