Search in sources :

Example 16 with MessageProducer

use of javax.jms.MessageProducer 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 17 with MessageProducer

use of javax.jms.MessageProducer 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 18 with MessageProducer

use of javax.jms.MessageProducer 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)

Example 19 with MessageProducer

use of javax.jms.MessageProducer in project nifi by apache.

the class TestPutJMS method testPutCommitRoutesToFailure.

@Test
public void testPutCommitRoutesToFailure() throws JMSException, NoSuchFieldException, IllegalAccessException {
    final PutJMS putJMS = spy(new PutJMS());
    final TestRunner runnerPut = TestRunners.newTestRunner(putJMS);
    runnerPut.setProperty(JmsProperties.JMS_PROVIDER, TEST_PROVIDER);
    runnerPut.setProperty(JmsProperties.URL, TEST_URL);
    runnerPut.setProperty(JmsProperties.DESTINATION_TYPE, TEST_DEST_TYPE);
    runnerPut.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_NAME + testQueueSuffix());
    final ProcessContext context = runnerPut.getProcessContext();
    final Queue<WrappedMessageProducer> wrappedMessageProducerQueue = (Queue) spy(new LinkedBlockingQueue<>());
    injectFieldValue(PutJMS.class, putJMS, "producerQueue", wrappedMessageProducerQueue);
    final WrappedMessageProducer wrappedMessageProducer = spy(JmsFactory.createMessageProducer(context, true));
    final MessageProducer messageProducer = spy(wrappedMessageProducer.getProducer());
    final Connection connection = JmsFactory.createConnection(context);
    final Session jmsSession = spy(JmsFactory.createSession(context, connection, true));
    doAnswer(new Answer<WrappedMessageProducer>() {

        @Override
        public WrappedMessageProducer answer(InvocationOnMock invocationOnMock) {
            return wrappedMessageProducer;
        }
    }).when(wrappedMessageProducerQueue).poll();
    doAnswer(new Answer<MessageProducer>() {

        @Override
        public MessageProducer answer(InvocationOnMock invocationOnMock) {
            return messageProducer;
        }
    }).when(wrappedMessageProducer).getProducer();
    doAnswer(new Answer<Session>() {

        @Override
        public Session answer(InvocationOnMock invocationOnMock) {
            return jmsSession;
        }
    }).when(wrappedMessageProducer).getSession();
    doThrow(new JMSException("force commit to fail")).when(jmsSession).commit();
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("filename", "file1.txt");
    runnerPut.enqueue("putCommitRoutesToFailure".getBytes(), attributes);
    runnerPut.run();
    assertEquals(0, runnerPut.getFlowFilesForRelationship(PutJMS.REL_SUCCESS).size());
    assertEquals(1, runnerPut.getFlowFilesForRelationship(PutJMS.REL_FAILURE).size());
    final List<MockFlowFile> flowFilesFail = runnerPut.getFlowFilesForRelationship(PutJMS.REL_FAILURE);
    assertEquals(1, flowFilesFail.size());
}
Also used : HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ProcessContext(org.apache.nifi.processor.ProcessContext) MockFlowFile(org.apache.nifi.util.MockFlowFile) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MessageProducer(javax.jms.MessageProducer) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Queue(java.util.Queue) Session(javax.jms.Session) Test(org.junit.Test)

Example 20 with MessageProducer

use of javax.jms.MessageProducer in project nifi by apache.

the class TestPutJMS method testCreateMessageDirectly.

@Test
public void testCreateMessageDirectly() throws JMSException {
    final PutJMS putJMS = new PutJMS();
    final TestRunner runnerPut = TestRunners.newTestRunner(putJMS);
    runnerPut.setProperty(JmsProperties.JMS_PROVIDER, TEST_PROVIDER);
    runnerPut.setProperty(JmsProperties.URL, TEST_URL);
    runnerPut.setProperty(JmsProperties.DESTINATION_TYPE, TEST_DEST_TYPE);
    runnerPut.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_NAME + testQueueSuffix());
    runnerPut.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, TEST_ACK_MODE);
    final WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(runnerPut.getProcessContext(), true);
    final Session jmsSession = wrappedProducer.getSession();
    final MessageProducer producer = wrappedProducer.getProducer();
    final Message message = jmsSession.createTextMessage("createMessageDirectly");
    producer.send(message);
    jmsSession.commit();
    final GetJMSQueue getJmsQueue = new GetJMSQueue();
    final TestRunner runnerGet = TestRunners.newTestRunner(getJmsQueue);
    runnerGet.setProperty(JmsProperties.JMS_PROVIDER, TEST_PROVIDER);
    runnerGet.setProperty(JmsProperties.URL, TEST_URL);
    runnerGet.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_NAME + testQueueSuffix());
    runnerGet.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, TEST_ACK_MODE);
    runnerGet.run();
    final List<MockFlowFile> flowFiles = runnerGet.getFlowFilesForRelationship(new Relationship.Builder().name("success").build());
    assertEquals(1, flowFiles.size());
    final MockFlowFile successFlowFile = flowFiles.get(0);
    successFlowFile.assertContentEquals("createMessageDirectly");
    successFlowFile.assertAttributeEquals("jms.JMSDestination", TEST_DEST_NAME + testQueueSuffix());
    producer.close();
    jmsSession.close();
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) Message(javax.jms.Message) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) TestRunner(org.apache.nifi.util.TestRunner) Relationship(org.apache.nifi.processor.Relationship) MessageProducer(javax.jms.MessageProducer) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

MessageProducer (javax.jms.MessageProducer)1512 Session (javax.jms.Session)1302 MessageConsumer (javax.jms.MessageConsumer)987 Connection (javax.jms.Connection)915 TextMessage (javax.jms.TextMessage)888 Test (org.junit.Test)850 Message (javax.jms.Message)646 Queue (javax.jms.Queue)404 JMSException (javax.jms.JMSException)213 Destination (javax.jms.Destination)190 ConnectionFactory (javax.jms.ConnectionFactory)147 BytesMessage (javax.jms.BytesMessage)140 Topic (javax.jms.Topic)140 TopicSession (javax.jms.TopicSession)135 TopicConnection (javax.jms.TopicConnection)115 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)108 ObjectMessage (javax.jms.ObjectMessage)98 CountDownLatch (java.util.concurrent.CountDownLatch)91 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)90 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)88