How do I create and store a global object that is accesible to all nodes?

To be specific I want to (in a Java Snippet)

  • import a jar
  • initialize an object from that jar
  • and then make that object available to all nodes

How do you envision other nodes accessing this object? (e.g how would you have the Column Filter node access the object?) Or do you mean a specific subset of nodes that you want to access the object?

well the object is to be used by multiple Java Snippet nodes - I don’t want to output the object, I just need it to transform some data. But the creation of the object is costly so I should only do it once in the lifetime of the workflow.

I’ve done this sort of thing before by writing a custom Eclipse plugin, if you want to go that route.

I’m not familiar with how the Java Snippet node does class loading, but i take it that you’ve tried having a class in that jar you’re importing be a singleton, affecting that singleton in Java Snippet node 1 and then downstream seeing whether that singleton is the same instance affected by node 1?

Beyond that, in a pinch you could do some seriously hacky things like using the Eclipse IPreferenceStore to keep a ‘global’ object (if its really an object, as opposed to some values which could be represented as strings - you’ll need to do object serialization into and out of the preference store.)

Do you mean create my own node using the eclipse extension?

Here is more detail I should have provided in my post, the code looks like this - trying to use a .net object (I think Javonet is a singleton)

This works fine in a single Java Snippet node by the way.

//custom imports
import com.javonet.Javonet;
import com.javonet.JavonetException;
import com.javonet.JavonetFramework;
import com.javonet.api.NObject;

//custom code
Javonet.activate(“email”, “key”, JavonetFramework.v40);
Javonet.addReference(“D:\ClassLibrary.dll”);
NObject classLib = Javonet.New(“ClassLibrary.ClassLib”);
String value = classLib.invoke(“ReverseValue”,“Hello World”);

Javonet for sure only needs to be activated once, the references added one time as well.

I think you are likely to have to do this in your own node implementation. I think that the java snippet compiles the snippet code into an executable jar - one per node, and so singleton or not, you are going to recreate in each instance of the snippet, although I may be wrong on that.

It is possible to serialise objects to the knime workspace in your own node, which might also be a way to go.

Steve

I meant doing an Eclipse plugin, but not necessarily a KNIME node, depending on what you were trying to do.

I think you’re implying in the above cases, that a subsequent Java Snippet node executed after the JS node which enters the license information for Javonet, finds Javonet unlicensed? Aside from being gross, is there a significant time or processor or other penalty to having to do that activation in each JS node?

Every Java Snippet node has its own classloader. Therefore sharing data among several nodes will not work that way. And this is actually a good thing otherwise you could run into all kinds of nasty side-effects.

1 Like