Search in sources :

Example 1 with Listener

use of org.opennms.netmgt.telemetry.listeners.api.Listener in project opennms by OpenNMS.

the class Telemetryd method destroy.

@Override
public synchronized void destroy() {
    LOG.info("{} is stopping.", NAME);
    // Stop the listeners
    for (Listener listener : listeners) {
        try {
            LOG.info("Stopping {} listener.", listener.getName());
            listener.stop();
        } catch (InterruptedException e) {
            LOG.warn("Error while stopping listener.", e);
        }
    }
    listeners.clear();
    // Stop the dispatchers
    for (AsyncDispatcher<?> dispatcher : dispatchers) {
        try {
            LOG.info("Closing dispatcher.", dispatcher);
            dispatcher.close();
        } catch (Exception e) {
            LOG.warn("Error while closing dispatcher.", e);
        }
    }
    dispatchers.clear();
    // Stop the consumers
    for (TelemetryMessageConsumer consumer : consumers) {
        try {
            LOG.info("Stopping consumer for {} protocol.", consumer.getProtocol().getName());
            messageConsumerManager.unregisterConsumer(consumer);
        } catch (Exception e) {
            LOG.error("Error while stopping consumer.", e);
        }
    }
    consumers.clear();
    LOG.info("{} is stopped.", NAME);
}
Also used : Listener(org.opennms.netmgt.telemetry.listeners.api.Listener) EventListener(org.opennms.netmgt.events.api.annotations.EventListener)

Example 2 with Listener

use of org.opennms.netmgt.telemetry.listeners.api.Listener in project opennms by OpenNMS.

the class Telemetryd method start.

@Override
public synchronized void start() throws Exception {
    if (consumers.size() > 0) {
        throw new IllegalStateException(NAME + " is already started.");
    }
    LOG.info("{} is starting.", NAME);
    final TelemetrydConfiguration config = telemetrydConfigDao.getContainer().getObject();
    final AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
    for (Protocol protocol : config.getProtocols()) {
        if (!protocol.getEnabled()) {
            LOG.debug("Skipping disabled protocol: {}", protocol.getName());
            continue;
        }
        LOG.debug("Setting up protocol: {}", protocol.getName());
        // Create a Sink module using the protocol definition.
        // This allows for protocol to each have their respective queues and thread
        // related settings to help limit the impact of one protocol on another.
        final TelemetrySinkModule sinkModule = new TelemetrySinkModule(protocol);
        beanFactory.autowireBean(sinkModule);
        beanFactory.initializeBean(sinkModule, "sinkModule");
        // Create the consumer, but don't start it yet
        final TelemetryMessageConsumer consumer = new TelemetryMessageConsumer(protocol, sinkModule);
        beanFactory.autowireBean(consumer);
        beanFactory.initializeBean(consumer, "consumer");
        consumers.add(consumer);
        // Build the dispatcher, and all of
        final AsyncDispatcher<TelemetryMessage> dispatcher = messageDispatcherFactory.createAsyncDispatcher(sinkModule);
        dispatchers.add(dispatcher);
        for (org.opennms.netmgt.telemetry.config.model.Listener listenerDef : protocol.getListeners()) {
            listeners.add(ListenerFactory.buildListener(listenerDef, dispatcher));
        }
    }
    // Start the consumers
    for (TelemetryMessageConsumer consumer : consumers) {
        LOG.info("Starting consumer for {} protocol.", consumer.getProtocol().getName());
        messageConsumerManager.registerConsumer(consumer);
    }
    // Start the listeners
    for (Listener listener : listeners) {
        LOG.info("Starting {} listener.", listener.getName());
        listener.start();
    }
    LOG.info("{} is started.", NAME);
}
Also used : TelemetrySinkModule(org.opennms.netmgt.telemetry.ipc.TelemetrySinkModule) Listener(org.opennms.netmgt.telemetry.listeners.api.Listener) EventListener(org.opennms.netmgt.events.api.annotations.EventListener) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) TelemetryMessage(org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage) TelemetrydConfiguration(org.opennms.netmgt.telemetry.config.model.TelemetrydConfiguration) Protocol(org.opennms.netmgt.telemetry.config.model.Protocol)

Example 3 with Listener

use of org.opennms.netmgt.telemetry.listeners.api.Listener in project opennms by OpenNMS.

the class ListenerManager method deleted.

