|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.dtstack.flink.sql.dirtyManager.consumer; |
| 20 | + |
| 21 | +import com.dtstack.flink.sql.dirtyManager.entity.DirtyDataEntity; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +import java.io.Serializable; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.concurrent.LinkedBlockingQueue; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 30 | +import java.util.concurrent.atomic.AtomicLong; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author tiezhu |
| 34 | + * Company dtstack |
| 35 | + * Date 2020/8/27 星期四 |
| 36 | + */ |
| 37 | +public abstract class AbstractDirtyDataConsumer implements Runnable, Serializable { |
| 38 | + protected static final long serialVersionUID = -6058598201315176687L; |
| 39 | + |
| 40 | + protected static final Logger LOG = LoggerFactory.getLogger(AbstractDirtyDataConsumer.class); |
| 41 | + |
| 42 | + protected Long errorLimit = 1000L; |
| 43 | + protected AtomicLong errorCount = new AtomicLong(0L); |
| 44 | + |
| 45 | + protected AtomicLong count = new AtomicLong(0L); |
| 46 | + |
| 47 | + public AtomicBoolean isRunning = new AtomicBoolean(true); |
| 48 | + |
| 49 | + protected LinkedBlockingQueue<DirtyDataEntity> queue; |
| 50 | + |
| 51 | + /** |
| 52 | + * 消费队列数据 |
| 53 | + * |
| 54 | + * @throws Exception throw exception |
| 55 | + */ |
| 56 | + public abstract void consume() throws Exception; |
| 57 | + |
| 58 | + /** |
| 59 | + * 关闭消费者,需要释放资源 |
| 60 | + */ |
| 61 | + public abstract void close(); |
| 62 | + |
| 63 | + /** |
| 64 | + * 初始化消费者,初始化定时任务 |
| 65 | + * |
| 66 | + * @param properties 任务参数 |
| 67 | + * @throws Exception throw exception |
| 68 | + */ |
| 69 | + public abstract void init(Map<String, Object> properties) throws Exception; |
| 70 | + |
| 71 | + /** |
| 72 | + * 检验consumer是否正在执行 |
| 73 | + */ |
| 74 | + public boolean isRunning() { |
| 75 | + return isRunning.get(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void run() { |
| 80 | + try { |
| 81 | + while (isRunning.get()) { |
| 82 | + consume(); |
| 83 | + } |
| 84 | + } catch (Exception e) { |
| 85 | + LOG.error("consume dirtyData error", e); |
| 86 | + if (errorCount.getAndIncrement() > errorLimit) { |
| 87 | + throw new RuntimeException("The task failed due to the number of dirty data consume failed reached the limit " + errorLimit); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public AbstractDirtyDataConsumer setQueue(LinkedBlockingQueue<DirtyDataEntity> queue) { |
| 93 | + this.queue = queue; |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + public void collectDirtyData(DirtyDataEntity dataEntity, long blockingInterval) throws InterruptedException { |
| 98 | + queue.offer(dataEntity, blockingInterval, TimeUnit.MILLISECONDS); |
| 99 | + } |
| 100 | +} |
0 commit comments