Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there not a single example of java + nodejs interop? #842

Open
balayanv opened this issue Sep 3, 2024 · 7 comments
Open

Is there not a single example of java + nodejs interop? #842

balayanv opened this issue Sep 3, 2024 · 7 comments

Comments

@balayanv
Copy link

balayanv commented Sep 3, 2024

I understand that one can simply run unmodified node projects with this, that's nice but most people would look at graal so they can use node modules out there from java.

I remember doing all sorts of crazy hacks, setting up shared task queues because node has its event loop and you cant just call into it without being on that thread...

Is there not even a single example of java interacting with a simple node code? call node code from java and vice versa...

I remember also having to launch my code with custom shell scripts because the executable is node and intellij cant understand that its actually a fully powered jvm that runs node... so i had to transform, manually grab classpaths feed them into node command properly so i can just launch the app with graalnodejs (loosing any capability to debug stuff at the same time)...

This was a while ago, so maybe there is a better way to do things or i was doing some unnecessary magic to begin with? either way it was bad dev experience...

@pbk20191
Copy link

If you are using intellij it is quite easy to launch
image

intllij provides $ClassPath when you attach Build on Before Launch tab.
I also I think, I can attach debugger on jvm to if i can get the path of java-agent of intellij but can't find it yet.

In this image I'm using kotlin the top level class is just `Main3.
It can be any other classes. e.g) com.example.Runner ...

@balayanv
Copy link
Author

Oh i see i was going trough different route trying to fool intellij into treating it as a jvm...

@pbk20191
Copy link

pbk20191 commented Oct 21, 2024

for me the best way to hop thread between node.js and java thread. I create node.js binding which create ThreadSafeFunction
and pass as ExternalWrapper.

but somehow when I reference externalwrapper from java side.
JVM reference retain the native reference of the main_loop's uv_async_t.
So memory leak happens #817 #819

But I hope this will be solved in GraalJS 24.2.0 which provides EventLoopExecutor!

@balayanv
Copy link
Author

@pbk20191 thanks for the replies, you'd do me a huge favour if you could share some sample code doing java <-> node interop
i remember diving into this mess years ago, now i was revisiting the project cos i have some new ideas but i dont remember much from my findings. I do remember how annoying it was to get async code work without node runtime though.

Either way i know you dont have to do it, you dont seem to be even from oracle, its just odd that they have no sample code for this at all...

@pbk20191
Copy link

pbk20191 commented Oct 26, 2024

repo.zip

unpack the file and clone the git-bundle like below

unzip repo.zip
git clone repo.bundle

I only tested it using Intellij.
You have to specify GraalNodejs path in running configuration.
Also run npm install in the project path.
Since you need to build native-nodejs-extension.

You have to check several settings carefully.
I hope this could be helpful ( though, in my opinion I will wait for graal 24.2.0 for convenience)

there is two main entry Point

  1. Main
    -> supports only window

  2. Main2
    -> use node_thread_safe_function so It should works in most platform

@balayanv
Copy link
Author

thanks, main 2 is what i wanted
appreciate the help

@pbk20191
Copy link

@balayanv
I realized that I forgot to add some fix. I re uploaded the zip file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants