esper 事件引擎,各种事件类型示例代码

原创文章 转载请注明出处

package com.hp.iot.engine.esper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.espertech.esper.client.EPAdministrator;
import com.espertech.esper.client.EPRuntime;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.UpdateListener;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class EsperTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();

        EPAdministrator admin = epService.getEPAdministrator();
        Map<String, Object> address = new HashMap<String, Object>();
        Map<String, Object> Person = new HashMap<String, Object>();
        Map<String, Object> children = new HashMap<String, Object>();
        children.put("name", String.class);
        children.put("age", int.class);

        admin.getConfiguration().addEventType("children_test", children);

        address.put("road", String.class);
        address.put("buildnum", int.class);
        address.put("host", String.class);
        admin.getConfiguration().addEventType("address_test", address);


        Person.put("name", String.class);
        Person.put("age", int.class);
        Person.put("addr", "address_test");
        Person.put("children", "children_test[]");
        admin.getConfiguration().addEventType("person_test", Person);


        String epl = "select * from person_test where addr.road=\"r1\"";


        EPStatement state = admin.createEPL(epl);
        state.addListener(new UpdateListener() {

            public void update(EventBean[] paramArrayOfEventBean1, EventBean[] paramArrayOfEventBean2) {
                // TODO Auto-generated method stub
                System.out.println("test %%%%%%%%%%%%%");
                String json = JSON.toJSONString(paramArrayOfEventBean1[0]);
                System.out.println("json %%%%%%%%%%%%%" + json);

            }
        });

        String epl2 = "select * from person_test where children[1].name=\"ch2\"";

        EPStatement state2 = admin.createEPL(epl2, "ep2", null);
        System.out.println("----------------------" + state2.getText());
        state2.addListener(new UpdateListener() {

            public void update(EventBean[] paramArrayOfEventBean1, EventBean[] paramArrayOfEventBean2) {
                // TODO Auto-generated method stub
                System.out.println("index ############");
                String json = JSON.toJSONString(paramArrayOfEventBean1[0]);
                System.out.println("json ############" + json);

            }
        });

        String epl3 = "select * from person_test";
        EPStatement state3 = admin.createEPL(epl3, "epl3", null);
        System.out.println("----------------------" + state2.getText());
        state2.addListener(new UpdateListener() {

            public void update(EventBean[] paramArrayOfEventBean1, EventBean[] paramArrayOfEventBean2) {
                // TODO Auto-generated method stub
                System.out.println("------------------epl 3");
            }
        });


        EPRuntime runtime = epService.getEPRuntime();

        Map<String, Object> add = new HashMap<String, Object>();
        add.put("road", "r1");
        add.put("buildnum", 2);
        add.put("host", "host1");

        Map<String, Object> ch1 = new HashMap<String, Object>();
        ch1.put("name", "ch1");
        ch1.put("age", 2);

        Map<String, Object> ch2 = new HashMap<String, Object>();
        ch2.put("name", "ch2");
        ch2.put("age", 2);

        Map[] child = new HashMap[2];
        child[0] = ch1;
        child[1] = ch2;

        Map<String, Object> per = new HashMap<String, Object>();
        per.put("name", "roger");
        per.put("age", 2);
        per.put("addr", add);
        per.put("children", child);
        runtime.sendEvent(per, "person_test");

     
    }

}

 

posted on 2016-03-09 14:18  Yudar  阅读(1795)  评论(0编辑  收藏  举报