This can be accomplished by putting the Map in a Singleton and accessing this singleton from the beans. That way there is a single instance for all the EJB instances (since they share the same classloader). Different Session beans in different EAR's would not work though as they would each have their own classloader, but that doesn't appear to be your scenario.
This can be accomplished by putting the Map in a Singleton and accessing this singleton from the beans. That way there is a single instance for all the EJB instances (since they share the same classloader). Different Session beans in different EAR's would not work though as they would each have their own classloader, but that doesn't appear to be your scenario.
Your existing usage of ConcurrentHashMap will sufficiently handle most of your cases but you still need synchronization around the addAnimal method since you need the map to be consistent between the two method calls.
New EJB 3.1 spec provides new type of beans - Singleton beans - that are exactly what you need. The only implementation with partial support of EJB 3.1 I know of is Apache OpenEJB 3.1. It includes Singleton beans but I haven't used them yet.
This is a stateless session bean. As the name implies you should not hold any kind of state in such a bean. A EJB container may work with multiple instances of your bean to satisfy concurrent requests.
How do you synchronize data of the Lists of all instances? I thing there is no way to do this. Mainting such a List into a session bean, which provied the business logic, even violates the concept of the business layer in my opinion.
You have to take care of transactions and concurrent write by your own. Why don't you hold the List of animals on the client side? I thing this would be the better way.
I agree with violating the concept of business layer. That is why, ideally, I would be able to inject the Map as a resource of some sort. I need the ability to have the Map on the Server side because I am shoehorning a Java SE application into a Java EE context.
– dubdubdubdot Mar 11 '09 at 21:47 An evil idea: Use a static class with a static Map that is used by all session beans and hope synchonization of the Map is well implemented and does not infuence the container doing his work. – Timo Mar 11 '09 at 21:55.
I think the best think would be to move the data structure out of the EJB completely and store it as a static data structure. You are probably correct in the fact that your objects are being erased because the EJB's are being pooled. Note depending on how you implement access to the datastructure you many need to synchronize access to ensure thread safety.
If you want to maintain state inside your EJB then consider looking at stateful session beans. Also if you did want to see when the beans were being destroyed you could override the ejbremove method I forget the annotation for Java 5. Karl.
Thanks. Stateful Session beans are only for (as I understand it) maintaining state with a specific client, not for the whole app server. – dubdubdubdot Mar 11 '09 at 21:44 Yes your correct, sorry should have made that clear.
– Karl Mar 11 '09 at 22:30.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.