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

com.swirlds.common.metrics.platform.MetricsEventBus Maven / Gradle / Ivy

Go to download

Swirlds is a software platform designed to build fully-distributed applications that harness the power of the cloud without servers. Now you can develop applications with fairness in decision making, speed, trust and reliability, at a fraction of the cost of traditional server-based platforms.

There is a newer version: 0.56.6
Show newest version
/*
 * Copyright (C) 2023-2024 Hedera Hashgraph, LLC
 *
 * 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 com.swirlds.common.metrics.platform;

import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * The {@code MetricsEventBus} is an intermediate solution to organize the communication and decouple components
 * until we have defined a more general, final solution.
 *
 * @param 
 * 		the event-class
 */
public class MetricsEventBus {

    private final Executor executor;
    private final Queue> subscribers = new ConcurrentLinkedQueue<>();

    /**
     * Constructor of {@code MetricsEventBus}
     *
     * @param executor
     * 		An {@link Executor} that is used to notify subscribers
     * @throws NullPointerException in case {@code executor} parameter is {@code null}
     */
    public MetricsEventBus(final Executor executor) {
        this.executor = Objects.requireNonNull(executor, "executor must not be null");
    }

    /**
     * Subscribe a new subscriber
     * 

* The new subscriber will receive the given previous events after subscription. * * @param subscriber * The new {@code subscriber} * @param previousEvents * A {@link Supplier} of previous events. To ensure, that we do not miss events, this will be evaluated * after the subscriber was added. * @return a {@link Runnable} with which the subscriber can be unsubscribed * @throws NullPointerException if any of the following parameters are {@code null}. *

    *
  • {@code subscriber}
  • *
  • {@code previousEvents}
  • *
*/ public Runnable subscribe(final Consumer subscriber, final Supplier> previousEvents) { Objects.requireNonNull(subscriber, "subscriber must not be null"); Objects.requireNonNull(previousEvents, "previousEvents must not be null"); subscribers.add(subscriber); executor.execute(() -> previousEvents.get().forEach(subscriber)); return () -> subscribers.remove(subscriber); } /** * Submit a new event. *

* This method will return immediately. The subscribers using the {@link Executor} of this class * * @param event * The Event that will be sent to all subscribers * @throws NullPointerException in case {@code event} parameter is {@code null} */ public void submit(final T event) { Objects.requireNonNull(event, "event must not be null"); executor.execute(() -> subscribers.forEach(subscriber -> subscriber.accept(event))); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy