Reprinted please indicate the source: http://blog.csdn.net/luonanqin
If you have mastered the EPL syntax, you can easily deal with many business scenarios. However, there are always some complicated scenarios where the basic syntax is not enough. For example, when a user requests to log on to the service, the user fails to log on to the service for M consecutive times within n seconds, the IP address is considered as a brute-force password cracking attack. Or, if the user's operation interval on the page exceeds n seconds, the user is deemed to have closed the page. The above examples may not be good enough or have been implemented in other ways, but Esper can indeed abstract it into multiple associated events for processing. The processing of related events, such as pattern, is the central content of this article. It is like a custom template. If an event enters the engine according to the template, it will trigger the listener, we can capture the signal and perform subsequent processing.
Today, I will talk about some basic knowledge to get you started. The specific syntax will be detailed in the next article.
1. Pattern atoms and pattern Operators
Pattern forms a template by combining atomic events and operators. There are three types of atomic events and four types of operators:
Atomic event:
1 ).Common events: including pojo, MAP, array, and XML
2 ).Time event: including n time units, crontab
3 ).Custom Plugin: used to observe the occurrence of a specific event
OPERATOR:
1 ).Repeated operators: every, every-distinct, [num] And
2 ).Logical operators: And, or, not
3 ).Sequence OPERATOR:-> (followed)
4 ).Event lifecycle OPERATOR: Timer: Within, Timer: withinmax, while-expression, custom plug-in
Operators naturally have the following priority:
| Precedence |
Operator |
Description |
Example |
| 1 |
Guard Postfix |
Where Timer: Within and while (expression) (incl. withinmax and plug-in pattern guard) |
Myevent where Timer: Within (1 sec) |
| 2 |
Unary |
Every, not every, distinct |
Every myevent Timer: interval (5 min) and not myevent |
| 3 |
Repeat |
[Num], |
[5] myevent [1 .. 3] myevent until mytherevent
|
| 4 |
And |
And |
Every (myevent and myotherevent) |
| 5 |
Or |
Or |
Every (myevent or myotherevent) |
| 6 |
Followed |
-> |
Every (myevent-> mytherevent) |
You can first have an impression on the content above, so that you can easily understand the details later.
2. Pattern filter expression
There is no difference between the filter expression of pattern and the ordinary expression, so I will not explain it further. You can take a look at the following examples. You don't have to worry about what it means except filter.
1 ).Every e1 = rfidevent-> E2 = rfidevent (assetid = e1.assetid)
2 ).Every e1 = rfidevent-> E2 = rfidevent (mylib. isinradius (e1.x, e1.y, x, y) and zone in (1, e1.zone ))
3 ).Every (rfidevent (Zone> 1) and rfidevent (Zone <10 ))
3. Controlling event consumption
As mentioned above, because pattern can be composed of multiple atomic events, there will naturally be multiple filters. Under normal circumstances, all filters will determine the events that enter the engine, however, when we only need to determine one time, as long as a filter is satisfied, other filters do not need to worry about this event. Esper considers this requirement. We only need to add a @ consume annotation after the filter expression. This annotation can follow a number to indicate the filtering priority. The default priority is 1. A greater value indicates a higher priority.
In order to combine the knowledge above, I gave a complete example:
package example;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;/** * Created by Luonanqin on 8/11/14. */class ConsumeEvent {private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String toString() {return "ConsumeEvent{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}';}}class PatternConsumeListener1 implements UpdateListener {public void update(EventBean[] newEvents, EventBean[] oldEvents) {if (newEvents != null) {for (int i = 0; i < newEvents.length; i++) {System.out.println("a: " + newEvents[i].get("a"));System.out.println("b: " + newEvents[i].get("b"));}}}}class PatternConsumeListener2 implements UpdateListener {public void update(EventBean[] newEvents, EventBean[] oldEvents) {if (newEvents != null) {for (int i = 0; i < newEvents.length; i++) {System.out.println("a: " + newEvents[i].get("a"));System.out.println("b: " + newEvents[i].get("b"));System.out.println("c: " + newEvents[i].get("c"));}}}}public class PatternConsumeTest {public static void main(String[] args) {EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();EPAdministrator admin = epService.getEPAdministrator();EPRuntime runtime = epService.getEPRuntime();String consume = ConsumeEvent.class.getName();String epl1 = "every (a=" + consume + "(id = 1)@consume and b=" + consume + "(name = 'luonq'))";System.out.println("EPL1: " + epl1 + "\n");EPStatement stat1 = admin.createPattern(epl1);stat1.addListener(new PatternConsumeListener1());ConsumeEvent ce1 = new ConsumeEvent();ce1.setId(1);ce1.setName("luonq");System.out.println("Send Event: " + ce1);runtime.sendEvent(ce1);System.out.println();ConsumeEvent ce2 = new ConsumeEvent();ce2.setId(2);ce2.setName("luonq");System.out.println("Send Event: " + ce2);runtime.sendEvent(ce2);stat1.destroy();System.out.println();String epl2 = "every (a=" + consume + "(id = 1)@consume(2) or b=" + consume + "(name = 'luonq')@consume(3) or c=" + consume + "(age > 2))";System.out.println("EPL2: " + epl2 + "\n");EPStatement stat2 = admin.createPattern(epl2);stat2.addListener(new PatternConsumeListener2());ConsumeEvent ce3 = new ConsumeEvent();ce3.setId(1);ce3.setName("luonq");ce3.setAge(3);System.out.println("Send Event: " + ce3);runtime.sendEvent(ce3);ConsumeEvent ce4 = new ConsumeEvent();ce4.setId(1);ce4.setName("luonqin");ce4.setAge(1);System.out.println("Send Event: " + ce4);runtime.sendEvent(ce4);ConsumeEvent ce5 = new ConsumeEvent();ce5.setId(3);ce5.setName("luonqin");ce5.setAge(5);System.out.println("Send Event: " + ce5);runtime.sendEvent(ce5);}}
Execution result:
EPL1: every (a=example.ConsumeEvent(id = 1)@consume and b=example.ConsumeEvent(name = 'luonq'))Send Event: ConsumeEvent{id=1, name='luonq', age=0}Send Event: ConsumeEvent{id=2, name='luonq', age=0}a: ConsumeEvent{id=1, name='luonq', age=0}b: ConsumeEvent{id=2, name='luonq', age=0}EPL2: every (a=example.ConsumeEvent(id = 1)@consume(2) or b=example.ConsumeEvent(name = 'luonq')@consume(3) or c=example.ConsumeEvent(age > 2))Send Event: ConsumeEvent{id=1, name='luonq', age=3}a: nullb: ConsumeEvent{id=1, name='luonq', age=3}c: nullSend Event: ConsumeEvent{id=1, name='luonqin', age=1}a: ConsumeEvent{id=1, name='luonqin', age=1}b: nullc: nullSend Event: ConsumeEvent{id=3, name='luonqin', age=5}a: nullb: nullc: ConsumeEvent{id=3, name='luonqin', age=5}The every keyword indicates that the engine matches each event with pattern, regardless of whether the previous match is complete. Or and mean or and, which means satisfying or satisfying all.
Remove the consume execution result:
EPL1: every (a=example.ConsumeEvent(id = 1) and b=example.ConsumeEvent(name = 'luonq'))Send Event: ConsumeEvent{id=1, name='luonq', age=0}a: ConsumeEvent{id=1, name='luonq', age=0}b: ConsumeEvent{id=1, name='luonq', age=0}Send Event: ConsumeEvent{id=2, name='luonq', age=0}EPL2: every (a=example.ConsumeEvent(id = 1) or b=example.ConsumeEvent(name = 'luonq') or c=example.ConsumeEvent(age > 2))Send Event: ConsumeEvent{id=1, name='luonq', age=3}a: ConsumeEvent{id=1, name='luonq', age=3}b: nullc: nullSend Event: ConsumeEvent{id=1, name='luonqin', age=1}a: ConsumeEvent{id=1, name='luonqin', age=1}b: nullc: nullSend Event: ConsumeEvent{id=3, name='luonqin', age=5}a: nullb: nullc: ConsumeEvent{id=3, name='luonqin', age=5}The above example may not be very understandable, but it does not matter. It will be very easy to wait until the operators are explained in detail later.
This article has little content, mainly to share some essential basic knowledge with you. The next article will focus on operators. If the length is too long, it may be divided into two articles for you. Please wait ......
Esper 14: pattern (1)