语法
create context context_name
start start_condition
end end_condition
如:
// 9点到17点此context才可用(以引擎的时间为准)。如果事件进入的事件不在此范围内,则不受该context影响 create context NineToFive start (0, 9, *, *, *) end (0, 17, *, *, *)
例子
import com.espertech.esper.client.*;/*** @author yaoyuan2* @date 2019/3/26*/ public class NoOverLappingContextTest {public static void main(String[] args) {EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();EPAdministrator admin = epService.getEPAdministrator();EPRuntime runtime = epService.getEPRuntime();String start = StartEvent.class.getName();String end = EndEvent.class.getName();String other = OtherEvent.class.getName();// 以StartEvent事件作为开始条件,EndEvent事件作为结束条件String epl1 = "create context NoOverLapping start " + start + " end " + end;String epl2 = "context NoOverLapping select * from " + other;admin.createEPL(epl1);EPStatement state = admin.createEPL(epl2);state.addListener(new UpdateListener() {@Overridepublic void update(EventBean[] newEvents, EventBean[] oldEvents) {if (newEvents != null) {EventBean event = newEvents[0];System.out.println("Class:" + event.getUnderlying().getClass().getName() + ", id:" + event.get("id"));}}});StartEvent s = new StartEvent();System.out.println("sendEvent: StartEvent");runtime.sendEvent(s);OtherEvent o = new OtherEvent();o.setId(2);System.out.println("sendEvent: OtherEvent");runtime.sendEvent(o);EndEvent e = new EndEvent();System.out.println("sendEvent: EndEvent");runtime.sendEvent(e);OtherEvent o2 = new OtherEvent();o2.setId(4);System.out.println("sendEvent: OtherEvent");runtime.sendEvent(o2);} } class StartEvent { }class EndEvent { } class OtherEvent {private int id;public int getId(){return id;}public void setId(int id){this.id = id;} }
输出
sendEvent: StartEvent sendEvent: OtherEvent Class:com.ebc.NoOverLappingContext.OtherEvent, id:2 sendEvent: EndEvent sendEvent: OtherEvent
由此看出,在NoOverLapping这个Context下监控OtherEvent,必须是在StartEvent被触发才能监控到,所以在EndEvent发送后,再发送一个OtherEvent是不会触发Listener的。