
org.talend.sdk.component.runtime.input.StreamingInputImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of component-runtime-impl Show documentation
Show all versions of component-runtime-impl Show documentation
Module responsible to understand the API model and make it runnable.
This MUST be considered as an internal of the stack.
The newest version!
/**
* Copyright (C) 2006-2023 Talend Inc. - www.talend.com
*
* 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 org.talend.sdk.component.runtime.input;
import static java.lang.Thread.sleep;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.talend.sdk.component.runtime.input.Streaming.RetryStrategy;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.PostConstruct;
import org.talend.sdk.component.api.configuration.Option;
import org.talend.sdk.component.runtime.input.Streaming.RetryConfiguration;
import org.talend.sdk.component.runtime.input.Streaming.StopStrategy;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class StreamingInputImpl extends InputImpl {
private RetryConfiguration retryConfiguration;
private transient Thread shutdownHook;
private final AtomicBoolean running = new AtomicBoolean();
private transient Semaphore semaphore;
private StopStrategy stopStrategy;
private transient long readRecords = 0L;
public StreamingInputImpl(final String rootName, final String name, final String plugin,
final Serializable instance, final RetryConfiguration retryConfiguration, final StopStrategy stopStrategy) {
super(rootName, name, plugin, instance);
shutdownHook = new Thread(() -> running.compareAndSet(true, false),
getClass().getName() + "_" + rootName() + "-" + name() + "_" + hashCode());
this.retryConfiguration = retryConfiguration;
this.stopStrategy = stopStrategy;
log.debug("[StreamingInputImpl] Created with retryStrategy: {}, stopStrategy: {}.", this.retryConfiguration,
this.stopStrategy);
}
protected StreamingInputImpl() {
// no-op
}
@Override
protected Object readNext() {
if (!running.get()) {
return null;
}
if (stopStrategy.isActive() && stopStrategy.shouldStop(readRecords)) {
log.debug("[readNext] stopStrategy condition validated.");
return null;
}
try {
semaphore.acquire();
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
return null;
}
try {
final RetryStrategy strategy = retryConfiguration.getStrategy();
int retries = retryConfiguration.getMaxRetries();
while (running.get() && retries > 0) {
Object next = null;
if (stopStrategy.isActive() && stopStrategy.getMaxActiveTime() > -1) {
// Some connectors do not block input and return null (rabbitmq for instance). Thus, the future
// timeout is never reached and retryStrategy is run then. So, need to check timeout in the loop.
if (stopStrategy.shouldStop(readRecords)) {
log.debug("[readNext] shouldStop now! Duration {}ms",
System.currentTimeMillis() - stopStrategy.getStartedAtTime());
return null;
}
final ExecutorService executor = Executors.newSingleThreadExecutor();
final Future
© 2015 - 2025 Weber Informatics LLC | Privacy Policy