问题描述
尝试找到一种在Worklight Server中执行常规管家的方法。简化方案:
Trying to find an approach to perform some regular house-keeping in a Worklight Server. Simplified scenario:
我们有一个适配器与后端系统通信。当用户使用Worklight进行身份验证时,他们会创建一些在每次服务调用时传递给后端的凭据。如果不使用一段时间,这些凭证可能会变得陈旧。因此,我们想要的是所有活动会话的心跳。我有一个单独的Java对象,当用户进行身份验证时,我会在其中存储凭据,我想要做的是使用某种后台工作线程来迭代凭证列表并对服务器进行心跳调用。
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
}
任务离子是:我们如何触发心跳。我尝试过使用几乎可以工作的Java TimerTask。我可以安排Java TimerTask调用我的心跳。问题是,当在TimerTask下运行时,我们没有正常的Worklight Server环境,对WL.Server.invokeProcedure()的调用抛出异常,并且考虑到这一点,我似乎不太可能访问正常的Worklight API实际上是一个外国线程。
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.
我们正在使用Worklight 6.1,在WebSphere Liberty服务器上进行部署。目前我能想到的最好是编写一些外部迷你应用程序或shell脚本,定期调用heartbeat()方法。
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.
有更好的建议吗?
推荐答案
我不确定心跳后端是个好主意,而且这听起来像是一个可能的安全漏洞。您应该配置WL服务器和后端会话超时。
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.
至于实际问题的答案 - 您可以使用EventSource创建后台任务。请参阅此处 -
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
这篇关于Worklight Server中的后台工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!