com.alibaba.otter.shared.arbitrate.demo.servcie.ExtractServiceDemo.java Source code

Java tutorial

Introduction

Here is the source code for com.alibaba.otter.shared.arbitrate.demo.servcie.ExtractServiceDemo.java

Source

/*
 * Copyright (C) 2010-2101 Alibaba Group Holding Limited.
 *
 * 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.
 */

package com.alibaba.otter.shared.arbitrate.demo.servcie;

import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang.math.RandomUtils;

import com.alibaba.otter.shared.arbitrate.ArbitrateEventService;
import com.alibaba.otter.shared.arbitrate.model.EtlEventData;
import com.alibaba.otter.shared.common.utils.thread.NamedThreadFactory;
import com.google.common.collect.Maps;

/**
 * extract?
 * 
 * @author jianghang 2011-8-22 ?04:28:12
 */
public class ExtractServiceDemo implements PipelineLifeCycle {

    private ArbitrateEventService arbitrateEventService;
    private ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("ExtractService"));
    private Map<Long, Future> threads = Maps.newConcurrentMap();

    public void submit(Long pipelineId) {
        if (threads.containsKey(pipelineId)) {
            throw new IllegalArgumentException("pipeline is dup!");
        }

        Future future = executor.submit(new ExtractWorker(pipelineId));
        threads.put(pipelineId, future);
    }

    public void destory(Long pipelineId) {
        if (!threads.containsKey(pipelineId)) {
            throw new IllegalArgumentException("pipeline is not exist!");
        }

        Future future = threads.get(pipelineId);
        future.cancel(true);
    }

    private class ExtractWorker implements Runnable {

        private ExecutorService executor = Executors.newFixedThreadPool(10,
                new NamedThreadFactory("ExtractWorker"));
        private Long pipelineId;

        public ExtractWorker(Long pipelineId) {
            this.pipelineId = pipelineId;
        }

        public void run() {
            while (true) {
                try {
                    // ?single?eventData??nextNid?
                    final EtlEventData eventData = arbitrateEventService.extractEvent().await(pipelineId);
                    // ??
                    executor.submit(new Callable() {

                        @Override
                        public Object call() throws Exception {
                            Long nextNid = eventData.getNextNid();
                            // ?
                            Thread.sleep(500 + RandomUtils.nextInt(2000)); // sleep?

                            if (isLocal(nextNid)) {// ?
                                // pipe??
                                eventData.setDesc(new Object());// ?
                            } else {
                                // HTTPpipe??
                                eventData.setDesc(new Object());// ?
                            }

                            // ??
                            arbitrateEventService.extractEvent().single(eventData);
                            return true;
                        }

                    });

                } catch (InterruptedException e) {
                    System.out.printf("Pipeline [%s] Select is Interrupted", pipelineId);
                    // ?
                    break;
                } catch (Exception e) {
                    System.out.printf("Pipeline [%s] Select is error", pipelineId);
                    e.printStackTrace();
                }
            }
        }

        private boolean isLocal(Long nid) {
            // Long currentId = OtterClientServicesLocator.getConfigClientService().currentNode()
            // .getId();
            // return currentId.equals(nid);
            return true;
        }

    }

    public void setArbitrateEventService(ArbitrateEventService arbitrateEventService) {
        this.arbitrateEventService = arbitrateEventService;
    }

}