NEsper事件处理 z

http://esper.codehaus.org/nesper/documentation/documentation.html

 

环境配置

  1. NEsper库下载:下载网址
  2. Vs2010环境集成

在项目中添加下载的dll文件下的库即可。

  • Demo展示

事件类:

  1. public class ObjectEvent  
  2.     {  
  3.         public String itemName;  
  4.         public double price;  
  5.         public ObjectEvent(String itemName, double price)  
  6.         {  
  7.             this.itemName = itemName;  
  8.             this.price = price;  
  9.         }  
  10.         public String GetItemName()//命名规则必需正确,大小写不对会导致错误  
  11.         {  
  12.             return itemName;  
  13.         }  
  14.         public double GetPrice()  
  15.         {  
  16.             return price;  
  17.         }  
  18.     }  
public class ObjectEvent

    {

        public String itemName;

        public double price;

        public ObjectEvent(String itemName, double price)

        {

            this.itemName = itemName;

            this.price = price;

        }

        public String GetItemName()//命名规则必需正确,大小写不对会导致错误

        {

            return itemName;

        }

        public double GetPrice()

        {

            return price;

        }

    }
事件发送类
  1. class EventSender  
  2.     {  
  3.         private static EPServiceProvider epService = EPServiceProviderManager.GetDefaultProvider();  
  4.         static public void objectEventSend()  
  5.         {  
  6.             Configuration config = new Configuration();  
  7.             EPServiceProvider epService = EPServiceProviderManager.GetDefaultProvider();  
  8.             String epl =  
  9.                 "select *,sum(Price) from ConsoleApplicationNesper.ObjectEvent.win:length(2)";  
  10.   
  11.             EPStatement statement = epService.EPAdministrator.CreateEPL(epl);  
  12.             statement.Events += EventListener.objectEventHandle;  
  13.   
  14.             ObjectEvent e = new ObjectEvent("shirt", 74.50);  
  15.             Console.WriteLine("send shirt:74.50");  
  16.             epService.EPRuntime.SendEvent(e);  
  17.             ObjectEvent e1 = new ObjectEvent("shoes", 60.50);  
  18.             Console.WriteLine("send shoes:60.50");  
  19.             epService.EPRuntime.SendEvent(e1);  
  20.             ObjectEvent e2 = new ObjectEvent("coat", 70.50);  
  21.             Console.WriteLine("send coat:70.50");  
  22.             epService.EPRuntime.SendEvent(e2);  
  23.   
  24.   
  25.             Console.ReadKey();  
  26.         }  
  27. }  
class EventSender

    {

        private static EPServiceProvider epService = EPServiceProviderManager.GetDefaultProvider();

        static public void objectEventSend()

        {

            Configuration config = new Configuration();

            EPServiceProvider epService = EPServiceProviderManager.GetDefaultProvider();

            String epl =

                "select *,sum(Price) from ConsoleApplicationNesper.ObjectEvent.win:length(2)";



            EPStatement statement = epService.EPAdministrator.CreateEPL(epl);

            statement.Events += EventListener.objectEventHandle;



            ObjectEvent e = new ObjectEvent("shirt", 74.50);

            Console.WriteLine("send shirt:74.50");

            epService.EPRuntime.SendEvent(e);

            ObjectEvent e1 = new ObjectEvent("shoes", 60.50);

            Console.WriteLine("send shoes:60.50");

            epService.EPRuntime.SendEvent(e1);

            ObjectEvent e2 = new ObjectEvent("coat", 70.50);

            Console.WriteLine("send coat:70.50");

            epService.EPRuntime.SendEvent(e2);





            Console.ReadKey();

        }

}
事件监听类
  1. class EventListener  
  2.     {  
  3.         static public void objectEventHandle(Object sender, UpdateEventArgs e)  
  4.         {  
  5.             EventBean eventResult = e.NewEvents[0];  
  6.             Console.WriteLine("sumPrice:" + eventResult.Get("sum(Price)"));  
  7.             Console.WriteLine("received: " + eventResult.Get("ItemName") + ":"  
  8.                 + eventResult.Get("Price"));  
  9.             //Console.ReadKey();  
  10.         }  
  11. }  
class EventListener

    {

        static public void objectEventHandle(Object sender, UpdateEventArgs e)

        {

            EventBean eventResult = e.NewEvents[0];

            Console.WriteLine("sumPrice:" + eventResult.Get("sum(Price)"));

            Console.WriteLine("received: " + eventResult.Get("ItemName") + ":"

                + eventResult.Get("Price"));

            //Console.ReadKey();

        }

}
运行结果
  1. 1---object event example  
  2. send shirt:74.50  
  3. sumPrice:74.5  
  4. received: shirt:74.5  
  5. send shoes:60.50  
  6. sumPrice:135  
  7. received: shoes:60.5  
  8. send coat:70.50  
  9. sumPrice:131  
  10. received: coat:70.5  

你可能感兴趣的:(事件)