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 void handle(InetSocketAddress sourceAddress, byte[] message, boolean partialMessage) { ProcessSession session = this.sessionFactory.createSession(); FlowFile flowFile = session.create(); flowFile = session.write(flowFile, new OutputStreamCallback() { @Override public void process(OutputStream out) throws IOException { out.write(message); } }); flowFile = session.putAttribute(flowFile, SOURCE_ENDPOINT_ATTRIBUTE, sourceAddress.toString()); if (!GetTCP.this.dynamicAttributes.isEmpty()) { flowFile = session.putAllAttributes(flowFile, GetTCP.this.dynamicAttributes); } if (partialMessage) { session.transfer(flowFile, REL_PARTIAL); } else { session.transfer(flowFile, REL_SUCCESS); } session.commit(); } }
@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; } }
/** * Cleanup */ @OnStopped public void stop() { unsubscribe(); if (!renderedXMLs.isEmpty()) { if (sessionFactory != null) { getLogger().info("Finishing processing leftover events"); ProcessSession session = sessionFactory.createSession(); processQueue(session); } else { throw new ProcessException("Stopping the processor but there is no ProcessSessionFactory stored and there are messages in the 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 events and stopping before the onTrigger happens. The messages " + "in the internal queue cannot finish processing until until the processor is triggered to run."); } } sessionFactory = null; provenanceUri = null; renderedXMLs = null; }
@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 testLogPropertyRegexNoIgnore() { 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_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, containsString("foobaz-value")); assertThat(logMessage, containsString("foo-value")); assertThat(logMessage, not(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")); }
@Override public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException { this.sessionFactory = sessionFactory; if (!isSubscribed()) { String errorMessage = subscribe(context); if (errorMessage != null) { context.yield(); getLogger().error(errorMessage); return; } } final int flowFileCount = processQueue(sessionFactory.createSession()); final long now = System.currentTimeMillis(); if (flowFileCount > 0) { lastActivityTimestamp = now; } else if (inactiveDurationToReconnect > 0) { if ((now - lastActivityTimestamp) > inactiveDurationToReconnect) { getLogger().info("Exceeds configured 'inactive duration to reconnect' {} ms. Unsubscribe to reconnect..", new Object[]{inactiveDurationToReconnect}); unsubscribe(); } } }
@Test public void testLogPropertyWithCSVAndRegexNoIgnore() { 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(); // there's an AND relationship between like properties, so only foo should be logged in this case runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_CSV, "foo, bar"); runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_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, containsString("foo-value")); assertThat(logMessage, not(containsString("bar-value"))); }
@Test public void testLogPropertyCSVWithIgnoreCSV() { 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(); // add foo,foobaz and remove foobaz runner.setProperty(LogAttribute.ATTRIBUTES_TO_LOG_CSV, "foo,foobaz"); runner.setProperty(LogAttribute.ATTRIBUTES_TO_IGNORE_CSV, "foobaz"); 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, not(containsString("bar-value"))); }
@Test public void testLogPropertyRegexWithIgnoreRegex() { 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_REGEX, "foo.*"); // includes foo,foobaz runner.setProperty(LogAttribute.ATTRIBUTES_TO_IGNORE_REGEX, "foobaz.*"); // includes foobaz 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, not(containsString("bar-value"))); } }