Search in sources :

Example 1 with WrappedMessageConsumer

use of org.apache.nifi.processors.standard.util.WrappedMessageConsumer in project nifi by apache.

the class GetJMSQueue method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLogger();
    WrappedMessageConsumer wrappedConsumer = consumerQueue.poll();
    if (wrappedConsumer == null) {
        try {
            wrappedConsumer = JmsFactory.createQueueMessageConsumer(context);
        } catch (JMSException e) {
            logger.error("Failed to connect to JMS Server due to {}", e);
            context.yield();
            return;
        }
    }
    try {
        super.consume(context, session, wrappedConsumer);
    } finally {
        if (!wrappedConsumer.isClosed()) {
            consumerQueue.offer(wrappedConsumer);
        }
    }
}
Also used : WrappedMessageConsumer(org.apache.nifi.processors.standard.util.WrappedMessageConsumer) JMSException(javax.jms.JMSException) ComponentLog(org.apache.nifi.logging.ComponentLog)

Example 2 with WrappedMessageConsumer

use of org.apache.nifi.processors.standard.util.WrappedMessageConsumer in project nifi by apache.

the class GetJMSQueue method cleanupResources.

@OnStopped
public void cleanupResources() {
    WrappedMessageConsumer wrappedConsumer = consumerQueue.poll();
    while (wrappedConsumer != null) {
        wrappedConsumer.close(getLogger());
        wrappedConsumer = consumerQueue.poll();
    }
}
Also used : WrappedMessageConsumer(org.apache.nifi.processors.standard.util.WrappedMessageConsumer) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped)

Example 3 with WrappedMessageConsumer

use of org.apache.nifi.processors.standard.util.WrappedMessageConsumer in project nifi by apache.

the class GetJMSTopic method onStopped.

@OnStopped
public void onStopped() {
    final WrappedMessageConsumer consumer = this.wrappedConsumer;
    if (consumer != null) {
        consumer.close(getLogger());
        this.wrappedConsumer = null;
    }
}
Also used : WrappedMessageConsumer(org.apache.nifi.processors.standard.util.WrappedMessageConsumer) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped)

Example 4 with WrappedMessageConsumer

use of org.apache.nifi.processors.standard.util.WrappedMessageConsumer in project nifi by apache.

the class GetJMSTopic method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLogger();
    WrappedMessageConsumer consumer = this.wrappedConsumer;
    if (consumer == null || consumer.isClosed()) {
        try {
            Properties props = null;
            try {
                props = getSubscriptionPropertiesFromFile();
            } catch (IOException ignore) {
            }
            if (props == null) {
                props = getSubscriptionPropertiesFromContext(context);
            }
            String subscriptionName = props.getProperty(SUBSCRIPTION_NAME_PROPERTY);
            consumer = JmsFactory.createTopicMessageConsumer(context, subscriptionName);
            this.wrappedConsumer = consumer;
        } catch (final JMSException e) {
            logger.error("Failed to connect to JMS Server due to {}", new Object[] { e });
            context.yield();
            return;
        }
    }
    super.consume(context, session, consumer);
}
Also used : WrappedMessageConsumer(org.apache.nifi.processors.standard.util.WrappedMessageConsumer) JMSException(javax.jms.JMSException) IOException(java.io.IOException) JmsProperties(org.apache.nifi.processors.standard.util.JmsProperties) Properties(java.util.Properties) ComponentLog(org.apache.nifi.logging.ComponentLog)

Example 5 with WrappedMessageConsumer

use of org.apache.nifi.processors.standard.util.WrappedMessageConsumer in project nifi by apache.

the class JmsConsumer method consume.

public void consume(final ProcessContext context, final ProcessSession session, final WrappedMessageConsumer wrappedConsumer) throws ProcessException {
    final ComponentLog logger = getLogger();
    final MessageConsumer consumer = wrappedConsumer.getConsumer();
    final boolean clientAcknowledge = context.getProperty(ACKNOWLEDGEMENT_MODE).getValue().equalsIgnoreCase(ACK_MODE_CLIENT);
    final long timeout = context.getProperty(TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS);
    final boolean addAttributes = context.getProperty(JMS_PROPS_TO_ATTRIBUTES).asBoolean();
    final int batchSize = context.getProperty(BATCH_SIZE).asInteger();
    final JmsProcessingSummary processingSummary = new JmsProcessingSummary();
    final StopWatch stopWatch = new StopWatch(true);
    for (int i = 0; i < batchSize; i++) {
        final Message message;
        try {
            // all messages that are immediately available.
            if (processingSummary.getMessagesReceived() == 0) {
                message = consumer.receive(timeout);
            } else {
                message = consumer.receiveNoWait();
            }
        } catch (final JMSException e) {
            logger.error("Failed to receive JMS Message due to {}", e);
            wrappedConsumer.close(logger);
            break;
        }
        if (message == null) {
            // if no messages, we're done
            break;
        }
        try {
            processingSummary.add(map2FlowFile(context, session, message, addAttributes, logger));
        } catch (Exception e) {
            logger.error("Failed to receive JMS Message due to {}", e);
            wrappedConsumer.close(logger);
            break;
        }
    }
    if (processingSummary.getFlowFilesCreated() == 0) {
        context.yield();
        return;
    }
    session.commit();
    stopWatch.stop();
    if (processingSummary.getFlowFilesCreated() > 0) {
        final float secs = (stopWatch.getDuration(TimeUnit.MILLISECONDS) / 1000F);
        float messagesPerSec = (processingSummary.getMessagesReceived()) / secs;
        final String dataRate = stopWatch.calculateDataRate(processingSummary.getBytesReceived());
        logger.info("Received {} messages in {} milliseconds, at a rate of {} messages/sec or {}", new Object[] { processingSummary.getMessagesReceived(), stopWatch.getDuration(TimeUnit.MILLISECONDS), messagesPerSec, dataRate });
    }
    // if we need to acknowledge the messages, do so now.
    final Message lastMessage = processingSummary.getLastMessageReceived();
    if (clientAcknowledge && lastMessage != null) {
        try {
            // acknowledge all received messages by acknowledging only the last.
            lastMessage.acknowledge();
        } catch (final JMSException e) {
            logger.error("Failed to acknowledge {} JMS Message(s). This may result in duplicate messages. Reason for failure: {}", new Object[] { processingSummary.getMessagesReceived(), e });
        }
    }
}
Also used : WrappedMessageConsumer(org.apache.nifi.processors.standard.util.WrappedMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) JMSException(javax.jms.JMSException) JmsProcessingSummary(org.apache.nifi.processors.standard.util.JmsProcessingSummary) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) JMSException(javax.jms.JMSException) StopWatch(org.apache.nifi.util.StopWatch)

Aggregations

WrappedMessageConsumer (org.apache.nifi.processors.standard.util.WrappedMessageConsumer)5 JMSException (javax.jms.JMSException)3 ComponentLog (org.apache.nifi.logging.ComponentLog)3 IOException (java.io.IOException)2 OnStopped (org.apache.nifi.annotation.lifecycle.OnStopped)2 Properties (java.util.Properties)1 MapMessage (javax.jms.MapMessage)1 Message (javax.jms.Message)1 MessageConsumer (javax.jms.MessageConsumer)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 JmsProcessingSummary (org.apache.nifi.processors.standard.util.JmsProcessingSummary)1 JmsProperties (org.apache.nifi.processors.standard.util.JmsProperties)1 StopWatch (org.apache.nifi.util.StopWatch)1