Class instance and Threading question when using node-java

76 views
Skip to first unread message

O haya

unread,
Mar 11, 2017, 11:05:54 PM3/11/17
to node-java
Hi,

I am using node-java in a Node/Express web app to invoke a Java class that is kind of a "backend".  

The node-java is used in an "app.post(), and in there, I:

- Use "java.newInstanceSync("myClass")" to instantiate my Java class, then 
- User "java.callStaticMethodSync(myClass", "methodname",...) to call the methodname() method in the "myClass" class

That all seems to work, but I am kind of unclear about something:  Since the java.callStaticMethodSync() uses the string name of the class (i.e., "myClass"), and if I have instantiated the myClass class a bunch of times, exactly which one of those myClass instances is the java.callStaticMethodSync() actually going to be calling/using?

I think that I can see that when I do the newInstanceSynch() it is actually creating a new instance, because the return value has a different reference each time I create a new instance, but again, since the callStaticMethodSynch() only identifies the class by the string name of the class, I don't understand which actual instance it is invoking?


The main reason I am asking these questions is that I also have coded using non-synch calls (callStaticMethod() instead of callStaticMethodSynch()), but I haven't actually started using that code yet, because I am worried about the the above questions.

Thanks,
Jim

P.S.  I hope I am being clear with my question.  This is a kind of complicated situation, so if I have not, then please let me know.

Joe Ferner

unread,
Mar 12, 2017, 10:06:14 AM3/12/17
to node-java
To clean up the code you might want to do this instead...

Starting at the top of the file
var java = require('java');
var MyClass = java.import('myClass');
...
app.post('path', function(req, res) {
   var myClassInstance = new MyClass();
   myClassInstance.methodName('arg1', 'arg2', function(results) {
      res.send(results);
   })
});

O haya

unread,
Mar 12, 2017, 3:11:58 PM3/12/17
to node-java
Hi Joe,

I am really glad to see you responding!

Thanks for that!  Can you tell me if there would be a synch-style version of what you suggested, and if so, what would that look like?

Thanks again!

Jim

P.S.  Is there going to be an update of the node-java module?

O haya

unread,
Mar 12, 2017, 3:15:53 PM3/12/17
to node-java
Also, with your example, the myClassInstance var gets a new/different instance (or rather a reference to a new/different Java class instance) each time the app.post() gets invoked (i.e., each time a POST request comes in)... 

Is that correct?

O haya

unread,
Mar 13, 2017, 10:51:57 PM3/13/17
to node-java
Hi Joe,

I tried changing to what you suggested, but I am getting a TypeError.


This (the original approach) works:

var MyPdpClassStartup = java.newInstanceSync('ThirdPdp');

var testRequest = "XXXXXXXXXXXXXXXXXXXXX";

var responseToStartupRequest = java.callStaticMethodSync("ThirdPdp", "DoRequest", testRequest, "0000000", myDebug);



But, this doesn't work:

var myThirdPdpClass = java.import("ThirdPdp");
.
.
app.post('/zzzz/yyyy', function (req, res) {

var stringRequestMarker = ("000000" + requestMarker++).slice(-6);
newMyPdpClassInstance = new myThirdPdpClass();
var x = newMyPdpClassInstance.DoRequest(req.body, stringRequestMarker, myDebug);

The error I get is:  TypeError: newMyPdpClassInstance.DoRequest is not a function


Also, FYI, if I output the "newMyPdpClassInstance" variable in a console.log after the "new", I get the value:  ThirdPdp@2fc07784


So it seems like it is getting a reference to the instance (the "ThirdPdp@2fc07784"), but it doesn't seem to think that instance has a method named "DoRequest"?

Also, as you can see, I think the number of parameters in the call to the DoRequest() method is the same (3 string params).  

Also, FYI, I have tried with both .DoRequest() and .DoRequestSync() and get the same error.


I also tried with another function in that class (.timestmp()) that takes no params and returns a string, and that gave a similar TypeError.


How can I debug this further?

Thanks,
Jim


O haya

unread,
Mar 13, 2017, 11:14:16 PM3/13/17
to node-java
Hi,

I don't know if this helps or not, but I tried calling the .toString() method, and the .getClass() method on the instance, and those worked.  But when I tried .getMethods(), I got the same TypeError.

Jim
Reply all
Reply to author
Forward
0 new messages