public class AsyncMessageReader extends java.lang.Object implements MessageReader<XMessageHeader,XMessage>
MessageType msg = reader.readPayload(null, 0, ServerMessages.Type.THE_TYPE_VALUE);
readHeader()
method supports this use case. The caller will generally call the reader like so:
XMessageHeader header = reader.readHeader(); if (header.getMessageType() == ServerMessages.Type.TYPE_1_VALUE) { MessageType1 msg1 = reader.readPayload(null, 0, ServerMessages.Type.TYPE_1_VALUE); // do something with msg1 } else if (header.getMessageType() == ServerMessages.Type.TYPE_2_VALUE) { MessageType2 msg2 = reader.readPayload(null, 0, ServerMessages.Type.TYPE_2_VALUE); // do something with msg2 }
If the MessageReader encounters an Error message, it will throw a XProtocolError
exception to indicate that an error was returned from
the server.
All external interaction should know about message types listed in com.mysql.cj.x.protobuf.Mysqlx.ServerMessages.
TODO: write about async usageConstructor and Description |
---|
AsyncMessageReader(PropertySet propertySet,
SocketConnection socketConnection) |
Modifier and Type | Method and Description |
---|---|
void |
pushMessageListener(MessageListener<XMessage> l)
Queue a
MessageListener to receive messages delivered asynchronously. |
XMessageHeader |
readHeader()
Peek into the pending message for it's class/type.
|
XMessage |
readMessage(java.util.Optional<XMessage> reuse,
int expectedType)
Read message from server into to the given
Message instance or into the new one if not present. |
XMessage |
readMessage(java.util.Optional<XMessage> reuse,
XMessageHeader hdr)
Read message from server into to the given
Message instance or into the new one if not present. |
void |
start()
Start reading messages reader from the provided channel.
|
void |
stopAfterNextMessage()
Signal to the reader that it should stop reading messages after reading the next message.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getMessageSequence, resetMessageSequence, undecorate, undecorateAll
public AsyncMessageReader(PropertySet propertySet, SocketConnection socketConnection)
public void start()
MessageReader
start
in interface MessageReader<XMessageHeader,XMessage>
public void stopAfterNextMessage()
MessageReader
stopAfterNextMessage
in interface MessageReader<XMessageHeader,XMessage>
public void pushMessageListener(MessageListener<XMessage> l)
MessageReader
MessageListener
to receive messages delivered asynchronously.pushMessageListener
in interface MessageReader<XMessageHeader,XMessage>
l
- MessageListener
public XMessageHeader readHeader() throws java.io.IOException
readHeader
in interface MessageReader<XMessageHeader,XMessage>
java.io.IOException
- if an error occurspublic XMessage readMessage(java.util.Optional<XMessage> reuse, XMessageHeader hdr) throws java.io.IOException
MessageReader
Message
instance or into the new one if not present.
For asynchronous channel it synchronously reads the next message in the stream, blocking until the message is read fully.
Could throw CJCommunicationsException wrapping an IOException
during read or parsereadMessage
in interface MessageReader<XMessageHeader,XMessage>
reuse
- Message
object to reuse. May be ignored by implementation.hdr
- MessageHeader
instanceMessage
instancejava.io.IOException
- if an error occurspublic XMessage readMessage(java.util.Optional<XMessage> reuse, int expectedType) throws java.io.IOException
MessageReader
Message
instance or into the new one if not present.
For asynchronous channel it synchronously reads the next message in the stream, blocking until the message is read fully.
Could throw WrongArgumentException if the expected message type is not the next message (exception will be thrown in *caller* context).readMessage
in interface MessageReader<XMessageHeader,XMessage>
reuse
- Message
object to reuse. May be ignored by implementation.expectedType
- Expected type of message.Message
instancejava.io.IOException
- if an error occurs