I'm sorry for this that could be a silly question for experts but I wonder if it is possible to exchange messages between session bean instances.
I have two users (A and B) that are logged in to my web site. A user bean is used as a session bean containing the name and other personal information gathered from a database.
- first question: is there a way to know if an user is logged without quering in the DB? I would like to list all the users logged-in. Is there a pattern for this case?
- second questions: From the list above the user A can see that the user B is also logged-in. Clicking in the name of B listed in the page, the user A would like to send a message to B. B will find all the messages addressed to him in a special jsf page. Is there a way to do that without storing the message in the DB as the message is valid only if B is logged-in? I was thinking about a list of messages inside the user bean(every instance has its own list populated by other user bean instances). Is that the correct way to implement that? Any known patterns?
I think you don't have to acess the database but at least you have to access a kind of persistence layer such as hibernate. because the two sessions of A and B are stored in different threats so it could be difficult to communicate between 2 threats. so you would create a table in a database named message(ID,fromUserID,toUserID,Title,Content). the next you have to map the table into a java class and the work is done. now a hibernate session works parallel to the database and if you won't flush the session there would be no database accesses. now users can create messages that will be stored as javabeans in the hibernate sessions(take the method save() of the MessageDAO) if you want to access the messages you just have to implement a method in every user bean that will peform the findByToUserID() method and return a list of all the messages. remember if you dont call session.flush() the table message always stays empty.
I hope i could help you
posted 11 years ago
but how can I manage the fact that a user can log-out? In this case all the messages to him should be deleted.
A Java EE application is still a Java application. The difference between a normal Java application and a Java EE is only he fact that ONLY SOME of the instances of some classes (ejb annotated classes) benefits from the services provided by the EJB container.
Why I said that? Because you miss a simple and trivial solution. What we do in a normal applications? One probable solution is to use a Singleton. The same in Java EE.
I think you can create a class MessageManager which implements Singleton pattern. That instance would have a property like Map<String, Map><String, List><String>>>. The first String could be the target user name, second String would be sender user name, and the third one would be a message in queue. If a user sends a message to somebody, it should insert an entry here. As a sample if a send message x to b we would have messageQueue.get(b).get(a).add(x). Every user will read his messages periodically.
You could have also a Map<String, Date> like user time stamps. Every user when connects, will insert/update an entry in this map. Also, every connected user will update time stamp periodically. If the timestamp is older you can consider user away. If the time stamp is older enough you can remove the client from the map and consider it offline. Of course, you can give to a user the possibility to remove itself from timestamp map at sign out action.
Of course, you should synchronize access to these maps. Users comes from different threads.
I don't have the possibility to try this at home but I will give a shot maybe tomorrow and see.