public void onTrigger(ProcessContext context, ProcessSessionFactory factory) throws ProcessException { final ProcessSession session = factory.createSession(); processFlowFile(context, session); } }
@Override public synchronized ProcessSession createSession() { if (terminated) { throw new TerminatedTaskException(); } final ProcessSession session = delegate.createSession(); sessionMap.put(session, Boolean.TRUE); return session; }
@Override public final void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException { final ProcessSession session = sessionFactory.createSession(); try { onTrigger(context, session); session.commit(); } catch (final Throwable t) { session.rollback(true); throw t; } }
@Override protected BasicTransactionSemantics createTransaction() { LifecycleState lifecycleState = getLifecycleState(); if (lifecycleState == LifecycleState.STOP) { throw new ChannelFullException("Can't write to a stopped channel"); } return new NifiTransaction(sessionFactory.createSession(), relationship); }
@Override public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException { final ProcessSession session = sessionFactory.createSession(); try { onTrigger(context, session); session.commit(); } catch (final ProcessException e) { session.rollback(); throw e; } catch (final Throwable t) { session.rollback(); throw new RuntimeException(t); } }
@Override public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException { final ProcessSession session = sessionFactory.createSession(); try { onTrigger(context, session); session.commit(); } catch (final ProcessException e) { session.rollback(); throw e; } catch (final Throwable t) { session.rollback(); throw new RuntimeException(t); } }
public static void onTrigger( ProcessContext context, ProcessSessionFactory sessionFactory, ComponentLog logger, OnTrigger onTrigger, RollbackSession rollbackSession) throws ProcessException { final ProcessSession session = sessionFactory.createSession(); try { onTrigger.execute(session); session.commit(); } catch (final Throwable t) { logger.error("{} failed to process due to {}; rolling back session", new Object[]{onTrigger, t}); rollbackSession.rollback(session, t); throw t; } } }
@Override public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException { final ProcessSession session = sessionFactory.createSession(); try { onTrigger(context, session); session.commit(); } catch (final Throwable t) { getLogger() .error("{} failed to process due to {}; rolling back session", new Object[]{this, t}); session.rollback(true); throw t; } }
@Override public final void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException { if (processSessionFactory == null) { processSessionFactory = sessionFactory; } ProcessSession session = sessionFactory.createSession(); try { onTrigger(context, session); session.commit(); } catch (final Throwable t) { getLogger().error("{} failed to process due to {}; rolling back session", new Object[]{this, t}); session.rollback(true); throw t; } }
@OnStopped public void onStopped(final ProcessContext context) throws IOException { if(mqttQueue != null && !mqttQueue.isEmpty() && processSessionFactory != null) { logger.info("Finishing processing leftover messages"); ProcessSession session = processSessionFactory.createSession(); transferQueue(session); } else { if (mqttQueue!= null && !mqttQueue.isEmpty()){ throw new ProcessException("Stopping the processor but there is no ProcessSessionFactory stored and there are messages in the MQTT internal queue. Removing the processor now will " + "clear the queue but will result in DATA LOSS. This is normally due to starting the processor, receiving messages and stopping before the onTrigger happens. The messages " + "in the MQTT internal queue cannot finish processing until until the processor is triggered to run."); } } }
@Test public void testLogPropertyCSVNoIgnore() { final LogAttribute logAttribute = new LogAttribute(); final TestRunner runner = TestRunners.newTestRunner(logAttribute); final ProcessContext context = runner.getProcessContext(); final ProcessSession session = runner.getProcessSessionFactory().createSession(); final MockComponentLog LOG = runner.getLogger(); runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_CSV, "foo, bar"); final Map<String,String> attrs = Maps.newHashMap(); attrs.put("foo", "foo-value"); attrs.put("bar", "bar-value"); attrs.put("foobaz", "foobaz-value"); final MockFlowFile flowFile = runner.enqueue("content", attrs); final String logMessage = logAttribute.processFlowFile(LOG, LogAttribute.DebugLevels.info, flowFile, session, context); assertThat(logMessage, not(containsString("foobaz-value"))); assertThat(logMessage, containsString("foo-value")); assertThat(logMessage, containsString("bar-value")); }
@Test public void testLogPropertyWithIgnoreCSV() { final LogAttribute logAttribute = new LogAttribute(); final TestRunner runner = TestRunners.newTestRunner(logAttribute); final ProcessContext context = runner.getProcessContext(); final ProcessSession session = runner.getProcessSessionFactory().createSession(); final MockComponentLog LOG = runner.getLogger(); runner.setProperty(LogAttribute.ATTRIBUTES_TO_IGNORE_CSV, "bar"); final Map<String,String> attrs = Maps.newHashMap(); attrs.put("foo", "foo-value"); attrs.put("bar", "bar-value"); attrs.put("foobaz", "foobaz-value"); final MockFlowFile flowFile = runner.enqueue("content", attrs); final String logMessage = logAttribute.processFlowFile(LOG, LogAttribute.DebugLevels.info, flowFile, session, context); assertThat(logMessage, containsString("foobaz-value")); assertThat(logMessage, containsString("foo-value")); assertThat(logMessage, not(containsString("bar-value"))); }
@Test public void testLogPropertyWithIgnoreRegex() { final LogAttribute logAttribute = new LogAttribute(); final TestRunner runner = TestRunners.newTestRunner(logAttribute); final ProcessContext context = runner.getProcessContext(); final ProcessSession session = runner.getProcessSessionFactory().createSession(); final MockComponentLog LOG = runner.getLogger(); runner.setProperty(LogAttribute.ATTRIBUTES_TO_IGNORE_REGEX, "foo.*"); final Map<String,String> attrs = Maps.newHashMap(); attrs.put("foo", "foo-value"); attrs.put("bar", "bar-value"); attrs.put("foobaz", "foobaz-value"); final MockFlowFile flowFile = runner.enqueue("content", attrs); final String logMessage = logAttribute.processFlowFile(LOG, LogAttribute.DebugLevels.info, flowFile, session, context); assertThat(logMessage, not(containsString("foobaz-value"))); assertThat(logMessage, not(containsString("foo-value"))); assertThat(logMessage, containsString("bar-value")); }
@Test public void testAttribute_includeCoreAttributesAttribute() throws IOException { final TestRunner testRunner = TestRunners.newTestRunner(new AttributesToJSON()); testRunner.setProperty(AttributesToJSON.INCLUDE_CORE_ATTRIBUTES, "true"); ProcessSession session = testRunner.getProcessSessionFactory().createSession(); FlowFile ff = session.create(); testRunner.enqueue(ff); testRunner.run(); List<MockFlowFile> flowFilesForRelationship = testRunner.getFlowFilesForRelationship(AttributesToJSON.REL_SUCCESS); testRunner.assertTransferCount(AttributesToJSON.REL_FAILURE, 0); testRunner.assertTransferCount(AttributesToJSON.REL_SUCCESS, 1); MockFlowFile flowFile = flowFilesForRelationship.get(0); assertNull(flowFile.getAttribute(CoreAttributes.MIME_TYPE.key())); Map<String, String> val = new ObjectMapper().readValue(flowFile.getAttribute(AttributesToJSON.JSON_ATTRIBUTE_NAME), HashMap.class); assertEquals(3, val.size()); Set<String> coreAttributes = Arrays.stream(CoreAttributes.values()).map(CoreAttributes::key).collect(Collectors.toSet()); val.keySet().forEach(k -> assertTrue(coreAttributes.contains(k))); }
@Test public void testAttribute_noIncludeCoreAttributesContent() throws IOException { final TestRunner testRunner = TestRunners.newTestRunner(new AttributesToJSON()); testRunner.setProperty(AttributesToJSON.INCLUDE_CORE_ATTRIBUTES, "false"); testRunner.setProperty(AttributesToJSON.DESTINATION, AttributesToJSON.DESTINATION_CONTENT); ProcessSession session = testRunner.getProcessSessionFactory().createSession(); FlowFile ff = session.create(); ff = session.putAttribute(ff, TEST_ATTRIBUTE_KEY, TEST_ATTRIBUTE_VALUE); ff = session.putAttribute(ff, CoreAttributes.PATH.key(), TEST_ATTRIBUTE_VALUE); testRunner.enqueue(ff); testRunner.run(); testRunner.assertTransferCount(AttributesToJSON.REL_SUCCESS, 1); testRunner.assertTransferCount(AttributesToJSON.REL_FAILURE, 0); ObjectMapper mapper = new ObjectMapper(); Map<String, String> val = mapper.readValue(testRunner.getFlowFilesForRelationship(AttributesToJSON.REL_SUCCESS).get(0).toByteArray(), HashMap.class); assertEquals(TEST_ATTRIBUTE_VALUE, val.get(TEST_ATTRIBUTE_KEY)); assertEquals(1, val.size()); }
@Test(expected = AssertionError.class) public void testInvalidUserSuppliedAttributeList() throws Exception { final TestRunner testRunner = TestRunners.newTestRunner(new AttributesToJSON()); //Attribute list CANNOT be empty testRunner.setProperty(AttributesToJSON.ATTRIBUTES_LIST, ""); ProcessSession session = testRunner.getProcessSessionFactory().createSession(); FlowFile ff = session.create(); testRunner.enqueue(ff); testRunner.run(); }
@Test public void testContent_emptyListUserSpecifiedAttributes() throws Exception { final TestRunner testRunner = TestRunners.newTestRunner(new AttributesToJSON()); testRunner.setProperty(AttributesToJSON.DESTINATION, AttributesToJSON.DESTINATION_CONTENT); testRunner.setProperty(AttributesToJSON.INCLUDE_CORE_ATTRIBUTES, "false"); ProcessSession session = testRunner.getProcessSessionFactory().createSession(); FlowFile ff = session.create(); testRunner.enqueue(ff); testRunner.run(); testRunner.getFlowFilesForRelationship(AttributesToJSON.REL_SUCCESS).get(0) .assertAttributeNotExists(AttributesToJSON.JSON_ATTRIBUTE_NAME); testRunner.assertTransferCount(AttributesToJSON.REL_SUCCESS, 1); testRunner.assertTransferCount(AttributesToJSON.REL_FAILURE, 0); testRunner.getFlowFilesForRelationship(AttributesToJSON.REL_SUCCESS).get(0).assertContentEquals("{}"); }
@Test(expected = AssertionError.class) public void testInvalidIncludeCoreAttributesProperty() throws Exception { final TestRunner testRunner = TestRunners.newTestRunner(new AttributesToJSON()); testRunner.setProperty(AttributesToJSON.ATTRIBUTES_LIST, "val1,val2"); testRunner.setProperty(AttributesToJSON.DESTINATION, AttributesToJSON.DESTINATION_ATTRIBUTE); testRunner.setProperty(AttributesToJSON.INCLUDE_CORE_ATTRIBUTES, "maybe"); ProcessSession session = testRunner.getProcessSessionFactory().createSession(); FlowFile ff = session.create(); testRunner.enqueue(ff); testRunner.run(); }
@Test public void testInvalidJSONValueInAttribute() throws Exception { final TestRunner testRunner = TestRunners.newTestRunner(new AttributesToJSON()); testRunner.setProperty(AttributesToJSON.DESTINATION, AttributesToJSON.DESTINATION_ATTRIBUTE); ProcessSession session = testRunner.getProcessSessionFactory().createSession(); FlowFile ff = session.create(); //Create attribute that contains an invalid JSON Character ff = session.putAttribute(ff, TEST_ATTRIBUTE_KEY, "'badjson'"); testRunner.enqueue(ff); testRunner.run(); //Expecting success transition because Jackson is taking care of escaping the bad JSON characters testRunner.getFlowFilesForRelationship(AttributesToJSON.REL_SUCCESS).get(0) .assertAttributeExists(AttributesToJSON.JSON_ATTRIBUTE_NAME); testRunner.assertTransferCount(AttributesToJSON.REL_SUCCESS, 1); testRunner.assertTransferCount(AttributesToJSON.REL_FAILURE, 0); }
@Test public void testSplit_pathToNullValue() throws Exception { final TestRunner testRunner = TestRunners.newTestRunner(new SplitJson()); testRunner.setProperty(SplitJson.ARRAY_JSON_PATH_EXPRESSION, "$.nullField"); ProcessSession session = testRunner.getProcessSessionFactory().createSession(); FlowFile ff = session.create(); ff = session.write(ff, new OutputStreamCallback() { @Override public void process(OutputStream out) throws IOException { try (OutputStream outputStream = new BufferedOutputStream(out)) { outputStream.write("{\"stringField\": \"String Value\", \"nullField\": null}".getBytes(StandardCharsets.UTF_8)); } } }); testRunner.enqueue(ff); testRunner.run(); testRunner.assertTransferCount(SplitJson.REL_FAILURE, 1); }