Search in sources :

Example 1 with GetMessageEventsApiRequest

use of com.github.jamesnetherton.zulip.client.api.event.request.GetMessageEventsApiRequest in project zulip-java-client by jamesnetherton.

the class EventPoller method start.

/**
 * Starts event message polling.
 *
 * @throws ZulipClientException if the event polling request was not successful
 */
public synchronized void start() throws ZulipClientException {
    if (status.equals(Status.STOPPED)) {
        LOG.info("EventPoller starting");
        status = Status.STARTING;
        RegisterEventQueueApiRequest createQueue = new RegisterEventQueueApiRequest(this.client, narrows);
        GetMessageEventsApiRequest getEvents = new GetMessageEventsApiRequest(this.client);
        queue = createQueue.execute();
        executor = Executors.newSingleThreadExecutor();
        executor.submit(new Runnable() {

            private long lastEventId = queue.getLastEventId();

            @Override
            public void run() {
                while (status.equals(Status.STARTING) || status.equals(Status.STARTED)) {
                    try {
                        getEvents.withQueueId(queue.getQueueId());
                        getEvents.withLastEventId(lastEventId);
                        List<MessageEvent> messageEvents = getEvents.execute();
                        for (MessageEvent event : messageEvents) {
                            listener.onEvent(event.getMessage());
                        }
                        lastEventId = messageEvents.stream().max(Comparator.comparing(Event::getId)).get().getId();
                        Thread.sleep(5000);
                    } catch (ZulipClientException e) {
                        LOG.warning("Error processing events - " + e.getMessage());
                        if (e.getCode().equals("BAD_EVENT_QUEUE_ID")) {
                            // Queue may have been garbage collected so recreate it
                            try {
                                queue = createQueue.execute();
                            } catch (ZulipClientException zulipClientException) {
                                LOG.warning("Error recreating message queue - " + e.getMessage());
                            }
                        }
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        });
        LOG.info("EventPoller started");
        status = Status.STARTED;
    }
}
Also used : ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) GetMessageEventsApiRequest(com.github.jamesnetherton.zulip.client.api.event.request.GetMessageEventsApiRequest) RegisterEventQueueApiRequest(com.github.jamesnetherton.zulip.client.api.event.request.RegisterEventQueueApiRequest) List(java.util.List)

Aggregations

GetMessageEventsApiRequest (com.github.jamesnetherton.zulip.client.api.event.request.GetMessageEventsApiRequest)1 RegisterEventQueueApiRequest (com.github.jamesnetherton.zulip.client.api.event.request.RegisterEventQueueApiRequest)1 ZulipClientException (com.github.jamesnetherton.zulip.client.exception.ZulipClientException)1 List (java.util.List)1