Package com.chatmotorapi.api.listener
Class QueueResponseListener
java.lang.Object
com.chatmotorapi.api.listener.QueueResponseListener
- All Implemented Interfaces:
MotorResponseListener
public class QueueResponseListener extends Object implements MotorResponseListener
A concrete implementation of
MotorResponseListener
that stores response chunks in a user-provided queue.
This listener allows for asynchronous handling of streamed responses, enabling the application to process
the data in a flexible and efficient manner.
This implementation is particularly useful in scenarios where the application needs to process data chunks
as they arrive without blocking other operations.-
Constructor Summary
Constructors Constructor Description QueueResponseListener(Queue<String> queue)
Constructs a new QueueResponseListener with the specified queue. -
Method Summary
Modifier and Type Method Description void
onChunkReceived(MotorResponse motorResponse)
Called when a new chunk of response is received.void
onError(Throwable throwable)
Called when an error occurs during the response streaming.void
onResponseCompleted()
Called when the response streaming is successfully completed.
-
Constructor Details
-
QueueResponseListener
Constructs a new QueueResponseListener with the specified queue.- Parameters:
queue
- The queue where response chunks will be stored. It is expected to be thread-safe if used in a multi-threaded environment.
-
-
Method Details
-
onChunkReceived
Called when a new chunk of response is received. This method adds the response chunk to the provided queue.- Specified by:
onChunkReceived
in interfaceMotorResponseListener
- Parameters:
motorResponse
- TheMotorResponse
containing the chunk of data to be stored.
-
onResponseCompleted
public void onResponseCompleted()Description copied from interface:MotorResponseListener
Called when the response streaming is successfully completed. This method is only invoked if the entire process completes without encountering any errors.- Specified by:
onResponseCompleted
in interfaceMotorResponseListener
-
onError
Description copied from interface:MotorResponseListener
Called when an error occurs during the response streaming.- Specified by:
onError
in interfaceMotorResponseListener
- Parameters:
throwable
- the exception that occurred during streaming.
-