1 | /* |
2 | * Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. |
3 | * |
4 | * WSO2 Inc. licenses this file to you under the Apache License, |
5 | * Version 2.0 (the "License"); you may not use this file except |
6 | * in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, |
12 | * software distributed under the License is distributed on an |
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | * KIND, either express or implied. See the License for the |
15 | * specific language governing permissions and limitations |
16 | * under the License. |
17 | */ |
18 | package org.wso2.siddhi.core.stream.recevier.pattern; |
19 | |
20 | import org.wso2.siddhi.core.event.StreamEvent; |
21 | import org.wso2.siddhi.core.util.SchedulerQueue; |
22 | import org.wso2.siddhi.core.stream.StreamElement; |
23 | import org.wso2.siddhi.core.stream.recevier.StreamReceiver; |
24 | |
25 | import java.util.List; |
26 | import java.util.concurrent.ThreadPoolExecutor; |
27 | |
28 | public class PatternStreamReceiver implements StreamReceiver, StreamElement, Runnable { |
29 | |
30 | // private List<SingleStream> inputStreamList; |
31 | private String streamId; |
32 | private ThreadPoolExecutor threadPoolExecutor; |
33 | private SchedulerQueue<StreamEvent> inputQueue = new SchedulerQueue<StreamEvent>(); |
34 | private List<PatternSingleStreamReceiver> patternSingleStreamReceiverList; |
35 | private int patternSingleStreamReceiverListSize; |
36 | |
37 | public PatternStreamReceiver(String streamId, |
38 | List<PatternSingleStreamReceiver> patternSingleStreamReceiverList, |
39 | ThreadPoolExecutor threadPoolExecutor) { |
40 | this.streamId = streamId; |
41 | this.patternSingleStreamReceiverList = patternSingleStreamReceiverList; |
42 | this.threadPoolExecutor = threadPoolExecutor; |
43 | this.patternSingleStreamReceiverListSize = patternSingleStreamReceiverList.size(); |
44 | } |
45 | |
46 | @Override |
47 | public void receive(StreamEvent streamEvent) throws InterruptedException { |
48 | // //System.out.println(event); |
49 | if (!inputQueue.put(streamEvent)) { |
50 | threadPoolExecutor.submit(this); |
51 | } |
52 | } |
53 | |
54 | @Override |
55 | public void run() { |
56 | int eventCounter = 0; |
57 | while (true) { |
58 | StreamEvent streamEvent = inputQueue.poll(); |
59 | if (streamEvent == null) { |
60 | break; |
61 | } else if (eventCounter > 10) { |
62 | threadPoolExecutor.submit(this); |
63 | break; |
64 | } |
65 | eventCounter++; |
66 | try { |
67 | //in reverse order to execute the later states first to overcome to dependencies of count states |
68 | for (int i = patternSingleStreamReceiverListSize - 1; i >= 0; i--) { |
69 | patternSingleStreamReceiverList.get(i).moveNextEventsToCurrentEvents(); |
70 | } |
71 | for (int i = patternSingleStreamReceiverListSize - 1; i >= 0; i--) { |
72 | patternSingleStreamReceiverList.get(i).receive(streamEvent); |
73 | } |
74 | } catch (Throwable e) { |
75 | e.printStackTrace(); |
76 | } |
77 | |
78 | } |
79 | } |
80 | |
81 | public String getStreamId() { |
82 | return streamId; |
83 | } |
84 | |
85 | @Override |
86 | public SchedulerQueue<StreamEvent> getWindow() { |
87 | return null; |
88 | } |
89 | |
90 | |
91 | } |