public static JmsProcessingSummary map2FlowFile( final ProcessContext context, final ProcessSession session, final Message message, final boolean addAttributes, ProcessorLog logger) throws Exception { // Currently not very useful, because always one Message == one FlowFile final IntegerHolder msgsThisFlowFile = new IntegerHolder(1); FlowFile flowFile = session.create(); try { // MapMessage is exception, add only name-value pairs to FlowFile attributes if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; flowFile = session.putAllAttributes(flowFile, createMapMessageValues(mapMessage)); } else { // all other message types, write Message body to FlowFile content flowFile = session.write( flowFile, new OutputStreamCallback() { @Override public void process(final OutputStream rawOut) throws IOException { try (final OutputStream out = new BufferedOutputStream(rawOut, 65536)) { final byte[] messageBody = JmsFactory.createByteArray(message); out.write(messageBody); } catch (final JMSException e) { throw new ProcessException("Failed to receive JMS Message due to {}", e); } } }); } if (addAttributes) { flowFile = session.putAllAttributes(flowFile, JmsFactory.createAttributeMap(message)); } session.getProvenanceReporter().receive(flowFile, context.getProperty(URL).getValue()); session.transfer(flowFile, REL_SUCCESS); logger.info( "Created {} from {} messages received from JMS Server and transferred to 'success'", new Object[] {flowFile, msgsThisFlowFile.get()}); return new JmsProcessingSummary(flowFile.getSize(), message, flowFile); } catch (Exception e) { session.remove(flowFile); throw e; } }
@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")); producer.send(message); jmsSession.commit(); producer.close(); jmsSession.close(); }
@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(); }
@org.junit.Ignore public void testSendMapToQueue() 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(); final MapMessage message = jmsSession.createMapMessage(); message.setString("foo!", "bar"); message.setString("bacon", "meat"); producer.send(message); jmsSession.commit(); producer.close(); jmsSession.close(); }