问题
Trying to find an approach to perform some regular house-keeping in a Worklight Server. Simplified scenario:
We have an adapter talking to a back-end system. When user authenticates with Worklight they create some credentials that are passed to the back-end on each service call. Those credentials can become stale if not used for a period of time. Hence what we want is a "heartbeat" for all active sessions. I have a singleton Java object in which I stash the credentials when the user authenticates, what I want to do is have some kind of background worker thread to iterate the list of credentials and make a heartbeat call to the server.
I end up with adapter methods like this
// in business service adapter
businessMethod(){
make service call using credentials from user's Worklight session
}
// in authentication adapter, normal adapter authentication methods and a heartbeat
authentication(){
get back-end credentials
store credentials in user's session
stash credentials in singleton
}
// how do we cal this heartbeat every x min
heartbeat(){
for each credential in singleton stash
make heartbeat call to server keeping credential alive
}
The question is: how do we trigger that heartbeat. I've tried using a Java TimerTask, which nearly works. I can arrange that the Java TimerTask will call my heartbeat. The problem is that when running under the TimerTask we don't have a normal Worklight Server environment, calls to WL.Server.invokeProcedure() throw exceptions, and thinking about this it's seems unlikely that I would have access to the normal Worklight APIs from effectively a foreign thread.
We are using Worklight 6.1, deploying on a WebSphere Liberty server. At present best I can think of is to write some external mini-application or shell script that periodically calls the heartbeat() method.
Any better suggestions?
回答1:
I'm not sure heartbeating backend is a good idea, moreover this sounds like a possible security hole. You should configure your WL Server and backend session timeouts.
As for an answer to the actual question - you can use EventSource to create background task. See here - https://www.ibm.com/developerworks/community/blogs/worklight/entry/configuring_a_polling_event_source_to_send_push_notifications?lang=en
回答2:
David, I'm not entirely sure if it address your question directly, as I'm not sure if you're aiming just for a heartbeat, or whether you want to do some custom work on the adapter layer when the heartbeat is received (can you clarify a little more about the underlying purpose?); but are you aware that Worklight itself has a built-in heartbeat concept? You configure it with the value heartBeatIntervalInSec as part of the call to WL.Client.init(). See the documentation for more info.
来源:https://stackoverflow.com/questions/23004632/background-work-in-worklight-server