为什么所有事件都是大量而不是一个接一个?

时间:2015-07-21 14:04:57

标签: wso2 complex-event-processing wso2cep siddhi

我正在使用外部定时窗口测试非常简单的CEP查询。查询为define stream LoginEvents (timeStamp long, ip string, phone string); @info(name = 'query1') from LoginEvents#window.externalTime(timeStamp,5 sec) select timeStamp, ip insert all events into uniqueIps;;

查看单元测试here,我认为会发生的是回调将被调用9次,传入事件调用5次,到期调用4次。相反,它只被调用一次。为什么这样,我怎样才能到达为每个事件调用回调的状态?

2 个答案:

答案 0 :(得分:1)

这里所有事件都没有任何时间延迟发送到Siddhi,因此Siddhi将所有这些事件一起处理。这就是为什么事件作为批量返回。

如果要为每个事件调用回调,则必须扩展StreamCallback或QueryCallback并迭代返回的事件数组并为每个事件调用回调。

答案 1 :(得分:1)

这是我的实施。我必须复制send方法,因为它是私有的。您可以替换我的timestamp getter实现,因为它非常特定于我的用例。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.siddhi.core.event.ComplexEvent;
import org.wso2.siddhi.core.event.ComplexEventChunk;
import org.wso2.siddhi.core.event.Event;
import org.wso2.siddhi.core.event.stream.StreamEvent;
import org.wso2.siddhi.core.query.output.callback.QueryCallback;
import java.util.Arrays;

public abstract class CustomQueryCallback extends QueryCallback {

    private static final Logger log = LoggerFactory.getLogger(CustomQueryCallback.class);

    public void receiveStreamEvent(ComplexEventChunk complexEventChunk) {
        while (complexEventChunk.hasNext()) {
            ComplexEvent streamEvent = complexEventChunk.next();
            Event event = new Event(streamEvent.getOutputData().length).copyFrom(streamEvent);
            Event[] events = new Event[]{event};
            long timestamp = (streamEvent.getType() == StreamEvent.Type.EXPIRED ? streamEvent.getTimestamp() : (long) streamEvent.getOutputData()[2]);
            if (streamEvent.getType() == StreamEvent.Type.EXPIRED){
                send(timestamp, null, events);
            } else {
                send(timestamp, events, null);
            }
        }
    }

    private void send(long timeStamp, Event[] currentEvents, Event[] expiredEvents) {
        try {
            receive(timeStamp, currentEvents, expiredEvents);
        } catch (RuntimeException e) {
            log.error("Error on sending events" + Arrays.deepToString(currentEvents) + ", " + Arrays.deepToString(expiredEvents), e);
        }
    }

}