Java.proxy

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Java.proxy

Hugo <Nabble>
How can I get Java.proxy() to work? I thought the java() call on top should be enough to get the Java object available, but it is nil.
java()
local TimerTask = require "java:java.util.TimerTask"
local runTask = {}
runTask.run = function()
	-- do nothing
end
Java.proxy(TimerTask, runTask)

ERROR MESSAGE: attempt to index 'Java' (a nil value)
Reply | Threaded
Open this post in threaded view
|

Re: Java.proxy

fschmidt
Administrator
In theory, like this:
java()
local Runnable = require "java:java.lang.Runnable"
local TimerTask = require "java:java.util.TimerTask"
local runTask = {}
runTask.run = function()
	-- do nothing
end
Runnable.luan_proxy(runTask, TimerTask.new())
But this doesn't work because TimerTask doesn't have a public constructor.  In fact there is no reasonable way to proxy TimerTask in Java itself.  TimerTask is horribly designed because its design requires implementation inheritance.
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: Java.proxy

Hugo <Nabble>
So how would you implement a timer task that must be run every N minutes?
Reply | Threaded
Open this post in threaded view
|

Re: Java.proxy

fschmidt
Administrator
java()
local Runnable = require "java:java.lang.Runnable"
local Executors = require "java:java.util.concurrent.Executors"
local TimeUnit = require "java:java.util.concurrent.TimeUnit"
scheduler = Executors.newSingleThreadScheduledExecutor()
local runTask = {}
runTask.run = function() %>
ok
<% end
r = Runnable.luan_proxy(runTask)
scheduler.scheduleAtFixedRate(r,0,1,TimeUnit.SECONDS)
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: Java.proxy

Hugo <Nabble>
Thanks, this works. Closing thread.