use of org.apache.nifi.nar.NarCloseable in project nifi by apache.
the class StandardProcessScheduler method schedule.
@Override
public void schedule(final ReportingTaskNode taskNode) {
final LifecycleState lifecycleState = getLifecycleState(requireNonNull(taskNode), true);
if (lifecycleState.isScheduled()) {
return;
}
final int activeThreadCount = lifecycleState.getActiveThreadCount();
if (activeThreadCount > 0) {
throw new IllegalStateException("Reporting Task " + taskNode.getName() + " cannot be started because it has " + activeThreadCount + " threads still running");
}
if (!taskNode.isValid()) {
throw new IllegalStateException("Reporting Task " + taskNode.getName() + " is not in a valid state for the following reasons: " + taskNode.getValidationErrors());
}
final SchedulingAgent agent = getSchedulingAgent(taskNode.getSchedulingStrategy());
lifecycleState.setScheduled(true);
final Runnable startReportingTaskRunnable = new Runnable() {
@Override
public void run() {
final long lastStopTime = lifecycleState.getLastStopTime();
final ReportingTask reportingTask = taskNode.getReportingTask();
// Continually attempt to start the Reporting Task, and if we fail sleep for a bit each time.
while (true) {
try {
synchronized (lifecycleState) {
// bail; another thread will be responsible for invoking the @OnScheduled methods.
if (!lifecycleState.isScheduled() || lifecycleState.getLastStopTime() != lastStopTime) {
return;
}
try (final NarCloseable x = NarCloseable.withComponentNarLoader(reportingTask.getClass(), reportingTask.getIdentifier())) {
ReflectionUtils.invokeMethodsWithAnnotation(OnScheduled.class, reportingTask, taskNode.getConfigurationContext());
}
agent.schedule(taskNode, lifecycleState);
return;
}
} catch (final Exception e) {
final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
final ComponentLog componentLog = new SimpleProcessLogger(reportingTask.getIdentifier(), reportingTask);
componentLog.error("Failed to invoke @OnEnabled method due to {}", cause);
LOG.error("Failed to invoke the On-Scheduled Lifecycle methods of {} due to {}; administratively yielding this " + "ReportingTask and will attempt to schedule it again after {}", new Object[] { reportingTask, e.toString(), administrativeYieldDuration }, e);
ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnUnscheduled.class, reportingTask, taskNode.getConfigurationContext());
ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, reportingTask, taskNode.getConfigurationContext());
try {
Thread.sleep(administrativeYieldMillis);
} catch (final InterruptedException ie) {
}
}
}
}
};
componentLifeCycleThreadPool.execute(startReportingTaskRunnable);
taskNode.setScheduledState(ScheduledState.RUNNING);
}
use of org.apache.nifi.nar.NarCloseable in project nifi by apache.
the class StandardProcessScheduler method stopConnectable.
private synchronized void stopConnectable(final Connectable connectable) {
final LifecycleState state = getLifecycleState(requireNonNull(connectable), false);
if (!state.isScheduled()) {
return;
}
state.setScheduled(false);
getSchedulingAgent(connectable).unschedule(connectable, state);
if (!state.isScheduled() && state.getActiveThreadCount() == 0 && state.mustCallOnStoppedMethods()) {
final ConnectableProcessContext processContext = new ConnectableProcessContext(connectable, encryptor, getStateManager(connectable.getIdentifier()));
try (final NarCloseable x = NarCloseable.withComponentNarLoader(connectable.getClass(), connectable.getIdentifier())) {
ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, connectable, processContext);
}
}
}
use of org.apache.nifi.nar.NarCloseable in project nifi by apache.
the class StandardControllerServiceInvocationHandler method invoke.
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String methodName = method.getName();
if ("initialize".equals(methodName) || "onPropertyModified".equals(methodName)) {
throw new UnsupportedOperationException(method + " may only be invoked by the NiFi framework");
}
final ControllerServiceNode node = serviceNodeHolder.get();
final ControllerServiceState state = node.getState();
// only allow method call if service state is ENABLED.
final boolean disabled = state != ControllerServiceState.ENABLED;
if (disabled && !validDisabledMethods.contains(method)) {
// Use nar class loader here because we are implicitly calling toString() on the original implementation.
try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(originalService.getClass(), originalService.getIdentifier())) {
throw new IllegalStateException("Cannot invoke method " + method + " on Controller Service " + originalService.getIdentifier() + " because the Controller Service is disabled");
} catch (final Throwable e) {
throw new IllegalStateException("Cannot invoke method " + method + " on Controller Service with identifier " + originalService.getIdentifier() + " because the Controller Service is disabled");
}
}
try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(originalService.getClass(), originalService.getIdentifier())) {
return method.invoke(originalService, args);
} catch (final InvocationTargetException e) {
// to instead re-throw what the ControllerService threw, so we pull it out of the InvocationTargetException.
throw e.getCause();
}
}
use of org.apache.nifi.nar.NarCloseable in project nifi by apache.
the class ControllerServiceInitializer method initialize.
@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
ControllerService controllerService = (ControllerService) component;
ControllerServiceInitializationContext context = new MockControllerServiceInitializationContext();
try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
controllerService.initialize(context);
}
}
use of org.apache.nifi.nar.NarCloseable in project nifi by apache.
the class ControllerServiceInitializer method teardown.
@Override
public void teardown(ConfigurableComponent component) {
try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), component.getIdentifier())) {
ControllerService controllerService = (ControllerService) component;
final ComponentLog logger = new MockComponentLogger();
final MockConfigurationContext context = new MockConfigurationContext();
ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, controllerService, logger, context);
} finally {
ExtensionManager.removeInstanceClassLoader(component.getIdentifier());
}
}
Aggregations