Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Nashorn - invoking a callback function from Java

 
Ron McLeod
Saloon Keeper
Posts: 1286
143
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just starting working with Nashorn and things have been working well until I tried to make a asynchronous service - implmented in Java and called from a script.  My service receives a script function  which is to be called-back sometime later.  The function passed to my service is an anonymous function, and I can't figure-out how it can be called from Java.

I've spent an hour or so poking around at this, and I can see anyway to do it -- most likely my approach is wrong and I should be looking at doing this differently.  Any suggestions?

default.jstimerTest.js
Console output

 
Ron McLeod
Saloon Keeper
Posts: 1286
143
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working now - I exposed Runnable to the scripting environment and modified the TimerService#startTimer to take a Runnable for the timer handler rather than a script function.

default.jstimerTest.js
Console output

 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!