Java Reference
In-Depth Information
As you can see from this definition, this annotation is rather simple and has no properties.
But looks can be deceiving, because the annotated method can accept several different
parameters in any order. The WebSocket container will use Java reflection to examine the
parameters and invoke the annotated method with the parameters you've requested. The
types of parameters that the annotated method may accept are as follows:
javax.websocket.Session— Newly created session for this connection
javax.websocket.EndpointConfig— Configuration for the endpoint in-
cluding the lists of decoders and encoders and also a map containing user proper-
ties
@PathParam annotated parameters—Map variables from the URI to method
parameters
The @OnOpen annotated method will be called only once when the connection is opened.
If you accept a Session object, you can cache this object so that you can asynchronously
send messages back to the client. In the case of the BulletinService , you cache the
Session object and then use it from another method to send back updates to the client.
This other method is monitoring a JMS queue for messages.
@OnClose
The @OnClose annotation is placed on a method that will be invoked when the connection
is closed by either the client or the server. Like the method annotated with @OnOpen , the
@OnClose method can take an optional set of parameters including the Session , End-
pointConfig , and parameters annotated with @PathParam . This callback enables re-
sources to be cleaned up and released. For example, in ActionBazaar the BulletinSer-
vice endpoint is registered as a JMS consumer. The onClose annotated method is used
to deregister the endpoint as a JMS consumer so that it can be garbage-collected. The code
from ActionBazaar is shown in the following listing.
Listing 14.16. Using @OnClose annotation
Search WWH ::




Custom Search