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

io.reactivex.mantis.remote.observable.ConnectToObservable Maven / Gradle / Ivy

There is a newer version: 3.1.4
Show newest version
/*
 * Copyright 2019 Netflix, Inc.
 *
 * 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 io.reactivex.mantis.remote.observable;

import io.mantisrx.common.codec.Decoder;
import java.util.HashMap;
import java.util.Map;
import rx.functions.Action0;
import rx.functions.Action2;
import rx.subjects.PublishSubject;


public class ConnectToObservable extends ConnectToConfig {

    private Decoder decoder;
    private Action2 deocdingErrorHandler;

    ConnectToObservable(Builder builder) {
        super(builder.host, builder.port,
                builder.name,
                builder.subscribeParameters,
                builder.subscribeRetryAttempts,
                builder.suppressDecodingErrors,
                builder.connectionDisconnectCallback,
                builder.closeTrigger);
        this.decoder = builder.decoder;
        this.deocdingErrorHandler = builder.deocdingErrorHandler;
    }

    public Decoder getDecoder() {
        return decoder;
    }

    public Action2 getDeocdingErrorHandler() {
        return deocdingErrorHandler;
    }

    public static class Builder {

        private String host;
        private int port;
        private String name;
        private Decoder decoder;
        private Map subscribeParameters = new HashMap<>();
        private int subscribeRetryAttempts = 3;
        private Action2 deocdingErrorHandler = new Action2() {
            @Override
            public void call(T t1, Throwable t2) {
                t2.printStackTrace();
            }
        };
        private boolean suppressDecodingErrors = false;
        private Action0 connectionDisconnectCallback = new Action0() {
            @Override
            public void call() {}
        };
        private PublishSubject closeTrigger = PublishSubject.create();

        public Builder() {}

        public Builder(Builder config) {
            this.host = config.host;
            this.port = config.port;
            this.name = config.name;
            this.decoder = config.decoder;
            this.subscribeParameters.putAll(config.subscribeParameters);
            this.subscribeRetryAttempts = config.subscribeRetryAttempts;
            this.deocdingErrorHandler = config.deocdingErrorHandler;
            this.suppressDecodingErrors = config.suppressDecodingErrors;
        }

        public Builder host(String host) {
            this.host = host;
            return this;
        }

        public Builder port(int port) {
            this.port = port;
            return this;
        }

        public Builder closeTrigger(PublishSubject closeTrigger) {
            this.closeTrigger = closeTrigger;
            return this;
        }

        public Builder connectionDisconnectCallback(Action0 connectionDisconnectCallback) {
            this.connectionDisconnectCallback = connectionDisconnectCallback;
            return this;
        }

        public Builder deocdingErrorHandler(Action2 handler, boolean suppressDecodingErrors) {
            this.deocdingErrorHandler = handler;
            this.suppressDecodingErrors = suppressDecodingErrors;
            return this;
        }

        public Builder name(String name) {
            this.name = name;
            this.subscribeParameters.put("groupId", name); //used with modern server for routing
            return this;
        }

        public Builder slotId(String slotId) {
            this.subscribeParameters.put("slotId", slotId);
            return this;
        }

        public Builder decoder(Decoder decoder) {
            this.decoder = decoder;
            return this;
        }

        public Builder subscribeParameters(Map subscribeParameters) {
            this.subscribeParameters.putAll(subscribeParameters);
            return this;
        }

        public Builder subscribeAttempts(int subscribeAttempts) {
            this.subscribeRetryAttempts = subscribeAttempts;
            return this;
        }

        public ConnectToObservable build() {
            return new ConnectToObservable(this);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy