Write your own framework-Practice (event Framework)

Source: Internet
Author: User
Tags eventbus

To achieve high performance, cache is the key, and cache processing is the key

I have been paying attention to performance recently. I think back to the previous cache writing scenarios, and I am confused about synchronization.CodeAll of them are written in the business method. what's even worse is that the cache code is involved. As a result, multiple methods also involve a lot of code to process the cache, which is basically not structured.

Therefore, the eventframework is roughly written and has not been optimized. Because reflection is used, the efficiency is not high and optimization is required (just developed today ...)

Code download

This eventframework can split business methods and related events (event subscription), for example:

    • Event subscription:
    • After the order. Add function is called, eventframework can be called one by one based on the subscribed events (Cache processing can be placed here, And the cache processing structure is relatively controllable)
    • Di injection:
    • Constructor injection is not yet supported. Currently, only field and Attribute-level injection is supported.

 

For example

Assume that after order. Add, the following cache must be updated:

    • Various order list caches in the front-end member center (multiple cache items may need to be updated)
    • Cache of the Order List seen by employees who process multiple orders in the background (multiple cache items may need to be updated)

If the event framework is used, the Code is as follows:

Public   Static   Void  Configeventprocessors ()// This is the configuration for subscribing to event and event processing processor.{  //  Subscribe orderaddedevent Sysruntime. eventbus. subscribe <orderaddedevent, adminpendingorderlistcachebuilder> (); Sysruntime. eventbus. subscribe <Orderaddedevent, peruserorderlistcachebuilder> ();  //  Subscribe orderdeletedevent Sysruntime. eventbus. subscribe <orderdeletedevent, sampleorderdeletedeventprocessor> ();  //  Subscribe ordersavedevent Sysruntime. eventbus. subscribe <ordersavedevent, sampleordersavedeventprocessor> ();} 

Then, paste the event to the business logic interface.

 Public   Interface  Iorderbusiness {[raiseevent (  Typeof  (Ordersavedevent)]// Multiple identical events[Raiseevent (  Typeof (Ordersavedevent)]  Int  Add (orderentity order); [raiseevent (  Typeof  (Ordersavedevent)]  Bool  Save (orderentity order); [raiseevent (  Typeof  (Orderdeletedevent)]  Bool Delete ( Int  Orderid );} 

 

The above is a tag to trigger the event. You can also use direct triggering to trigger the event:

  Public   int   Add (orderentity order) { ///   validate  console. writeline ( "  In add  "   );   int  neworderid =  repository. insert (order);   If  (neworderid>  0  ) sysruntime. eventbus. raiseevent  
   
    (order); /// trigger event in code mode   return   neworderid;}  
  

 

Finally, the presentation layer code:

 Class  Program {  Static   Void Main ( String  [] ARGs) {startupconfig. configeventprocessors ();// Configure the association between event and event Processor Sysruntime. Container. register <iorderappfacade, orderapplication> ();// Isolate interfaces from specific implementation classesSysruntime. Container. Register <Iorderbusiness, ordercomponent>();// Same as aboveSysruntime. Container. Register <Iorderrepository, fakeorderrepository> ();// Same as aboveIorderappfacade orderapp = Sysruntime. Container. Resolve <iorderappfacade> ();// Obtain an instanceOrderapp. addorder (  "  Test  "  );}} 

 

Output:

 

Code download

 

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.