Java Reference
In-Depth Information
In this listing the cleanup() method is marked to handle close events . It's important
to realize that when the method annotated with @OnClose is being invoked, messages can
no longer be sent to the client. The connection has been indeed closed. Furthermore, if the
Session has been cached and is being used asynchronously, invoking methods to send
data may fail. Code might be in the process of sending data to the client when the connec-
tion is closed by the client or on the server. Be prepared to handle race conditions where
the connection has been closed (on the remote side) but the @OnClose method has yet
to be invoked. Liberal use of the synchronized keyword isn't the solution—only care-
fully handling errors and ensuring that code is thread-aware can ensure that the application
functions correctly.
The @OnMessage annotation is used to decorate the methods that will process incoming
messages. Multiple methods in an endpoint can be decorated with the @OnMessage an-
notation. Each one must process a different data type. For example, you can't have two
@OnMessage annotated methods that handle a String ; how will the container know
which method should be invoked?
The @OnMessage annotation is defined as follows:
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD})
public @interface OnMessage {
public long maxMessageSize() default -1L;
As you can see from its definition, the annotation has only one parameter and that is the
maximum message size. By default, there's no limit to a message size. But in practice, the
maximum message size, set in bytes, should always be configured to guard against either
bad client code or hacked clients that attempt to overwhelm the server with data. When a
message arrives that exceeds the limit, the connection will be closed and a close code of
1009 (too big) returned to the client.
The data type the method can handle is broken down into three types: text data, binary data,
and pong messages. These are broken out in table 14.1 .
Search WWH ::

Custom Search