001/* 002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bdelbosc 018 */ 019package org.nuxeo.lib.stream.computation; 020 021import java.util.stream.Collectors; 022import java.util.stream.IntStream; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026 027/** 028 * An abstract {@link Computation} that manages the metadata init.<br/> 029 * By convention the inputs streams are named internally: i1, i2 ...<br/> 030 * and the output streams are named: o1, o2 ... <br/> 031 * 032 * @since 9.3 033 */ 034public abstract class AbstractComputation implements Computation { 035 private static final Log log = LogFactory.getLog(AbstractComputation.class); 036 037 protected final ComputationMetadata metadata; 038 039 public static final String INPUT_1 = "i1"; 040 041 public static final String INPUT_2 = "i2"; 042 043 public static final String INPUT_3 = "i3"; 044 045 public static final String OUTPUT_1 = "o1"; 046 047 public static final String OUTPUT_2 = "o2"; 048 049 public static final String OUTPUT_3 = "o3"; 050 051 public static final String OUTPUT_4 = "o4"; 052 053 /** 054 * Creates a computation with the requested number of input and output streams.<br/> 055 * 056 * @since 10.3 057 */ 058 public AbstractComputation(String name, int nbInputStreams, int nbOutputStreams) { 059 this.metadata = new ComputationMetadata(name, 060 IntStream.range(1, nbInputStreams + 1).boxed().map(i -> "i" + i).collect(Collectors.toSet()), 061 IntStream.range(1, nbOutputStreams + 1).boxed().map(i -> "o" + i).collect(Collectors.toSet())); 062 } 063 064 @Override 065 public void init(ComputationContext context) { 066 067 } 068 069 @Override 070 public void processTimer(ComputationContext context, String key, long timestamp) { 071 072 } 073 074 @Override 075 public ComputationMetadata metadata() { 076 return metadata; 077 } 078 079 @Override 080 public void processRetry(ComputationContext context, Throwable failure) { 081 log.warn(String.format("Computation: %s fails last record: %s, retrying ...", metadata.name(), 082 context.getLastOffset()), failure); 083 } 084 085 @Override 086 public void processFailure(ComputationContext context, Throwable failure) { 087 log.error(String.format("Computation: %s fails last record: %s, after retries.", metadata.name(), 088 context.getLastOffset()), failure); 089 } 090}