All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nstream.adapter.nats.NatsIngestingAgent Maven / Gradle / Ivy

The newest version!
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// 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 nstream.adapter.nats;

import io.nats.client.Connection;
import io.nats.client.Message;
import io.nats.client.Subscription;
import nstream.adapter.common.ext.NatsIngressSettings;
import nstream.adapter.common.ingress.IngestorMetricsAgent;
import nstream.adapter.common.provision.ProvisionLoader;
import nstream.adapter.common.schedule.DeferrableException;
import swim.concurrent.TaskRef;
import swim.structure.Value;

public abstract class NatsIngestingAgent
    extends IngestorMetricsAgent {

  protected volatile TaskRef longPollTask;
  protected volatile Connection natsConnection;
  // protected volatile JetStream jetStream;
  // protected volatile JetStreamSubscription sub;
  // protected ConsumerConfiguration cc;
  // protected PullSubscribeOptions pso;
  // protected volatile String natsStream;
  protected volatile Subscription sub;
  protected volatile String natsSubject;

  public NatsIngestingAgent() {
  }

  protected TaskRef longPollTask() {
    return this.longPollTask;
  }

  // protected void assignConsumerConfiguration(ConsumerConfiguration cc) {
  //   this.cc = cc;
  // }

  // protected void assignPullSubscribeOptions(PullSubscribeOptions pso) {
  //   this.pso = pso;
  // }

  @Override
  protected NatsIngressSettings parseIngressSettings(Value prop) {
    final NatsIngressSettings settings = NatsIngressSettings.form().cast(prop);
    return settings == null ? NatsIngressSettings.defaultSettings() : settings;
  }

  @Override
  protected void stageReception() {
    try {
      loadSettings(NatsAdapterUtils.ingressSettingsFromProp(getProp("natsIngressConf")));
      this.natsConnection = ProvisionLoader
          .getProvision(this.ingressSettings.getConnectionProvisionName())
          .value();
      // this.jetStream = this.natsConnection.jetStream();
      // this.natsStream = this.ingressSettings.getNatsIngressStreamName();
      this.natsSubject = this.ingressSettings.getNatsIngressSubjectName();
      this.sub = this.natsConnection.subscribe(this.natsSubject);
      // this.sub = this.jetStream.subscribe(this.natsSubject, PullSubscribeOptions.DEFAULT_PULL_OPTS); // TODO: utilize this.natsStream
      this.longPollTask = prepareLoop(this::longPollTask, this::longPollLoop);
      this.longPollTask.cue();
    } catch (Exception e) {
      throw new RuntimeException(nodeUri() + ": Failed to stage reception", e);
    }
  }

  protected void longPollLoop() throws DeferrableException {
    // final Iterator iter = this.sub.iterate(100, Duration.ofSeconds(1));
    // while (iter.hasNext()) {
    //   ingestOrCancel(iter.next());
    //   // FIXME: better acking
    // }
    try {
      final Message msg = this.sub.nextMessage(1000L);
      if (msg != null) {
        ingestOrContinue(msg);
      }
    } catch (InterruptedException e) {
      throw new DeferrableException(nodeUri() + ": interrupted", e);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy