WebSocket Partial Message Handling | Java EE 7 Application Developer Exam

Receive Partial Messages in WebSocket | 1Z0-900 Exam

Question

You need to exchange large binary messages using chunks in a WebSocket application.

Identify two ways in which you can receive partial messages.

(Choose two.)

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

CD.

https://abhirockzz.gitbooks.io/java-websocket-api-handbook/content/Receiving%20Messages.html

Sure, I'd be happy to provide an explanation!

WebSocket is a protocol that enables two-way communication between a client and a server over a single, long-lived connection. It allows for real-time communication and is commonly used in applications that require frequent data updates, such as chat applications, online games, and financial trading platforms. When transmitting large binary messages in a WebSocket application, it may be necessary to send the message in multiple parts or "chunks" to avoid overwhelming the network or the receiving endpoint.

To receive partial messages in a WebSocket application, there are two ways to accomplish this:

  1. Use a ChunkListener interface implementation - this interface allows the application to receive notification when a chunk of a message is received. The ChunkListener interface provides a single method:
java
void onChunk(ByteBuffer buffer, boolean last);

The buffer parameter contains the data of the received chunk, while the last parameter indicates whether this is the last chunk of the message. By implementing the ChunkListener interface, the application can receive partial messages and assemble them into a complete message when all the chunks have been received.

  1. Use a MessageHandler.Partial<ByteBuffer> interface implementation - this interface allows the application to receive partial messages as a ByteBuffer. The MessageHandler.Partial interface is a generic interface, where the type parameter specifies the type of data being received. The interface provides a single method:
java
void onMessage(T partialMessage, boolean last);

The partialMessage parameter contains the data of the received chunk, while the last parameter indicates whether this is the last chunk of the message. By implementing the MessageHandler.Partial<ByteBuffer> interface with the ByteBuffer type parameter, the application can receive partial messages and assemble them into a complete message when all the chunks have been received.

So, to summarize, the correct answers to the question are B and C:

B. Use a ChunkListener interface implementation. C. Use a MessageHandler.Partial<ByteBuffer> interface implementation.

Option A, "Define an @OnMessage method with a single MimePart parameter", is incorrect because there is no such parameter in the WebSocket API.

Option D, "Define an @OnMessage method with byte [] as the first parameter and a boolean as the second parameter", is incorrect because this method signature does not allow for the receipt of partial messages.