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);
}
}
}
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();
}
}
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;
}
}
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);
}
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 });
}
}
}
Aggregations