| 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; |
| 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.StreamProcessor; |
| 24 | import org.wso2.siddhi.query.api.stream.SingleStream; |
| 25 | |
| 26 | import java.util.concurrent.ThreadPoolExecutor; |
| 27 | |
| 28 | public class SingleStreamReceiver implements Runnable, StreamReceiver, |
| 29 | StreamElement { |
| 30 | private SingleStream inputStream; |
| 31 | private ThreadPoolExecutor threadPoolExecutor; |
| 32 | private SchedulerQueue<StreamEvent> inputQueue = new SchedulerQueue<StreamEvent>(); |
| 33 | private StreamProcessor firstStreamProcessor; |
| 34 | |
| 35 | public SingleStreamReceiver(SingleStream inputStream, |
| 36 | StreamProcessor firstStreamProcessor, |
| 37 | ThreadPoolExecutor threadPoolExecutor) { |
| 38 | this.inputStream = inputStream; |
| 39 | this.firstStreamProcessor = firstStreamProcessor; |
| 40 | this.firstStreamProcessor.setPrevious(this); |
| 41 | this.threadPoolExecutor = threadPoolExecutor; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void receive(StreamEvent streamEvent) throws InterruptedException { |
| 46 | // //System.out.println(event); |
| 47 | if (!inputQueue.put(streamEvent)) { |
| 48 | threadPoolExecutor.submit(this); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void run() { |
| 54 | int eventCounter = 0; |
| 55 | while (true) { |
| 56 | StreamEvent streamEvent = inputQueue.poll(); |
| 57 | if (streamEvent == null) { |
| 58 | break; |
| 59 | } else if (eventCounter > 10) { |
| 60 | threadPoolExecutor.submit(this); |
| 61 | break; |
| 62 | } |
| 63 | eventCounter++; |
| 64 | try { |
| 65 | firstStreamProcessor.process(streamEvent); |
| 66 | } catch (Throwable e) { |
| 67 | e.printStackTrace(); |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public String getStreamId() { |
| 74 | return inputStream.getStreamId(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public SchedulerQueue<StreamEvent> getWindow() { |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | } |