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

com.yhzdys.myosotis.event.multicast.actuator.NamespaceEventActuator Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
package com.yhzdys.myosotis.event.multicast.actuator;

import com.yhzdys.myosotis.event.multicast.EventExecutor;
import com.yhzdys.myosotis.executor.MulticasterExecutor;
import com.yhzdys.myosotis.misc.LoggerFactory;

import java.util.LinkedList;

public final class NamespaceEventActuator implements Actuator {

    private final MulticasterExecutor mExecutor;
    private final Runnable runner;
    private final LinkedList executors = new LinkedList<>();
    private boolean running;

    public NamespaceEventActuator(MulticasterExecutor mExecutor) {
        this.mExecutor = mExecutor;
        this.runner = () -> {
            for (; ; ) {
                EventExecutor currentExecutor;
                synchronized (executors) {
                    currentExecutor = executors.poll();
                    if (currentExecutor == null) {
                        running = false;
                        return;
                    }
                }
                try {
                    currentExecutor.execute();
                } catch (Throwable t) {
                    LoggerFactory.getLogger().error(t.getMessage(), t);
                }
            }
        };
    }

    @Override
    public void actuate(EventExecutor executor) {
        synchronized (executors) {
            int index = executors.indexOf(executor);
            if (index < 0) {
                executors.add(executor);
            } else {
                executors.set(index, executor);
            }
            if (!running) {
                running = true;
                this.mExecutor.execute(runner);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy