Search in sources :

Example 1 with WrappedMessageProducer

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

the class PutJMS method cleanupResources.

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

Example 2 with WrappedMessageProducer

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

the class PutJMS method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLogger();
    final List<FlowFile> flowFiles = session.get(context.getProperty(BATCH_SIZE).asInteger().intValue());
    if (flowFiles.isEmpty()) {
        return;
    }
    WrappedMessageProducer wrappedProducer = producerQueue.poll();
    if (wrappedProducer == null) {
        try {
            wrappedProducer = JmsFactory.createMessageProducer(context, true);
            logger.info("Connected to JMS server {}", new Object[] { context.getProperty(URL).getValue() });
        } catch (final JMSException e) {
            logger.error("Failed to connect to JMS Server due to {}", new Object[] { e });
            session.transfer(flowFiles, REL_FAILURE);
            context.yield();
            return;
        }
    }
    final Session jmsSession = wrappedProducer.getSession();
    final MessageProducer producer = wrappedProducer.getProducer();
    final int maxBufferSize = context.getProperty(MAX_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
    try {
        final Set<FlowFile> successfulFlowFiles = new HashSet<>();
        for (FlowFile flowFile : flowFiles) {
            if (flowFile.getSize() > maxBufferSize) {
                session.transfer(flowFile, REL_FAILURE);
                logger.warn("Routing {} to failure because its size exceeds the configured max", new Object[] { flowFile });
                continue;
            }
            // Read the contents of the FlowFile into a byte array
            final byte[] messageContent = new byte[(int) flowFile.getSize()];
            session.read(flowFile, new InputStreamCallback() {

                @Override
                public void process(final InputStream in) throws IOException {
                    StreamUtils.fillBuffer(in, messageContent, true);
                }
            });
            final Long ttl = context.getProperty(MESSAGE_TTL).asTimePeriod(TimeUnit.MILLISECONDS);
            final String replyToQueueName = context.getProperty(REPLY_TO_QUEUE).evaluateAttributeExpressions(flowFile).getValue();
            final Destination replyToQueue = replyToQueueName == null ? null : JmsFactory.createQueue(context, replyToQueueName);
            int priority = DEFAULT_MESSAGE_PRIORITY;
            try {
                final Integer priorityInt = context.getProperty(MESSAGE_PRIORITY).evaluateAttributeExpressions(flowFile).asInteger();
                priority = priorityInt == null ? priority : priorityInt;
            } catch (final NumberFormatException e) {
                logger.warn("Invalid value for JMS Message Priority: {}; defaulting to priority of {}", new Object[] { context.getProperty(MESSAGE_PRIORITY).evaluateAttributeExpressions(flowFile).getValue(), DEFAULT_MESSAGE_PRIORITY });
            }
            try {
                final Message message = createMessage(jmsSession, context, messageContent, flowFile, replyToQueue, priority);
                if (ttl == null) {
                    producer.setTimeToLive(0L);
                } else {
                    producer.setTimeToLive(ttl);
                }
                producer.send(message);
            } catch (final JMSException e) {
                logger.error("Failed to send {} to JMS Server due to {}", new Object[] { flowFile, e });
                session.transfer(flowFiles, REL_FAILURE);
                context.yield();
                try {
                    jmsSession.rollback();
                } catch (final JMSException jmse) {
                    logger.warn("Unable to roll back JMS Session due to {}", new Object[] { jmse });
                }
                wrappedProducer.close(logger);
                return;
            }
            successfulFlowFiles.add(flowFile);
            session.getProvenanceReporter().send(flowFile, context.getProperty(URL).getValue());
        }
        try {
            jmsSession.commit();
            session.transfer(successfulFlowFiles, REL_SUCCESS);
            final String flowFileDescription = successfulFlowFiles.size() > 10 ? successfulFlowFiles.size() + " FlowFiles" : successfulFlowFiles.toString();
            logger.info("Sent {} to JMS Server and transferred to 'success'", new Object[] { flowFileDescription });
        } catch (JMSException e) {
            logger.error("Failed to commit JMS Session due to {} and transferred to 'failure'", new Object[] { e });
            session.transfer(flowFiles, REL_FAILURE);
            context.yield();
            wrappedProducer.close(logger);
        }
    } finally {
        if (!wrappedProducer.isClosed()) {
            producerQueue.offer(wrappedProducer);
        }
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) Destination(javax.jms.Destination) StreamMessage(javax.jms.StreamMessage) Message(javax.jms.Message) BytesMessage(javax.jms.BytesMessage) InputStream(java.io.InputStream) JMSException(javax.jms.JMSException) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) MessageProducer(javax.jms.MessageProducer) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) Session(javax.jms.Session) ProcessSession(org.apache.nifi.processor.ProcessSession) HashSet(java.util.HashSet)

Example 3 with WrappedMessageProducer

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

the class TestGetJMSQueue method testSendObjectToQueue.

