0

I am fairly new to Java EE. I was going through CDI and EJB material and came across @SessionScoped and @Stateful annotation.

Looking at the definition : @SessionScope: A user’s interaction with a web application across multiple HTTP requests is maintained.

@Statfeful : looks like it has the same functionality as @SessionScope

So here are my doubts :

1) Do both of them serve the same purpose ? 2) I have a bit of experience with Node and Python. In those languages for maintaining client state I always used a cookie which is tied to an HTTPSession object which is further tied to an in-memory cache(redis/memcache) for distribution across all the servers.

So I can use the same technique here as well... correct ? Then why should I use these notations ? Also, if I plan to use them, then how to make them distributed across all servers? I mean a request from client can come to server1 and then next request can go to server2 ..in that case if this SesssionScoped object is not distributed then how will things work correctly ?

What is the purpose of these annotation ?

JackSparrow
  • 187
  • 1
  • 2
  • 15

2 Answers2

0

No, they're not the same, but they can be used together.

@SessionScoped binds an object to an HTTP Session. @Stateful Session Beans simply mean that their state can be maintained over invocations from a single caller. Stateful Session Beans came from heavy client apps, ones in which a Java SE program may look up an EJB on a remote server to do work. Those types of client/server apps might use a Stateful EJB to maintain some amount of session state on the server, while not requiring HTTP (they used protocols like RMI-IIOP, CORBA on the application servers).

These days, you can continue to use Stateful Session Beans, with an HTTP Session.

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • @sessionScope binds an object to HTTP session ...can you please explain on this ? If my sessionScoped object is not distributed then how will this work ? And how is this binding done ? Previously, I used cookies to bind my http request to http session object (in node.js) – JackSparrow May 10 '15 at 17:50
  • I mean any object that intent to hold client state has to be distributed across all app serves , isnt it ? – JackSparrow May 10 '15 at 17:52
  • What do you mean not distributed? I think you're confusing clustering and http sessions. – John Ament May 11 '15 at 10:42
  • I mean, lets say I am holding user's data in a SessionScoped bean . My app is deployed on 2 servers with a load balancer in between. Now a client makes a request and it hits server1 , here we instantiate a SessionScoped bean to hold his session data. Now he makes another request and it goes to server2 . In this case there is no state present in server 2, now what ? We generally use http session object in such cases and these session object are maintained in in-memory keyvalue store like cohorence/redis etc ... – JackSparrow May 11 '15 at 12:18
  • you definitely are confusing clustering and sessions. clustering is an option that allows you to share data between two nodes. – John Ament May 11 '15 at 16:38
  • Actually I am unaware of clustering :( Does clustering allow you to access stateful EJB create on one box(holding state of a client) from a different box (on the consecutive request from the same client)? – JackSparrow May 11 '15 at 17:37
0

After having a word with my teammate, I think I have a possible answer to my question.

The use of sessionScope or stateful can be harnessed in case of sticky sessions. Sticky and NON-Sticky sessions

In this case a user session sticks to one box only and is not distributed across all the boxes. But if we are not using sticky sessions, these annotations are pretty much useless

Community
  • 1
  • 1
JackSparrow
  • 187
  • 1
  • 2
  • 15