Please remember that the HTTP session is not synchronized. Thus, using the session.put is a critical method. Most likely, what you really want to do is counter = session.get("counter"); counter.increment (or incrementAndGet) In this way you ensure exactly the same object is accessed and incremted / consumed.
On Wed, Apr 25, 2012 at 4:03 PM, Gabriel Belingueres <belingue...@gmail.com>wrote: > The "S2 way" of accessing the session is implementing the SessionAware > interface. > > First you must realize that what you are putting on session scope is > not the AtomicInteger per se, but its current value at the moment you > call the first action, so unless you call the first action again, the > value stored in session will NOT change. > The second action does not change the value in session, just print > what was already stored. > > When you said that after some time, the results are "stabilized", I > guess that you are requesting the first action after no other request > is running so you get the current value again and then call the second > action serially, giving you the same value. > > Also, you have concurrency issues if the first action: > > HitCounter.increment(); > ActionContext.getContext().getSession().put("counter", > HitCounter.getAtomicCounter()); > > you increment without returning the new value. Instead you call the > getAtomicCounter() method, which will cause interference between > threads (ie not thread safe), if the point that every session have a > different counter value, then you must change the increment method > like this: > public static int increment() { > return counter.incrementAndGet(); > } > and use that value to store it in session (no call to getAtomicCounter()). > > HTH, > Gabriel > > 2012/4/25 Dionis Argiri <dio...@gmail.com>: > > Hi! > > > > I have some troubles (with understanding?) how session works. > > > > For example, I have two actions: First and Second. > > > > I have written simple utility, that uses executor service to send 100000 > > asynchronous requests to action Second. > > > > In First action (which is called only from my browser) I do: > > > > HitCounter.increment(); > > ActionContext.getContext().getSession().put("counter", > > HitCounter.getAtomicCounter()); > > return Action.SUCCESS; > > > > In second action I do: > > > > System.out.println("From session: > > "+ActionContext.getContext().getSession().get("counter")); > > System.out.println("Actual:"+ HitCounter.getAtomicCounter()); > > return Action.SUCCESS; > > > > And the output I see(and it really makes me mad): > > > > From session: 2 > > Actual: 69352 > > > > After some time when I use this Fitst action/Second action only from my > > browsers and no concurrent requests come(generated by my load utility), > > results "are stabilized" to actual values. Thus, it seems that I have > > concurrency issues with session or I'm missing some points. > > > > Is there a standard way that I should use to solve this problem, when > using > > Struts2 ? > > > > P.S. HitCounter realisation: > > > > public class HitCounter { > > private static AtomicInteger counter = new AtomicInteger(0); > > > > public static void increment() { > > counter.incrementAndGet(); > > } > > public static int getAtomicCounter() { > > return counter.get(); > > } > > } > > > > BR, > > > > Dionis > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscr...@struts.apache.org > For additional commands, e-mail: user-h...@struts.apache.org > >