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.sequence; |
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 SequenceStreamReceiver 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<SequenceSingleStreamReceiver> sequenceSingleStreamReceiverList; |
35 | private int sequenceSingleStreamReceiverListSize; |
36 | private List<SequenceSingleStreamReceiver> otherStreamReceiverList; |
37 | private int otherStreamReceiverListSize; |
38 | |
39 | public SequenceStreamReceiver(String streamId, |
40 | List<SequenceSingleStreamReceiver> sequenceSingleStreamReceiverList, |
41 | ThreadPoolExecutor threadPoolExecutor) { |
42 | this.streamId = streamId; |
43 | this.sequenceSingleStreamReceiverList = sequenceSingleStreamReceiverList; |
44 | this.threadPoolExecutor = threadPoolExecutor; |
45 | this.sequenceSingleStreamReceiverListSize = sequenceSingleStreamReceiverList.size(); |
46 | } |
47 | |
48 | @Override |
49 | public void receive(StreamEvent event) throws InterruptedException { |
50 | // //System.out.println(event); |
51 | if (!inputQueue.put(event)) { |
52 | threadPoolExecutor.submit(this); |
53 | } |
54 | } |
55 | |
56 | @Override |
57 | public void run() { |
58 | int eventCounter = 0; |
59 | while (true) { |
60 | |
61 | StreamEvent streamEvent = inputQueue.poll(); |
62 | //System.out.println("thread "+Thread.currentThread().getName()+" "+streamEvent); |
63 | if (streamEvent == null) { |
64 | break; |
65 | } else if (eventCounter > 10) { |
66 | threadPoolExecutor.submit(this); |
67 | break; |
68 | } |
69 | eventCounter++; |
70 | try{ |
71 | //in reverse order to execute the later states first to overcome to dependencies of count states |
72 | for (int i = sequenceSingleStreamReceiverListSize - 1; i >= 0; i--) { |
73 | sequenceSingleStreamReceiverList.get(i).moveNextEventsToCurrentEvents(); |
74 | } |
75 | if (otherStreamReceiverListSize > 0) { |
76 | StreamEvent resetEvent = new SequenceResetEvent(System.currentTimeMillis()); |
77 | for (int i = 0, otherStreamReceiverListSize = otherStreamReceiverList.size(); i < otherStreamReceiverListSize; i++) { |
78 | otherStreamReceiverList.get(i).receive(resetEvent); |
79 | } |
80 | } |
81 | for (int i = sequenceSingleStreamReceiverListSize - 1; i >= 0; i--) { |
82 | sequenceSingleStreamReceiverList.get(i).receive(streamEvent); |
83 | } |
84 | }catch (Throwable t){ |
85 | t.printStackTrace(); |
86 | } |
87 | |
88 | } |
89 | } |
90 | |
91 | public String getStreamId() { |
92 | return streamId; |
93 | } |
94 | |
95 | @Override |
96 | public SchedulerQueue<StreamEvent> getWindow() { |
97 | return null; |
98 | } |
99 | |
100 | |
101 | public void setOtherStreamReceivers( |
102 | List<SequenceSingleStreamReceiver> otherStreamReceiverList) { |
103 | this.otherStreamReceiverList = otherStreamReceiverList; |
104 | otherStreamReceiverListSize = otherStreamReceiverList.size(); |
105 | } |
106 | |
107 | public List<SequenceSingleStreamReceiver> getSequenceSingleStreamReceiverList() { |
108 | return sequenceSingleStreamReceiverList; |
109 | } |
110 | } |