Search in sources :

Example 1 with Message

use of org.opennms.core.ipc.sink.api.Message in project opennms by OpenNMS.

the class AbstractMessageConsumerManager method unregisterConsumer.

@SuppressWarnings("unchecked")
@Override
public synchronized <S extends Message, T extends Message> void unregisterConsumer(MessageConsumer<S, T> consumer) throws Exception {
    if (consumer == null) {
        return;
    }
    try (MDCCloseable mdc = Logging.withPrefixCloseable(MessageConsumerManager.LOG_PREFIX)) {
        LOG.info("Unregistering consumer: {}", consumer);
        final SinkModule<?, Message> module = (SinkModule<?, Message>) consumer.getModule();
        consumersByModule.remove(module, (MessageConsumer<?, Message>) consumer);
        if (consumersByModule.get(module).size() < 1) {
            waitForStartup.thenRunAsync(new Runnable() {

                @Override
                public void run() {
                    try {
                        LOG.info("Stopping consumption of messages for module: {}", module);
                        stopConsumingForModule(module);
                    } catch (Exception e) {
                        LOG.error("Unexpected exception while trying to stop consumer for module: {}", module, e);
                    }
                }
            });
        }
    }
}
Also used : SinkModule(org.opennms.core.ipc.sink.api.SinkModule) Message(org.opennms.core.ipc.sink.api.Message) MDCCloseable(org.opennms.core.logging.Logging.MDCCloseable)

Example 2 with Message

use of org.opennms.core.ipc.sink.api.Message in project opennms by OpenNMS.

the class AbstractMessageConsumerManager method registerConsumer.

@SuppressWarnings("unchecked")
@Override
public synchronized <S extends Message, T extends Message> void registerConsumer(MessageConsumer<S, T> consumer) throws Exception {
    if (consumer == null) {
        return;
    }
    try (MDCCloseable mdc = Logging.withPrefixCloseable(MessageConsumerManager.LOG_PREFIX)) {
        LOG.info("Registering consumer: {}", consumer);
        final SinkModule<?, Message> module = (SinkModule<?, Message>) consumer.getModule();
        final int numConsumersBefore = consumersByModule.get(module).size();
        consumersByModule.put(module, (MessageConsumer<?, Message>) consumer);
        if (numConsumersBefore < 1) {
            waitForStartup.thenRunAsync(new Runnable() {

                @Override
                public void run() {
                    try {
                        LOG.info("Starting to consume messages for module: {}", module);
                        startConsumingForModule(module);
                    } catch (Exception e) {
                        LOG.error("Unexpected exception while trying to start consumer for module: {}", module, e);
                    }
                }
            });
        }
    }
}
Also used : SinkModule(org.opennms.core.ipc.sink.api.SinkModule) Message(org.opennms.core.ipc.sink.api.Message) MDCCloseable(org.opennms.core.logging.Logging.MDCCloseable)

Example 3 with Message

use of org.opennms.core.ipc.sink.api.Message in project opennms by OpenNMS.

the class TrapSinkModuleTest method testEqualsAndHashCode.

@Test
public void testEqualsAndHashCode() throws Exception {
    SinkModule<Message, Message> mockModule = Mockito.mock(SinkModule.class);
    Mockito.when(mockModule.getId()).thenReturn("id");
    OnmsDistPoller distPollerMock = Mockito.mock(OnmsDistPoller.class);
    TrapdConfig config = new TrapdConfigBean();
    final TrapSinkModule module = new TrapSinkModule(config, distPollerMock);
    Assert.assertEquals(module, module);
    Assert.assertEquals(module.hashCode(), module.hashCode());
    final TrapSinkModule other = new TrapSinkModule(config, distPollerMock);
    Assert.assertEquals(module, other);
    Assert.assertEquals(module.hashCode(), other.hashCode());
    Assert.assertNotEquals(module, mockModule);
    Assert.assertNotEquals(module.hashCode(), mockModule.hashCode());
}
Also used : Message(org.opennms.core.ipc.sink.api.Message) OnmsDistPoller(org.opennms.netmgt.model.OnmsDistPoller) TrapdConfig(org.opennms.netmgt.config.TrapdConfig) Test(org.junit.Test)

Example 4 with Message

use of org.opennms.core.ipc.sink.api.Message in project opennms by OpenNMS.

the class CamelSinkServerProcessor method process.

@Override
public void process(Exchange exchange) {
    final byte[] messageBytes = exchange.getIn().getBody(byte[].class);
    final Message message = module.unmarshal(messageBytes);
    consumerManager.dispatch(module, message);
}
Also used : Message(org.opennms.core.ipc.sink.api.Message)

Aggregations

Message (org.opennms.core.ipc.sink.api.Message)4 SinkModule (org.opennms.core.ipc.sink.api.SinkModule)2 MDCCloseable (org.opennms.core.logging.Logging.MDCCloseable)2 Test (org.junit.Test)1 TrapdConfig (org.opennms.netmgt.config.TrapdConfig)1 OnmsDistPoller (org.opennms.netmgt.model.OnmsDistPoller)1