use of org.apache.nifi.processor.ProcessSession in project nifi by apache.
the class AbstractFlumeProcessor method onTrigger.
/*
* Borrowed from AbstractProcessor. The FlumeSourceProcessor needs to implement this directly
* to handle event driven sources, but it's marked final in AbstractProcessor.
*/
@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;
}
}
use of org.apache.nifi.processor.ProcessSession in project nifi by apache.
the class StandardFunnel method onTrigger.
@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);
}
}
use of org.apache.nifi.processor.ProcessSession in project kylo by Teradata.
the class ExecuteSparkContextJob method onTrigger.
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
final ComponentLog logger = getLog();
FlowFile flowFile = null;
try {
if (context.hasIncomingConnection()) {
flowFile = session.get();
if (flowFile == null) {
return;
}
}
} catch (NoSuchMethodError e) {
logger.error("Failed to get incoming", e);
}
FlowFile outgoing = (flowFile == null ? session.create() : flowFile);
final String appName = context.getProperty(APP_NAME).evaluateAttributeExpressions(flowFile).getValue().trim();
final String classPath = context.getProperty(CLASS_PATH).evaluateAttributeExpressions(flowFile).getValue().trim();
final String contextName = context.getProperty(CONTEXT_NAME).evaluateAttributeExpressions(flowFile).getValue().trim();
final SparkContextType contextType = SparkContextType.valueOf(context.getProperty(CONTEXT_TYPE).getValue());
final JobService jobService = context.getProperty(JOB_SERVICE).asControllerService(JobService.class);
final String resultsOutputLocation = context.getProperty(RESULTS_OUTPUT_LOCATION).getValue();
final String numExecutors = context.getProperty(NUM_EXECUTORS).evaluateAttributeExpressions(flowFile).getValue().trim();
final String memPerNode = context.getProperty(MEM_PER_NODE).evaluateAttributeExpressions(flowFile).getValue().trim();
final String numCPUCores = context.getProperty(NUM_CPU_CORES).evaluateAttributeExpressions(flowFile).getValue().trim();
final int contextTimeout = context.getProperty(CONTEXT_TIMEOUT).evaluateAttributeExpressions(flowFile).asInteger();
final boolean async = context.getProperty(ASYNC).asBoolean();
String args = "";
if (context.getProperty(ARGS).isSet()) {
args = context.getProperty(ARGS).evaluateAttributeExpressions(flowFile).getValue().trim();
}
boolean createSuccess = jobService.createContext(contextName, numExecutors, memPerNode, numCPUCores, contextType, contextTimeout, false);
if (createSuccess) {
final SparkJobResult result = jobService.executeSparkContextJob(appName, classPath, contextName, args, async);
if (result.success) {
if (Objects.equals(resultsOutputLocation, FLOW_FILE_ATTRIBUTE_VALUE)) {
outgoing = session.putAttribute(outgoing, appName + ".result", result.result);
} else {
outgoing = session.write(outgoing, outputStream -> IOUtils.write(result.result, outputStream, "UTF-8"));
}
session.transfer(outgoing, REL_SUCCESS);
} else {
session.transfer(outgoing, REL_FAILURE);
}
} else {
session.transfer(outgoing, REL_FAILURE);
}
}
use of org.apache.nifi.processor.ProcessSession in project nifi by apache.
the class TestHttpRemoteSiteListener method testDuplicatedTransactionId.
@Test
public void testDuplicatedTransactionId() {
HttpRemoteSiteListener transactionManager = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
String transactionId = transactionManager.createTransaction();
assertTrue("Transaction should be active.", transactionManager.isTransactionActive(transactionId));
ProcessSession processSession = Mockito.mock(ProcessSession.class);
FlowFileTransaction transaction = new FlowFileTransaction(processSession, null, null, 0, null, null);
transactionManager.holdTransaction(transactionId, transaction, null);
try {
transactionManager.holdTransaction(transactionId, transaction, null);
fail("The same transaction id can't hold another transaction");
} catch (IllegalStateException e) {
}
}
use of org.apache.nifi.processor.ProcessSession in project nifi by apache.
the class TestHttpRemoteSiteListener method testNoneExistingTransaction.
@Test
public void testNoneExistingTransaction() {
HttpRemoteSiteListener transactionManager = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
String transactionId = "does-not-exist-1";
assertFalse("Transaction should not be active.", transactionManager.isTransactionActive(transactionId));
ProcessSession processSession = Mockito.mock(ProcessSession.class);
FlowFileTransaction transaction = new FlowFileTransaction(processSession, null, null, 0, null, null);
try {
transactionManager.holdTransaction(transactionId, transaction, null);
} catch (IllegalStateException e) {
fail("Transaction can be held even if the transaction id is not valid anymore," + " in order to support large file or slow network.");
}
transactionId = "does-not-exist-2";
try {
transactionManager.finalizeTransaction(transactionId);
fail("But transaction should not be finalized if it isn't active.");
} catch (IllegalStateException e) {
}
}
Aggregations