Hello world program of dynamicallyModifiesClasspath() of Apache Nifi properties descriptor not working

泪湿孤枕 提交于 2020-01-11 11:02:36

问题


I have a processor which depends on some jar as a dependency. However changes are made to the dependency jar quite frequenctly. So I want to be able to specify the jar path as a property of processor and let the nifi load the jar each time I modify the path in the property and restart the processor. This is supposed to be doable using dynamicallyModifiesClasspath as explained in this article. However I am unable to do this. Below is my code of hello world program using dynamicallyModifiesClasspath property:

(Below, Djl stands for "dynamic jar loading", a random prefix I guessed to name this hello world program.)

DjlDependencyClass.java

This is the class on which my nifi processor depends and I want to dynamically change its jar path in my nifi processor.

public class DjlDependencyClass {

    public static String getMessage()
    {
        return "DJL-DEPENDENCY VERSION-1";
    }
}

MyDjlProcessor.java

This is the nifi processor which depends on DjlDependencyClass.

import org.apache.nifi.annotation.lifecycle.OnScheduled;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.processor.exception.ProcessException;    
import org.apache.nifi.processor.AbstractProcessor;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processor.ProcessorInitializationContext;
import org.apache.nifi.processor.Relationship;
import org.apache.nifi.processor.util.StandardValidators;

import com.mycompany.djldependency.DjlDependencyClass;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MyDjlProcessor extends AbstractProcessor {

    public static final Relationship MY_RELATIONSHIP = new Relationship.Builder()
            .name("MY_RELATIONSHIP")
            .description("Example relationship")
            .build();

    public static final PropertyDescriptor pathToDjlDependencyJar = new PropertyDescriptor.Builder()
            .name("Djl Dependency JAR")
            .description("Djl Dependency JAR")
            .required(true)
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
            .dynamicallyModifiesClasspath(true)
            .expressionLanguageSupported(true)
            .build();

    private List<PropertyDescriptor> descriptors;

    private Set<Relationship> relationships;

    @Override
    protected void init(final ProcessorInitializationContext context) {
        final List<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>();
        descriptors.add(pathToDjlDependencyJar);
        this.descriptors = Collections.unmodifiableList(descriptors);

        final Set<Relationship> relationships = new HashSet<Relationship>();
        relationships.add(MY_RELATIONSHIP);
        this.relationships = Collections.unmodifiableSet(relationships);
    }

    @Override
    public Set<Relationship> getRelationships() {
        return this.relationships;
    }

    @Override
    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
        return descriptors;
    }

    @OnScheduled
    public void onScheduled(final ProcessContext context) {

    }

    @Override
    public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
        System.out.println(DjlDependencyClass.getMessage());
    }
}

This is how dependency inside pom is marked to have provided scope, so that the dependency jar will not be embedded inside nar:

   <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>djl-dependency</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>provided </scope>
    </dependency>

I am testing this processor as follows:

Configuration of the processor:

I am getting following exception:

2019-05-27 17:01:54,536 ERROR [Timer-Driven Process Thread-1] com.mycompany.djl.MyDjlProcessor MyDjlProcessor[id=f8fa5750-016a-1000-ecc3-c19732119332] MyDjlProcessor[id=f8fa5750-016a-1000-ecc3-c19732119332] failed to process due to java.lang.NoClassDefFoundError: com/mycompany/djldependency/DjlDependencyClass; rolling back session: {}
java.lang.NoClassDefFoundError: com/mycompany/djldependency/DjlDependencyClass
    at com.mycompany.djl.MyDjlProcessor.onTrigger(MyDjlProcessor.java:76)
    at org.apache.nifi.processor.AbstractProcessor.onTrigger(AbstractProcessor.java:27)
    at org.apache.nifi.controller.StandardProcessorNode.onTrigger(StandardProcessorNode.java:1122)
    at org.apache.nifi.controller.tasks.ContinuallyRunProcessorTask.call(ContinuallyRunProcessorTask.java:147)
    at org.apache.nifi.controller.tasks.ContinuallyRunProcessorTask.call(ContinuallyRunProcessorTask.java:47)
    at org.apache.nifi.controller.scheduling.TimerDrivenSchedulingAgent$1.run(TimerDrivenSchedulingAgent.java:128)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

Am I doing something stupid here?

Please download both projects (nifi and dependency jar) here.


回答1:


The API document for PropertyDescriptor mentions the following note:

If a component contains a PropertyDescriptor where dynamicallyModifiesClasspath is set to true, the component must also be annotated with @RequiresInstanceClassloading, otherwise the component will be considered invalid.

I'm not sure whether that 'invalid' part still holds true but yeah, after updating your processor code to have it annotated with @RequiresInstanceClassloading, I was able to get it working.



来源:https://stackoverflow.com/questions/56325462/hello-world-program-of-dynamicallymodifiesclasspath-of-apache-nifi-properties

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!