AbstractWorker.java 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2017, OpenSkywalking Organization All rights reserved.
 *
 * Licensed 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
 *
 *     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.
 *
 * Project repository: https://github.com/OpenSkywalking/skywalking
 */

P
pengys5 已提交
19
package org.skywalking.apm.collector.stream.worker;
20 21

import org.skywalking.apm.collector.core.framework.Executor;
P
pengys5 已提交
22 23
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
P
pengys5 已提交
24

P
SpiTest  
pengys5 已提交
25
/**
26
 * @author pengys5
P
SpiTest  
pengys5 已提交
27
 */
28
public abstract class AbstractWorker<S extends WorkerRef> implements Executor {
P
pengys5 已提交
29

P
pengys5 已提交
30 31
    private final Logger logger = LoggerFactory.getLogger(AbstractWorker.class);

32
    private final Role role;
P
actor  
pengys5 已提交
33

34
    private final ClusterWorkerContext clusterContext;
P
pengys5 已提交
35

P
pengys5 已提交
36
    public AbstractWorker(Role role, ClusterWorkerContext clusterContext) {
37 38
        this.role = role;
        this.clusterContext = clusterContext;
P
pengys5 已提交
39 40
    }

41
    @Override public final void execute(Object message) {
P
pengys5 已提交
42 43 44 45 46
        try {
            onWork(message);
        } catch (WorkerException e) {
            logger.error(e.getMessage(), e);
        }
47 48
    }

P
pengys5 已提交
49 50 51 52 53 54 55 56
    /**
     * The data process logic in this method.
     *
     * @param message Cast the message object to a expect subclass.
     * @throws WorkerException Don't handle the exception, throw it.
     */
    protected abstract void onWork(Object message) throws WorkerException;

57
    public abstract void preStart() throws ProviderNotFoundException;
P
actor  
pengys5 已提交
58

59
    final public ClusterWorkerContext getClusterContext() {
60
        return clusterContext;
P
pengys5 已提交
61 62
    }

63 64
    final public Role getRole() {
        return role;
P
actor  
pengys5 已提交
65
    }
66 67 68 69

    protected abstract S getSelf();

    protected abstract void putSelfRef(S workerRef);
P
SpiTest  
pengys5 已提交
70
}