LocalAsyncWorkerRef.java 2.5 KB
Newer Older
P
peng-yongsheng 已提交
1
/*
wu-sheng's avatar
wu-sheng 已提交
2 3 4 5 6 7
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
P
peng-yongsheng 已提交
8 9 10 11 12 13 14 15 16 17 18
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

19
package org.apache.skywalking.apm.collector.analysis.worker.model.base;
P
peng-yongsheng 已提交
20

P
peng-yongsheng 已提交
21
import java.util.Iterator;
P
peng-yongsheng 已提交
22
import java.util.List;
P
peng-yongsheng 已提交
23
import org.apache.skywalking.apm.collector.core.data.EndOfBatchQueueMessage;
24
import org.apache.skywalking.apm.collector.core.graph.NodeProcessor;
P
peng-yongsheng 已提交
25 26 27 28
import org.apache.skywalking.apm.commons.datacarrier.DataCarrier;
import org.apache.skywalking.apm.commons.datacarrier.consumer.IConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
P
peng-yongsheng 已提交
29 30 31 32

/**
 * @author peng-yongsheng
 */
P
peng-yongsheng 已提交
33
public class LocalAsyncWorkerRef<INPUT extends EndOfBatchQueueMessage, OUTPUT extends EndOfBatchQueueMessage> extends WorkerRef<INPUT, OUTPUT> implements IConsumer<INPUT> {
P
peng-yongsheng 已提交
34

P
peng-yongsheng 已提交
35 36 37
    private final Logger logger = LoggerFactory.getLogger(LocalAsyncWorkerRef.class);

    private DataCarrier<INPUT> dataCarrier;
P
peng-yongsheng 已提交
38

39
    LocalAsyncWorkerRef(NodeProcessor<INPUT, OUTPUT> destinationHandler) {
40
        super(destinationHandler);
41 42
    }

P
peng-yongsheng 已提交
43 44 45 46 47
    public void setQueueEventHandler(DataCarrier<INPUT> dataCarrier) {
        this.dataCarrier = dataCarrier;
    }

    @Override public void consume(List<INPUT> data) {
P
peng-yongsheng 已提交
48 49 50 51 52 53 54 55 56 57 58
        Iterator<INPUT> inputIterator = data.iterator();

        int i = 0;
        while (inputIterator.hasNext()) {
            INPUT input = inputIterator.next();
            i++;
            if (i == data.size()) {
                input.setEndOfBatch(true);
            }
            out(input);
        }
P
peng-yongsheng 已提交
59 60 61 62 63 64 65
    }

    @Override public void init() {
    }

    @Override public void onError(List<INPUT> data, Throwable t) {
        logger.error(t.getMessage(), t);
P
peng-yongsheng 已提交
66 67
    }

P
peng-yongsheng 已提交
68
    @Override public void onExit() {
69 70
    }

wu-sheng's avatar
wu-sheng 已提交
71
    @Override protected void in(INPUT input) {
P
peng-yongsheng 已提交
72
        dataCarrier.produce(input);
73 74
    }

wu-sheng's avatar
wu-sheng 已提交
75 76
    @Override protected void out(INPUT input) {
        super.out(input);
P
peng-yongsheng 已提交
77 78
    }
}