Java Reference
In-Depth Information
Listing 14.12.
MessageHandler
interface
In this listing you can see the two subinterfaces that can be implemented. Both of these
subinterfaces have an
onMessage
method. The interface uses Java's Generics so that you
can be certain about the type to be passed to the implementation. If the type is a custom
Java object or domain object from the application, then the decoder registered on the end-
point will be used. The partial handler interface includes a flag denoting whether the chunk
of data passed in is complete.
There are some drawbacks to implementing the
MessageHandler
interface. Although
we haven't covered the
@OnMessage
annotation yet, with the
@OnMessage
annotation
you can optionally receive a
javax.websocket.Session
instance along with the
data. You'd use the
Session
object to send data back to the client. Also, because a
Mes-
sageHandler
isn't a managed object, you can't inject references to EJBs, entity man-
agers, and the like into it.
The code for the
ChatMessageHandler
is shown in the listing that follows. This mes-
sage handler is used both by the endpoints bidder and seller and by the CSR.