/**
* Copyright 2016 StreamSets Inc.
* <p>
* Licensed under the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.streamsets.pipeline.lib.httpsource;
import com.google.common.annotations.VisibleForTesting;
import com.streamsets.pipeline.api.ProtoSource;
import com.streamsets.pipeline.api.Stage;
import com.streamsets.pipeline.api.base.BaseStage;
import com.streamsets.pipeline.lib.http.HttpConfigs;
import com.streamsets.pipeline.lib.http.HttpReceiver;
import com.streamsets.pipeline.lib.http.HttpReceiverServer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public abstract class AbstractHttpServerProtoSource<R extends HttpReceiver, C extends ProtoSource.Context>
extends BaseStage<C> {
private final HttpConfigs httpConfigs;
private R receiver;
private HttpReceiverServer server;
private BlockingQueue<Exception> errorQueue;
private List<Exception> errorList;
public AbstractHttpServerProtoSource(HttpConfigs httpConfigs, R receiver) {
this.httpConfigs = httpConfigs;
this.receiver = receiver;
}
protected R getReceiver() {
return receiver;
}
public HttpConfigs getHttpConfigs() {
return httpConfigs;
}
@Override
protected List<Stage.ConfigIssue> init() {
List<ConfigIssue> issues = super.init();
if (issues.isEmpty()) {
errorQueue = new ArrayBlockingQueue<>(100);
errorList = new ArrayList<>(100);
server = new HttpReceiverServer(httpConfigs, receiver, getErrorQueue());
issues.addAll(server.init(getContext()));
}
return issues;
}
@Override
public void destroy() {
if (server != null) {
server.destroy();
}
super.destroy();
}
@VisibleForTesting
BlockingQueue<Exception> getErrorQueue() {
return errorQueue;
}
protected void dispatchHttpReceiverErrors(long intervalMillis) {
if (intervalMillis > 0) {
try {
Thread.sleep(intervalMillis);
} catch (InterruptedException ex) {
}
}
// report errors reported by the HttpReceiverServer
errorList.clear();
getErrorQueue().drainTo(errorList);
for (Exception exception : errorList) {
getContext().reportError(exception);
}
}
}