Search in sources :

Example 1 with MQTTQueueMessage

use of org.apache.nifi.processors.mqtt.common.MQTTQueueMessage in project nifi by apache.

the class ConsumeMQTT method transferQueue.

private void transferQueue(ProcessSession session) {
    while (!mqttQueue.isEmpty()) {
        FlowFile messageFlowfile = session.create();
        final MQTTQueueMessage mqttMessage = mqttQueue.peek();
        Map<String, String> attrs = new HashMap<>();
        attrs.put(BROKER_ATTRIBUTE_KEY, broker);
        attrs.put(TOPIC_ATTRIBUTE_KEY, mqttMessage.getTopic());
        attrs.put(QOS_ATTRIBUTE_KEY, String.valueOf(mqttMessage.getQos()));
        attrs.put(IS_DUPLICATE_ATTRIBUTE_KEY, String.valueOf(mqttMessage.isDuplicate()));
        attrs.put(IS_RETAINED_ATTRIBUTE_KEY, String.valueOf(mqttMessage.isRetained()));
        messageFlowfile = session.putAllAttributes(messageFlowfile, attrs);
        messageFlowfile = session.write(messageFlowfile, new OutputStreamCallback() {

            @Override
            public void process(final OutputStream out) throws IOException {
                out.write(mqttMessage.getPayload());
            }
        });
        String transitUri = new StringBuilder(broker).append(mqttMessage.getTopic()).toString();
        session.getProvenanceReporter().receive(messageFlowfile, transitUri);
        session.transfer(messageFlowfile, REL_MESSAGE);
        session.commit();
        if (!mqttQueue.remove(mqttMessage) && logger.isWarnEnabled()) {
            logger.warn(new StringBuilder("FlowFile ").append(messageFlowfile.getAttribute(CoreAttributes.UUID.key())).append(" for Mqtt message ").append(mqttMessage).append(" had already been removed from queue, possible duplication of flow files").toString());
        }
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) MQTTQueueMessage(org.apache.nifi.processors.mqtt.common.MQTTQueueMessage)

Example 2 with MQTTQueueMessage

use of org.apache.nifi.processors.mqtt.common.MQTTQueueMessage in project nifi by apache.

the class TestConsumeMQTT method testMessageNotConsumedOnCommitFail.

/**
 * If the session.commit() fails, we should not remove the unprocessed message
 */
@Test
public void testMessageNotConsumedOnCommitFail() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    testRunner.run(1, false);
    ConsumeMQTT processor = (ConsumeMQTT) testRunner.getProcessor();
    MQTTQueueMessage mock = mock(MQTTQueueMessage.class);
    when(mock.getPayload()).thenReturn(new byte[0]);
    when(mock.getTopic()).thenReturn("testTopic");
    BlockingQueue<MQTTQueueMessage> mqttQueue = getMqttQueue(processor);
    mqttQueue.add(mock);
    try {
        ProcessSession session = testRunner.getProcessSessionFactory().createSession();
        transferQueue(processor, (ProcessSession) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { ProcessSession.class }, (proxy, method, args) -> {
            if (method.getName().equals("commit")) {
                throw new RuntimeException();
            } else {
                return method.invoke(session, args);
            }
        }));
        fail("Expected runtime exception");
    } catch (InvocationTargetException e) {
        assertTrue("Expected generic runtime exception, not " + e, e.getCause() instanceof RuntimeException);
    }
    assertTrue("Expected mqttQueue to contain uncommitted message.", mqttQueue.contains(mock));
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) MQTTQueueMessage(org.apache.nifi.processors.mqtt.common.MQTTQueueMessage) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 3 with MQTTQueueMessage

use of org.apache.nifi.processors.mqtt.common.MQTTQueueMessage in project nifi by apache.

the class TestPublishMQTT method verifyPublishedMessage.

@Override
public void verifyPublishedMessage(byte[] payload, int qos, boolean retain) {
    MQTTQueueMessage mqttQueueMessage = mqttTestClient.publishedMessage;
    assertEquals(Arrays.toString(payload), Arrays.toString(mqttQueueMessage.getPayload()));
    assertEquals(qos, mqttQueueMessage.getQos());
    assertEquals(retain, mqttQueueMessage.isRetained());
    assertEquals(topic, mqttQueueMessage.getTopic());
}
Also used : MQTTQueueMessage(org.apache.nifi.processors.mqtt.common.MQTTQueueMessage)

Aggregations

MQTTQueueMessage (org.apache.nifi.processors.mqtt.common.MQTTQueueMessage)3 OutputStream (java.io.OutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ProcessSession (org.apache.nifi.processor.ProcessSession)1 OutputStreamCallback (org.apache.nifi.processor.io.OutputStreamCallback)1 Test (org.junit.Test)1