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

com.king.platform.net.http.netty.sse.DelegatingAsyncSseClientCallback Maven / Gradle / Ivy

There is a newer version: 3.0.26
Show newest version
package com.king.platform.net.http.netty.sse;


import com.king.platform.net.http.EventCallback;
import com.king.platform.net.http.SseClient;
import com.king.platform.net.http.SseClientCallback;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;

public class DelegatingAsyncSseClientCallback implements SseClientCallback {
	private final CopyOnWriteArrayList sseClientCallbacks = new CopyOnWriteArrayList<>();
	private final CopyOnWriteArrayList eventCallbacks = new CopyOnWriteArrayList<>();
	private final CopyOnWriteArrayList disconnectCallbacks = new CopyOnWriteArrayList<>();
	private final CopyOnWriteArrayList connectCallbacks = new CopyOnWriteArrayList<>();
	private final CopyOnWriteArrayList errorCallbacks = new CopyOnWriteArrayList<>();

	private final ConcurrentHashMap> eventCallbackMap = new ConcurrentHashMap<>();

	private final Executor httpClientCallbackExecutor;


	DelegatingAsyncSseClientCallback(Executor httpClientCallbackExecutor) {
		this.httpClientCallbackExecutor = httpClientCallbackExecutor;
	}

	@Override
	public void onConnect() {
		httpClientCallbackExecutor.execute(() -> {
            for (SseClientCallback callback : sseClientCallbacks) {
                callback.onConnect();
            }

            for (SseClient.ConnectCallback callback : connectCallbacks) {
                callback.onConnect();
            }
        });

	}

	@Override
	public void onDisconnect() {
		httpClientCallbackExecutor.execute(() -> {
            for (SseClientCallback callback : sseClientCallbacks) {
                callback.onDisconnect();
            }

            for (SseClient.DisconnectCallback callback : disconnectCallbacks) {
                callback.onDisconnect();
            }
        });

	}

	@Override
	public void onError(final Throwable throwable) {
		httpClientCallbackExecutor.execute(() -> {
            for (SseClientCallback callback : sseClientCallbacks) {
                callback.onError(throwable);
            }

            for (SseClient.ErrorCallback errorCallback : errorCallbacks) {
                errorCallback.onError(throwable);
            }
        });

	}

	@Override
	public void onEvent(final String lastSentId, final String event, final String data) {
		httpClientCallbackExecutor.execute(() -> {
            for (SseClientCallback callback : sseClientCallbacks) {
                callback.onEvent(lastSentId, event, data);
            }

            invokeCallbacks(lastSentId, event, data, eventCallbacks);

            if (event != null) {
                List eventCallbacks = eventCallbackMap.get(event);
                invokeCallbacks(lastSentId, event, data, eventCallbacks);
            }
        });
	}

	private void invokeCallbacks(String lastSentId, String event, String data, List eventCallbacks) {
		if (eventCallbacks != null) {
			for (EventCallback eventCallback : eventCallbacks) {
				eventCallback.onEvent(lastSentId, event, data);
			}
		}
	}

	void addSseClientCallbacks(SseClientCallback callback) {
		sseClientCallbacks.add(callback);
	}

	void addEventCallback(String eventName, EventCallback callback) {
		List eventCallbacks = eventCallbackMap.get(eventName);
		if (eventCallbacks == null) {
			eventCallbacks = new CopyOnWriteArrayList<>();
			List prevValue = eventCallbackMap.putIfAbsent(eventName, eventCallbacks);
			if (prevValue != null) {
				eventCallbacks = prevValue;
			}
		}
		eventCallbacks.add(callback);
	}

	void addEventCallback(EventCallback callback) {
		eventCallbacks.add(callback);
	}

	void addCloseCallback(SseClient.DisconnectCallback disconnectCallback) {
		disconnectCallbacks.add(disconnectCallback);
	}

	void addConnectCallback(SseClient.ConnectCallback connectCallback) {
		connectCallbacks.add(connectCallback);
	}

	public void addErrorCallback(SseClient.ErrorCallback errorCallback) {
		errorCallbacks.add(errorCallback);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy