Java Reference
In-Depth Information
Programmatic endpoints simply give you control over the registration of message handlers.
There are two reasons for using programmatic endpoints:
• Separate the message handler from the endpoint implementation.
• Register different message handlers depending on session information.
For the first reason, separating the endpoint implementation and message handler enables
reuse of the message handler. There's not necessarily a one-to-one relationship between an
endpoint and a message handler. An endpoint handles the lifecycle events and is bound to
a URL. Consider the case of the ActionBazaar chat service—it makes use of two separ-
ate endpoints, one for the bidder/seller and another for the CSR. In both cases the message
handler delegates the incoming message to the SupportServer . The SupportServ-
er is a singleton bean that routes the message to the correct recipient. Because the message
handler logic is the same for both endpoints, it doesn't make any sense to have two differ-
ent implementations.
The second reason for using programmatic endpoints is more complicated. You may want
to use different message handlers depending on parameters passed in when the session
is opened. For example, you may support a type parameter on the connection string
that's used to control the protocol used. A type of Java would result in serialized Java
objects being exchanged instead of the default JSON. So the URL http://actionbazaar/
chat?type=Java would result in a message handler being used that would accept
java.io.InputStream / byte[] and deserialize to Java objects. Other possibilities
abound.
Understanding message handlers
Understanding the javax.websocket.MessageHandler interface is key for using
programmatic endpoints. You don't actually implement this interface directly; instead you
implement one of its inner interfaces. There are two inner interfaces: Partial or Whole .
As their names suggest, you can either wait and process the whole message or defer until
the whole message is available. Your choice of which interface to use depends on how you
intend to process the data. Are you accepting a stream of data such as an image or video or
a discrete message such as an update in an online video game? The definition of the Mes-
sageHandler interface is shown in the following listing.
Search WWH ::




Custom Search