@Override
public void deleted(String pid) {
    final Listener listener = listenersByPid.remove(pid);
    if (listener != null) {
        LOG.info("Stopping listener for pid: {}", pid);
        try {
            listener.stop();
        } catch (InterruptedException e) {
            LOG.error("Error occured while stopping listener for pid: {}", pid, e);
        }
    }
    final AsyncDispatcher<TelemetryMessage> dispatcher = dispatchersByPid.remove(pid);
    if (dispatcher != null) {
        LOG.info("Closing dispatcher for pid: {}", pid);
        try {
            dispatcher.close();
        } catch (Exception e) {
            LOG.error("Error occured while closing dispatcher for pid: {}", pid, e);
        }
    }
}
Also used : Listener(org.opennms.netmgt.telemetry.listeners.api.Listener) TelemetryMessage(org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage)

Example 4 with Listener

use of org.opennms.netmgt.telemetry.listeners.api.Listener in project opennms by OpenNMS.

the class ListenerFactory method buildListener.

public static Listener buildListener(ListenerDefinition listenerDef, AsyncDispatcher<TelemetryMessage> dispatcher) throws Exception {
    // Instantiate the associated class
    final Object listenerInstance;
    try {
        final Class<?> clazz = Class.forName(listenerDef.getClassName());
        final Constructor<?> ctor = clazz.getConstructor();
        listenerInstance = ctor.newInstance();
    } catch (Exception e) {
        throw new RuntimeException(String.format("Failed to instantiate listener with class name '%s'.", listenerDef.getClassName(), e));
    }
    // Cast
    if (!(listenerInstance instanceof Listener)) {
        throw new IllegalArgumentException(String.format("%s must implement %s", listenerDef.getClassName(), Listener.class.getCanonicalName()));
    }
    final Listener listener = (Listener) listenerInstance;
    // Apply the parameters
    final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(listener);
    wrapper.setPropertyValues(listenerDef.getParameterMap());
    // Update the name
    // The one given in the definition wins over any that may be set by the parameters
    listener.setName(listenerDef.getName());
    // Use the given dispatcher
    listener.setDispatcher(dispatcher);
    return listener;
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) Listener(org.opennms.netmgt.telemetry.listeners.api.Listener)

Example 5 with Listener

use of org.opennms.netmgt.telemetry.listeners.api.Listener in project opennms by OpenNMS.

the class ListenerManager method updated.

@Override
public void updated(String pid, Dictionary<String, ?> properties) {
    final Listener existingListener = listenersByPid.get(pid);
    if (existingListener != null) {
        LOG.info("Updating existing listener/dispatcher for pid: {}", pid);
        deleted(pid);
    } else {
        LOG.info("Creating new listener/dispatcher for pid: {}", pid);
    }
    // Convert the dictionary to a map
    final Map<String, String> parameters = MapUtils.fromDict(properties);
    // Build the protocol and listener definitions
    final MapBasedProtocolDef protocolDef = new MapBasedProtocolDef(parameters);
    final MapBasedListenerDef listenerDef = new MapBasedListenerDef(parameters);
    final TelemetrySinkModule sinkModule = new TelemetrySinkModule(protocolDef);
    sinkModule.setDistPollerDao(distPollerDao);
    final AsyncDispatcher<TelemetryMessage> dispatcher = messageDispatcherFactory.createAsyncDispatcher(sinkModule);
    try {
        final Listener listener = ListenerFactory.buildListener(listenerDef, dispatcher);
        listener.start();
        listenersByPid.put(pid, listener);
        dispatchersByPid.put(pid, dispatcher);
    } catch (Exception e) {
        LOG.error("Failed to build listener.", e);
        try {
            dispatcher.close();
        } catch (Exception ee) {
            LOG.error("Failed to close dispatcher.", e);
        }
    }
    LOG.info("Successfully started listener/dispatcher for pid: {}", pid);
}
Also used : TelemetrySinkModule(org.opennms.netmgt.telemetry.ipc.TelemetrySinkModule) Listener(org.opennms.netmgt.telemetry.listeners.api.Listener) TelemetryMessage(org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage)

Aggregations

Listener (org.opennms.netmgt.telemetry.listeners.api.Listener)5 TelemetryMessage (org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage)3 EventListener (org.opennms.netmgt.events.api.annotations.EventListener)2 TelemetrySinkModule (org.opennms.netmgt.telemetry.ipc.TelemetrySinkModule)2 Protocol (org.opennms.netmgt.telemetry.config.model.Protocol)1 TelemetrydConfiguration (org.opennms.netmgt.telemetry.config.model.TelemetrydConfiguration)1 BeanWrapper (org.springframework.beans.BeanWrapper)1 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)1