gpt4 book ai didi

java - Apache Nifi 属性描述符的 dynamicallyModifiesClasspath() 的 Hello world 程序不起作用

转载 作者:行者123 更新时间:2023-11-30 12:05:23 27 4
gpt4 key购买 nike

我有一个处理器,它依赖于一些 jar 作为依赖项。然而,依赖 jar 的更改非常频繁。所以我希望能够将 jar 路径指定为处理器的属性,并让 nifi 在每次修改属性中的路径并重新启动处理器时加载 jar。这应该可以使用 dynamicallyModifiesClasspath 来实现如 this article 中所述.但是我无法做到这一点。下面是我使用 dynamicallyModifiesClasspath 属性的 hello world 程序代码:

(在下文中,Djl 代表“动态 jar 加载”,我猜是为这个 hello world 程序命名的随机前缀。)

DjlDependencyClass.java

这是我的 nifi 处理器所依赖的类,我想在我的 nifi 处理器中动态更改它的 jar 路径。

public class DjlDependencyClass {

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

MyDjlProcessor.java

这是依赖于 DjlDependencyClass 的 nifi 处理器。

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());
}
}

这就是 pom 内部的依赖项被标记为具有 provided 范围的方式,这样依赖项 jar 就不会嵌入到 nar 中:

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

我正在测试这个处理器如下:

enter image description here

处理器配置:

enter image description here

我遇到以下异常:

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)

我是不是在做什么蠢事?

请下载两个项目(nifi 和依赖jar)here .

最佳答案

PropertyDescriptor 的 API 文档提到以下注释:

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.

我不确定“无效”部分是否仍然适用,但是是的,在更新您的处理器代码以使用 @RequiresInstanceClassloading 对其进行注释之后,我能够让它工作。

关于java - Apache Nifi 属性描述符的 dynamicallyModifiesClasspath() 的 Hello world 程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56325462/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com