1

I've seen this solution about sharing session for different applications in this topic: Any way to share session state between different applications in tomcat?

I´ve been able to register a JNDI bean and get it in different applications. But what I really want is to change a value of this bean (add a value to a hashmap) in one application and to retrieve the new value in other applications, but the change only seems to happen locally.

Is there any solution?

Thanks

Community
  • 1
  • 1
Fábio
  • 11
  • 3

2 Answers2

0

Have you tried to use the JNDI Context rebind method? This allows you to replace the currently bound object with a new one.

PJ Fanning
  • 953
  • 5
  • 13
  • This might be dangerous if the object is highly accessed and changed, isn't it? There is the possibility one aplication overwrite data that other application changed. – Fábio Oct 18 '11 at 18:27
0

Do you get the same bean in all apps or does the container create a new bean for each app?

If you can't get it to work, here is another workaround: Create another web app and use that to share state. Convert state into something that you can send easily to the app (XML or JSON).

If you're a bit careful (only use Input/OutputStreams and ISO-8859-1 encoding), it should also be possible to serialize Java objects directly and pass them to the server (with the usual caveats of serialization).

Lastly, you could set up an RMI server on the same machine which offers a map-like API to exchange data.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • I believe it is the same bean that is recovered. This is what I am doing. I've put this code inside the web.xml of both applications bean/SessionPool beans.SessionPool `Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); SessionPool bean = (SessionPool) envCtx.lookup("bean/SessionPool");` – Fábio Oct 18 '11 at 18:38
  • Make sure you get the same bean: Print the hashCode() or use a debugger to see the object ID. If the lookup creates a new bean every time, it won't work. – Aaron Digulla Oct 19 '11 at 12:55