0

I have java class in JSF which act as a Scheduler and it runs every five seconds in background to perform some updates in DB.

since it is not declared as a bean in faces config or via annotation, i am not able to get session using

facesContext.getExternalContext().getSession(false);

Class scheduler{
    public void Scheduler(){
        HttpSession session = getSession();  // throughing null pointer    Exception.
    }
}


private HttpSession getSession()
{
    FacesContext facesContext = FacesContext.getCurrentInstance();

    HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);

    return session; 
}

How can I get an object of session in this Scenario ?

A.L
  • 10,259
  • 10
  • 67
  • 98
Bifrost
  • 417
  • 5
  • 23
  • Hi Balu, above link does not address my problem because in order to get session Object first you must have FacesContext's object in hand. I have read your article on creating FacesContext object inside doFilter() method, but again for that you must have objects of request and response. but in my class i dont have any thing at all. – Bifrost Sep 21 '15 at 08:37
  • Hi Balu, could you please add a code snippet w.r.t my code above to get an object of session via facesContext. Also, Work work = (Work) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("work"); i didn't get one thing that where "work" has been set ? – Bifrost Sep 21 '15 at 09:10
  • Hi Balu, still i am getting null pointer Exception, could you please modify above code to make it work ? – Bifrost Sep 21 '15 at 09:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90210/discussion-between-lokendra-lovely-and-balusc). – Bifrost Sep 21 '15 at 09:54
  • hi Balu, my scheduler is an independent file. As soon my application starts , it starts processing records. i guess your answer is applicable when my scheduler is a part of backing bean. – Bifrost Sep 21 '15 at 10:47

0 Answers0