Reputation: 2531
I'm a newbie in jave ee. I need the following behaviour. I've got an queue of registered users. The user on the head of queue can visit the page A 10 min. After 10 min he should be redirected to another page B and removed from queue. If this user makes logout/closes browser he should be removed from queue also.
I cannot make redirect to page B if user stays at the page A 10 min.
I'm trying to make asynchronous timer which makes redirect to B:
On the page A:
<h:body>
#{CheckBean.runTimer()}
...
This is CheckBean:
public class CheckBean {
@EJB
ActiveUserTimer timer;
...
public void runTimer() {
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext externalContext = context.getExternalContext();
timer.run(externalContext, System.currentTimeMillis());
...
EJB timer:
@Stateless
public class ActiveUserTimer{
@Asynchronous
public void run (ExternalContext externalContext, long time) {
while (true) {
if (System.currentTimeMillis() - time > 10000 || someFuncDetectedUserIsOffline()) {
externalContext.redirect("../public/index.xhtml");
Thread.sleep(1000);
}
}
}
The issue is when I pass externalContext to run() method externalContext is not null but the externalContext.requestParameterMap value is:
Exception occurred in target VM:
java.lang.NullPointerException: at com.sun.enterprise.web.pwc.connector.coyote.PwcCoyoteRequest.getFormHintFieldEncoding(PwcCoyoteRequest.java:225) at com.sun.enterprise.web.pwc.connector.coyote.PwcCoyoteRequest.setRequestEncodingFromSunWebXml(PwcCoyoteRequest.java:188) at com.sun.enterprise.web.pwc.connector.coyote.PwcCoyoteRequest.getCharacterEncoding(PwcCoyoteRequest.java:129) at org.apache.catalina.connector.Request.parseRequestParameters(Request.java:3114) at org.apache.catalina.connector.Request.getParameterNames(Request.java:1282) at org.apache.catalina.connector.RequestFacade.getParameterNames(RequestFacade.java:438) at com.sun.faces.context.RequestParameterMap.getEntryIterator(RequestParameterMap.java:126) at com.sun.faces.context.BaseContextMap$EntrySet.iterator(BaseContextMap.java:166) at com.sun.faces.context.BaseContextMap$BaseSet.size(BaseContextMap.java:154) at java.util.Collections$UnmodifiableCollection.size(Collections.java:996) at java.util.AbstractMap.size(AbstractMap.java:67) at java.util.Collections$UnmodifiableMap.size(Collections.java:1276) at com.mycompany.hib.ActiveUserTimer.run(ActiveUserTimer.java:29) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) ...
I looked at Quartz but I suppose I've got the exact issue if I would pass externalContext instance. Is there any other ways to make it?
Upvotes: 0
Views: 1308
Reputation: 85779
This task doesn't require any server timer at all. You can do it using plain JavaScript. Check the setTimeout
function in order to solve your problem.
Knowing this, you can do something similar to
<script type="text/javascript">
setTimeout(function() {
document.getElementById('myForm:btnContinue').submit();
}, 10*1000*60);
</script>
<h:form id="myForm">
<!-- page content... -->
<h:commandButton id="btnContinue" value="Continue" action="#{bean.continue}" />
</h:form>
Upvotes: 2