@org.junit.Ignore
public void testSendObjectToQueue() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
    runner.setProperty(JmsProperties.URL, "tcp://localhost:61616");
    runner.setProperty(JmsProperties.DESTINATION_TYPE, JmsProperties.DESTINATION_TYPE_QUEUE);
    runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
    runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);
    WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(runner.getProcessContext(), true);
    final Session jmsSession = wrappedProducer.getSession();
    final MessageProducer producer = wrappedProducer.getProducer();
    // Revision class is used because test just needs any Serializable class in core NiFi
    final ObjectMessage message = jmsSession.createObjectMessage(new Revision(1L, "ID", "COMP_ID"));
    producer.send(message);
    jmsSession.commit();
    producer.close();
    jmsSession.close();
}
Also used : Revision(org.apache.nifi.web.Revision) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) TestRunner(org.apache.nifi.util.TestRunner) ObjectMessage(javax.jms.ObjectMessage) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 4 with WrappedMessageProducer

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

the class TestGetJMSQueue method testSendBytesToQueue.

@Test
public void testSendBytesToQueue() throws Exception {
    PutJMS putJms = new PutJMS();
    TestRunner putRunner = TestRunners.newTestRunner(putJms);
    putRunner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
    putRunner.setProperty(JmsProperties.URL, "vm://localhost?broker.persistent=false");
    putRunner.setProperty(JmsProperties.DESTINATION_TYPE, JmsProperties.DESTINATION_TYPE_QUEUE);
    putRunner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
    putRunner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);
    WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(putRunner.getProcessContext(), true);
    final Session jmsSession = wrappedProducer.getSession();
    final MessageProducer producer = wrappedProducer.getProducer();
    final BytesMessage message = jmsSession.createBytesMessage();
    message.writeBytes("Hello Bytes".getBytes());
    producer.send(message);
    jmsSession.commit();
    GetJMSQueue getJmsQueue = new GetJMSQueue();
    TestRunner runner = TestRunners.newTestRunner(getJmsQueue);
    runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
    runner.setProperty(JmsProperties.URL, "vm://localhost?broker.persistent=false");
    runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
    runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);
    runner.run();
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(new Relationship.Builder().name("success").build());
    assertTrue(flowFiles.size() == 1);
    MockFlowFile successFlowFile = flowFiles.get(0);
    successFlowFile.assertContentEquals("Hello Bytes");
    successFlowFile.assertAttributeEquals("jms.JMSDestination", "queue.testing");
    producer.close();
    jmsSession.close();
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) TestRunner(org.apache.nifi.util.TestRunner) Relationship(org.apache.nifi.processor.Relationship) BytesMessage(javax.jms.BytesMessage) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 5 with WrappedMessageProducer

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

the class TestGetJMSQueue method testSendStreamToQueue.

@Test
public void testSendStreamToQueue() throws Exception {
    PutJMS putJms = new PutJMS();
    TestRunner putRunner = TestRunners.newTestRunner(putJms);
    putRunner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
    putRunner.setProperty(JmsProperties.URL, "vm://localhost?broker.persistent=false");
    putRunner.setProperty(JmsProperties.DESTINATION_TYPE, JmsProperties.DESTINATION_TYPE_QUEUE);
    putRunner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
    putRunner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);
    WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(putRunner.getProcessContext(), true);
    final Session jmsSession = wrappedProducer.getSession();
    final MessageProducer producer = wrappedProducer.getProducer();
    final StreamMessage message = jmsSession.createStreamMessage();
    message.writeBytes("Hello Stream".getBytes());
    producer.send(message);
    jmsSession.commit();
    GetJMSQueue getJmsQueue = new GetJMSQueue();
    TestRunner runner = TestRunners.newTestRunner(getJmsQueue);
    runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
    runner.setProperty(JmsProperties.URL, "vm://localhost?broker.persistent=false");
    runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
    runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);
    runner.run();
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(new Relationship.Builder().name("success").build());
    assertTrue(flowFiles.size() == 1);
    MockFlowFile successFlowFile = flowFiles.get(0);
    successFlowFile.assertContentEquals("Hello Stream");
    successFlowFile.assertAttributeEquals("jms.JMSDestination", "queue.testing");
    producer.close();
    jmsSession.close();
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) TestRunner(org.apache.nifi.util.TestRunner) Relationship(org.apache.nifi.processor.Relationship) StreamMessage(javax.jms.StreamMessage) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

WrappedMessageProducer (org.apache.nifi.processors.standard.util.WrappedMessageProducer)11 MessageProducer (javax.jms.MessageProducer)9 TestRunner (org.apache.nifi.util.TestRunner)9 Session (javax.jms.Session)8 Test (org.junit.Test)7 MockFlowFile (org.apache.nifi.util.MockFlowFile)6 Message (javax.jms.Message)4 Relationship (org.apache.nifi.processor.Relationship)4 Queue (java.util.Queue)3 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 BytesMessage (javax.jms.BytesMessage)3 JMSException (javax.jms.JMSException)3 StreamMessage (javax.jms.StreamMessage)3 HashMap (java.util.HashMap)2 MapMessage (javax.jms.MapMessage)2 ObjectMessage (javax.jms.ObjectMessage)2 ProcessContext (org.apache.nifi.processor.ProcessContext)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1