Java Doc for Discussion.java in  » J2EE » Enhydra-Demos » chat » business » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Enhydra Demos » chat.business 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   chat.business.Discussion

Discussion
public class Discussion implements Runnable(Code)
This class manages the contents of a discussion. Messages may be added at any time. There is an option limit on the size of the message queue, if so the oldest messages are deleted as new ones arrive.

Optionally the method startHarvester() may be called, once, to start a background thread that enforces an age limit on messages. If this method is not called, then there will be no age limit.

This class implements what I call the "on hold push" algorithm. The server (this class) maintains some state (a list of messages). Clients (ContentsPresentation.po and the web browser) request snapshots of this state. If the state has changed since the last time the client asked for a snapshot, then a new snapshot is sent to the client right away. If the state has not changed since the last time the client asked for a snapshot, then the request for a snapshot blocks until the state changes (either by a new message being added, or messages being deleted). As soon as the client has processed the snapshot it gets, it immediatly requests a new snapshot.

This has the following advantages: As soon as new information is available, it is sent immediatly to all interested parties. Since a whole snapshot is sent every time, the problem of having to do "diffs" between the old and new states is avoided. As is the problem of how to recover when this mechanism breaks down. Only standard HTTP is used, so the method is compatible with firewalls, proxies, all browsers, and even, in the case of an applet, paranoid security policies. (HTTP requests are commonly blocked on a read in real life. But usually it is a filesystem read, and the delay is much shorter.) For an example of this, try running this application in the Enhydra Multiserver, and then add a WAI connection. Your requests are being tunneled via CORBA from the Netscape server to the Multiserver, yet the application runs fine. Because the interested parties all have already established a socket connection, and are poised and ready to read data, they recieve the new state the instant it changes, pretty much as quickly as is possible. It is simple, robust and effective. This has the following disadvantages: If you have N users, you will have N open socket connections. This method does not scale well to very large values of N. If N is too large, some users will experience network errors, and their automatic reloading will stop. See your operating system for details on the maximum number of open sockets, and the optional NumThreads setting for connections in the Enhydra Multiserver config file. The whole state is sent every time. This uses more bandwidth than methods that only send deltas to the state. If the clients stop asking for snapshots, there is no way (from the server) to do anything about it. In order to tell if a client has a current copy of the state of things, timestamps are used. Every time the state changes, a unique identifier is assigned to the new current state (using time). The important thing is that the identifiers uniquely identify their state. Time is just one way of generating unique identifiers. Every time the client asks for a snapshot, it also sends the identifier for the state it currently has (there must be a special identifier used for the first request, when the client has no data. In this program it sends 0). If the id sent with the request matches, then the request blocks until the state changes. In real life most connections to the server use TCP/IP, which imposes a timeout limit on how long requests can block on a read. More importantly, web browsers will give up after a short period of time. Therefore, when a request is sent in, in addition to the current id a time limit is sent. This tells the server the maximum number of seconds the client is willing to wait (in this program 60 seconds is used). If this time limit expires, and the state has not yet changed, the server must return an answer to the client. In this program a snapshot is returned, even though the browser already has the same data (if the client were an applet, a special "no change" response could be sent). If a client wants to force an update, or wants to do a non-blocking read for some other reason, then it simply sets the timeout to zero. Static methods are used because there is only one chat room. This program's main goal is to show off a simple yet "real" Enhydra application.



Field Summary
public static  intmaxQueueSize
    


Method Summary
public static  voidaddMessage(String name, String text)
     Add a message to the discussion.
public static  voidclear()
     Throw out all the current messages.
public static  SnapshotImplgetContents(long currentState, long wait)
     Get a snapshot of the current state.
public static  longgetCurrentSize()
    
public static  intgetNumWaiting()
     How may clients are blocked on a read? This is, effectivly, the number of people participating in the discussion.
public static  longgetTotalReceived()
    
public  voidrun()
     Internal use only.
public static  voidstartHarvester(int lifetimeSec, int intervalSec)
     Call this function (only once) if you want to start the harverter thread.
public static  voidstopHarvester()
    

Field Detail
maxQueueSize
public static int maxQueueSize(Code)





Method Detail
addMessage
public static void addMessage(String name, String text)(Code)
Add a message to the discussion. Any waiting clients will be instantly notified.



clear
public static void clear()(Code)
Throw out all the current messages. All the waiting clients will be immediatly notified.



getContents
public static SnapshotImpl getContents(long currentState, long wait)(Code)
Get a snapshot of the current state. Might block.
Parameters:
  currentState - The state identifier that was returned last time this was called.This is the id of the state that the client currently has. It isasking for a snapshot of the state after things change andthis id is not longer current. If this has already happened, thecall will immediatly return. If it has not happned, the call willblock (not return) until things change.
Parameters:
  wait - The maxmimum number of seconds this call is allowed to block for.Send 0 for an instant response. If the call blocks and thenruns out of time, a snapshot is returned.A Snapshot object. This is just a way to return two things atonce: a Vector of Message objects and a state identifier.The client should use the state identifier for the next call tothis method. The client should call this method again as soon asit is done displaying the results.



getCurrentSize
public static long getCurrentSize()(Code)
How many messages are there right now in the list?



getNumWaiting
public static int getNumWaiting()(Code)
How may clients are blocked on a read? This is, effectivly, the number of people participating in the discussion.



getTotalReceived
public static long getTotalReceived()(Code)
How many messages have been recieved in total?



run
public void run()(Code)
Internal use only. This is provided only for the thread that deletes messages that are too old.



startHarvester
public static void startHarvester(int lifetimeSec, int intervalSec)(Code)
Call this function (only once) if you want to start the harverter thread. It runs in the background and periodically deletes messages that are too old. Most of the time the thread is sleeping. If this method is not called, then no age limit on messages will be enforced.

The longest a message can live is lifetimeSec + intervalSec seconds (in the extreme case).

The default value for the interval is a little shorter than the default value for the browser's timeout. This is so that if the browser is left idle (and messages start being deleted), the refresh cycle will sync up with this threads interval, and fewer updates will happen (delete, delete, delete... instead of timout, delete, timeout, delete, timeout, delete...). It's not a big deal, but hey...
Parameters:
  lifetimeSec - How long should messages be kept for (seconds).




stopHarvester
public static void stopHarvester()(Code)



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.