| 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.handler.window; |
| 19 | |
| 20 | import org.wso2.siddhi.core.event.ComplexEvent; |
| 21 | import org.wso2.siddhi.core.event.Event; |
| 22 | import org.wso2.siddhi.core.event.ListEvent; |
| 23 | import org.wso2.siddhi.core.event.StreamEvent; |
| 24 | import org.wso2.siddhi.core.event.remove.RemoveEvent; |
| 25 | import org.wso2.siddhi.core.event.remove.RemoveListEvent; |
| 26 | import org.wso2.siddhi.core.event.remove.RemoveStream; |
| 27 | import org.wso2.siddhi.query.api.expression.constant.IntConstant; |
| 28 | |
| 29 | import java.util.concurrent.TimeUnit; |
| 30 | |
| 31 | public class TimeWindowHandler extends WindowHandler implements Runnable { |
| 32 | |
| 33 | int timeToKeep; |
| 34 | |
| 35 | @Override |
| 36 | public void setParameters(Object[] parameters) { |
| 37 | if (parameters[0] instanceof Integer) { |
| 38 | timeToKeep = (Integer) parameters[0]; |
| 39 | } else { |
| 40 | timeToKeep = ((IntConstant) parameters[0]).getValue(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void process(ComplexEvent complexEvent) { |
| 46 | if (complexEvent instanceof StreamEvent) { |
| 47 | try { |
| 48 | StreamEvent streamEvent; |
| 49 | if (complexEvent instanceof Event) { |
| 50 | streamEvent = new RemoveEvent(((Event) complexEvent), System.currentTimeMillis() + timeToKeep); |
| 51 | } else { |
| 52 | streamEvent = new RemoveListEvent(((ListEvent) complexEvent).getEvents(), System.currentTimeMillis() + timeToKeep); |
| 53 | } |
| 54 | if (!getWindow().put(streamEvent)) { |
| 55 | getEventRemoverScheduler().schedule(this, timeToKeep, TimeUnit.MILLISECONDS); |
| 56 | } |
| 57 | } catch (InterruptedException e) { |
| 58 | e.printStackTrace(); |
| 59 | } |
| 60 | getNextPreStreamFlowProcessor().process(complexEvent); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | @Override |
| 66 | public void run() { |
| 67 | while (true) { |
| 68 | StreamEvent streamEvent = getWindow().peek(); |
| 69 | try { |
| 70 | if (streamEvent == null) { |
| 71 | break; |
| 72 | } |
| 73 | long timeDiff = ((RemoveStream) streamEvent).getExpiryTime() - System.currentTimeMillis(); |
| 74 | if (timeDiff > 0) { |
| 75 | getEventRemoverScheduler().schedule(this, timeDiff, TimeUnit.MILLISECONDS); |
| 76 | break; |
| 77 | } else { |
| 78 | streamEvent = getWindow().poll(); |
| 79 | getNextPreStreamFlowProcessor().process(streamEvent); |
| 80 | } |
| 81 | } catch (Throwable e) { |
| 82 | e.printStackTrace(); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |