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

com.github.linyuzai.connection.loadbalance.sse.reactive.ReactiveSseConnectionSubscriber Maven / Gradle / Ivy

The newest version!
package com.github.linyuzai.connection.loadbalance.sse.reactive;

import com.github.linyuzai.connection.loadbalance.core.concept.ConnectionLoadBalanceConcept;
import com.github.linyuzai.connection.loadbalance.sse.concept.SseConnectionSubscriber;
import com.github.linyuzai.connection.loadbalance.sse.concept.SseIdGenerator;
import com.github.linyuzai.connection.loadbalance.sse.concept.SubscriberSseCreation;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.Disposable;

import java.net.URI;
import java.util.function.Consumer;

@Getter
@RequiredArgsConstructor
public class ReactiveSseConnectionSubscriber extends SseConnectionSubscriber {

    private final SseIdGenerator sseIdGenerator;

    private final ReactiveSseClientFactory sseClientFactory;

    @Override
    public void doSubscribe(URI uri, ConnectionLoadBalanceConcept concept, Consumer onSuccess, Consumer onError, Runnable onComplete) {
        WebClient client = sseClientFactory.create();
        ReactiveSubscriberSseConnection connection = new ReactiveSubscriberSseConnection();
        Object id = sseIdGenerator.generateId(null);
        connection.setCreation(new SubscriberSseCreation(id, getEndpoint()));
        Disposable disposable = client.get()
                .uri(uri)
                .retrieve()
                .bodyToFlux(String.class)
                .doOnSubscribe(subscription -> onSuccess.accept(connection))
                .subscribe(message -> concept.onMessage(connection, message), e -> {
                    connection.closeSubscriber();
                    onError.accept(e);
                }, onComplete);
        connection.setDisposable(disposable);
    }

    @Override
    public String getType() {
        return "reactive";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy