Create a new java interface proxy.
This allows you to implement java interfaces in javascript.
Pass an object as the second argument with the names of the
methods you want to implement as keys and the implementations
as values in order to expose these methods to the java process.
Any arguments will be converted to javascript values and
return values will be converted to java values.
When the java process tries to call any method which is
not implemented by the proxy, an error will be thrown.
// Note: You can't do something like this: // runnable.run();
// Pass the proxy to a java method instead: constThread = importClass('java.lang.Thread'); constthread = newThread(runnable); // <- Pass the proxy here
// NOTE: You don't have to call this asynchronously // as this call instantly returns. thread.startSync();
constfunc = newProxy('java.util.function.Function', { // Any parameters and return types will be automatically converted apply: (str: string): string=> { returnstr.toUpperCase(); } });
// Import the string class constJString = java.importClass('java.lang.String'); conststr = newJString('hello');
// Pass the proxy. // NOTE: You must call this method async otherwise your program will hang. // See notes for more info. consttransformed = awaitstr.transform(func);
Any exceptions thrown by the proxy will be converted to java exceptions
and then rethrown in the java process. This may cause the exception
to again be rethrown in the javascript process.
When calling a java method that uses an interface defined by this, you must call
that method using the interface asynchronously as Node.js is single threaded
and can't wait for the java method to return while calling the proxy method at the
same time.
If you still want to call everything in a synchronous manner, make sure to enable
running the event loop while waiting for a java method to return by setting
JavaConfig.runEventLoopWhenInterfaceProxyIsActive to true.
This may cause application crashes, so it is strongly recommended to just use async methods.
If you want to keep the proxy alive, you must keep this instance in scope.
If that is not an option for you, you can manually keep the proxy alive
by setting the InterfaceProxyOptions.keepAsDaemon option to true.
// 'proxy' will eventually be garbage collected, // but it will be kept alive due to this option. executor.scheduleAtFixedRateSync(proxy, 0, 1, TimeUnit.SECONDS);
This will keep the proxy alive internally, thus the instance can be moved
out of scope. However, this will also keep the JVM alive, so you should
only use this if you are sure that you want to keep the JVM alive.
If you want to destroy the proxy, you must call clearDaemonProxies.
This will destroy all proxies which are kept alive by this option.
Calling JavaInterfaceProxy.reset will not destroy a proxy
kept alive by this option unless the force option is set to true.
Create a new java interface proxy. This allows you to implement java interfaces in javascript.
Pass an object as the second argument with the names of the methods you want to implement as keys and the implementations as values in order to expose these methods to the java process. Any arguments will be converted to javascript values and return values will be converted to java values.
When the java process tries to call any method which is not implemented by the proxy, an error will be thrown.
Examples
Implement
java.lang.Runnable
Implement
java.util.function.Function
to transform a stringWhich is equivalent to the following java code:
Throwing exceptions
Any exceptions thrown by the proxy will be converted to java exceptions and then rethrown in the java process. This may cause the exception to again be rethrown in the javascript process.
Notes
Possible deadlock warning
When calling a java method that uses an interface defined by this, you must call that method using the interface asynchronously as Node.js is single threaded and can't wait for the java method to return while calling the proxy method at the same time.
If you still want to call everything in a synchronous manner, make sure to enable running the event loop while waiting for a java method to return by setting JavaConfig.runEventLoopWhenInterfaceProxyIsActive to true. This may cause application crashes, so it is strongly recommended to just use async methods.
Keeping the proxy alive
If you want to keep the proxy alive, you must keep this instance in scope. If that is not an option for you, you can manually keep the proxy alive by setting the InterfaceProxyOptions.keepAsDaemon option to true.
This will keep the proxy alive internally, thus the instance can be moved out of scope. However, this will also keep the JVM alive, so you should only use this if you are sure that you want to keep the JVM alive.
If you want to destroy the proxy, you must call clearDaemonProxies. This will destroy all proxies which are kept alive by this option. Calling JavaInterfaceProxy.reset will not destroy a proxy kept alive by this option unless the force option is set to true.
See also