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 Details

    • QueueResponseListener

      public QueueResponseListener​(Queue<String> queue)
      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

      public void onChunkReceived​(MotorResponse motorResponse)
      Called when a new chunk of response is received. This method adds the response chunk to the provided queue.
      Specified by:
      onChunkReceived in interface MotorResponseListener
      Parameters:
      motorResponse - The MotorResponse 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 interface MotorResponseListener
    • onError

      public void onError​(Throwable throwable)
      Description copied from interface: MotorResponseListener
      Called when an error occurs during the response streaming.
      Specified by:
      onError in interface MotorResponseListener
      Parameters:
      throwable - the exception that occurred during streaming.