Search in sources :

Example 1 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException 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)

Example 2 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException in project zulip-java-client by jamesnetherton.

the class ZulipIntegrationTestBase method afterEach.

@AfterEach
public void afterEach() throws Exception {
    if (zulip != null) {
        // Clean up messages
        List<Message> messages = zulip.messages().getMessages(100, 0, Anchor.NEWEST).execute();
        if (messages != null) {
            for (Message message : messages) {
                try {
                    zulip.messages().deleteMessage(message.getId()).execute();
                } catch (ZulipClientException e) {
                // Ignore
                }
            }
        }
        List<Message> privateMessages = zulip.messages().getMessages(100, 0, Anchor.NEWEST).withNarrows(Narrow.of("is", "private")).execute();
        if (privateMessages != null) {
            for (Message message : privateMessages) {
                try {
                    zulip.messages().deleteMessage(message.getId()).execute();
                } catch (ZulipClientException e) {
                // Ignore
                }
            }
        }
        // Clean up streams
        List<Stream> streams = zulip.streams().getAll().withIncludeDefault(false).execute();
        if (streams != null) {
            for (Stream stream : streams) {
                try {
                    zulip.streams().delete(stream.getStreamId()).execute();
                } catch (ZulipClientException e) {
                // Ignore
                }
            }
        }
        // Clean up user groups
        List<UserGroup> groups = zulip.users().getUserGroups().execute();
        if (groups != null) {
            for (UserGroup group : groups) {
                try {
                    zulip.users().deleteUserGroup(group.getId()).execute();
                } catch (ZulipClientException e) {
                // Ignore
                }
            }
        }
        // Clean up profile fields
        List<ProfileField> fields = zulip.server().getCustomProfileFields().execute();
        if (fields != null) {
            for (ProfileField field : fields) {
                try {
                    zulip.server().deleteCustomProfileField(field.getId()).execute();
                } catch (ZulipClientException e) {
                // Ignore
                }
            }
        }
    }
    // Clean up drafts
    List<Draft> drafts = zulip.drafts().getDrafts().execute();
    if (drafts != null) {
        for (Draft draft : drafts) {
            try {
                zulip.drafts().deleteDraft(draft.getId()).execute();
            } catch (ZulipClientException e) {
            // Ignore
            }
        }
    }
}
Also used : ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) Draft(com.github.jamesnetherton.zulip.client.api.draft.Draft) Message(com.github.jamesnetherton.zulip.client.api.message.Message) ProfileField(com.github.jamesnetherton.zulip.client.api.server.ProfileField) Stream(com.github.jamesnetherton.zulip.client.api.stream.Stream) UserGroup(com.github.jamesnetherton.zulip.client.api.user.UserGroup) AfterEach(org.junit.jupiter.api.AfterEach)

Example 3 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException in project zulip-java-client by jamesnetherton.

the class ZulipEventIT method messageEventsWithNarrow.

@Test
public void messageEventsWithNarrow() throws Exception {
    CountDownLatch latch = new CountDownLatch(5);
    List<String> messages = new ArrayList<>();
    String streamA = UUID.randomUUID().toString().split("-")[0];
    String streamB = UUID.randomUUID().toString().split("-")[0];
    zulip.streams().subscribe(StreamSubscriptionRequest.of(streamA, streamA), StreamSubscriptionRequest.of(streamB, streamB)).execute();
    for (int i = 0; i < 10; i++) {
        List<Stream> streams = zulip.streams().getAll().execute();
        List<Stream> matches = streams.stream().filter(stream -> stream.getName().equals(streamA) || stream.getName().equals(streamB)).collect(Collectors.toList());
        if (matches.size() == 2) {
            break;
        }
        Thread.sleep(500);
    }
    EventPoller eventPoller = zulip.events().captureMessageEvents(new MessageEventListener() {

        @Override
        public void onEvent(Message event) {
            messages.add(event.getContent());
            latch.countDown();
        }
    }, Narrow.of("stream", streamA));
    try {
        eventPoller.start();
        MessageService messageService = zulip.messages();
        for (int i = 0; i < 10; i++) {
            String streamName = i % 2 == 0 ? streamA : streamB;
            messageService.sendStreamMessage("Stream " + streamName + " Content " + i, streamName, "testtopic").execute();
        }
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        int count = 0;
        for (int i = 0; i < 5; i++) {
            assertEquals("Stream " + streamA + " Content " + count, messages.get(i));
            count += 2;
        }
    } catch (ZulipClientException e) {
        e.printStackTrace();
        throw e;
    } finally {
        eventPoller.stop();
    }
}
Also used : Message(com.github.jamesnetherton.zulip.client.api.message.Message) StreamService(com.github.jamesnetherton.zulip.client.api.stream.StreamService) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) MessageService(com.github.jamesnetherton.zulip.client.api.message.MessageService) Stream(com.github.jamesnetherton.zulip.client.api.stream.Stream) EventPoller(com.github.jamesnetherton.zulip.client.api.event.EventPoller) StreamSubscriptionRequest(com.github.jamesnetherton.zulip.client.api.stream.StreamSubscriptionRequest) ZulipIntegrationTestBase(com.github.jamesnetherton.zulip.client.api.integration.ZulipIntegrationTestBase) UUID(java.util.UUID) Disabled(org.junit.jupiter.api.Disabled) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageEventListener(com.github.jamesnetherton.zulip.client.api.event.MessageEventListener) Narrow(com.github.jamesnetherton.zulip.client.api.narrow.Narrow) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) EventPoller(com.github.jamesnetherton.zulip.client.api.event.EventPoller) Message(com.github.jamesnetherton.zulip.client.api.message.Message) MessageEventListener(com.github.jamesnetherton.zulip.client.api.event.MessageEventListener) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) MessageService(com.github.jamesnetherton.zulip.client.api.message.MessageService) Stream(com.github.jamesnetherton.zulip.client.api.stream.Stream) Test(org.junit.jupiter.api.Test)

Example 4 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException in project zulip-java-client by jamesnetherton.

the class ZulipEventIT method messageEvents.

@Test
public void messageEvents() throws Exception {
    CountDownLatch latch = new CountDownLatch(3);
    List<String> messages = new ArrayList<>();
    String streamName = UUID.randomUUID().toString().split("-")[0];
    StreamSubscriptionRequest subscriptionRequest = StreamSubscriptionRequest.of(streamName, streamName);
    StreamService streamService = zulip.streams();
    streamService.subscribe(subscriptionRequest).execute();
    for (int i = 0; i < 10; i++) {
        List<Stream> streams = streamService.getAll().execute();
        List<Stream> matches = streams.stream().filter(stream -> stream.getName().equals(streamName)).collect(Collectors.toList());
        if (matches.size() == 1) {
            break;
        }
        Thread.sleep(500);
    }
    EventPoller eventPoller = zulip.events().captureMessageEvents(new MessageEventListener() {

        @Override
        public void onEvent(Message event) {
            messages.add(event.getContent());
            latch.countDown();
        }
    });
    try {
        eventPoller.start();
        MessageService messageService = zulip.messages();
        for (int i = 0; i < 3; i++) {
            messageService.sendStreamMessage("Test Content " + i, streamName, "testtopic").execute();
        }
        assertTrue(latch.await(5, TimeUnit.SECONDS));
        for (int i = 0; i < 3; i++) {
            assertEquals("Test Content " + i, messages.get(i));
        }
    } catch (ZulipClientException e) {
        e.printStackTrace();
        throw e;
    } finally {
        eventPoller.stop();
    }
}
Also used : StreamService(com.github.jamesnetherton.zulip.client.api.stream.StreamService) Message(com.github.jamesnetherton.zulip.client.api.message.Message) StreamService(com.github.jamesnetherton.zulip.client.api.stream.StreamService) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) MessageService(com.github.jamesnetherton.zulip.client.api.message.MessageService) Stream(com.github.jamesnetherton.zulip.client.api.stream.Stream) EventPoller(com.github.jamesnetherton.zulip.client.api.event.EventPoller) StreamSubscriptionRequest(com.github.jamesnetherton.zulip.client.api.stream.StreamSubscriptionRequest) ZulipIntegrationTestBase(com.github.jamesnetherton.zulip.client.api.integration.ZulipIntegrationTestBase) UUID(java.util.UUID) Disabled(org.junit.jupiter.api.Disabled) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageEventListener(com.github.jamesnetherton.zulip.client.api.event.MessageEventListener) Narrow(com.github.jamesnetherton.zulip.client.api.narrow.Narrow) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) EventPoller(com.github.jamesnetherton.zulip.client.api.event.EventPoller) Message(com.github.jamesnetherton.zulip.client.api.message.Message) StreamSubscriptionRequest(com.github.jamesnetherton.zulip.client.api.stream.StreamSubscriptionRequest) MessageEventListener(com.github.jamesnetherton.zulip.client.api.event.MessageEventListener) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) MessageService(com.github.jamesnetherton.zulip.client.api.message.MessageService) Stream(com.github.jamesnetherton.zulip.client.api.stream.Stream) Test(org.junit.jupiter.api.Test)

Example 5 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException in project zulip-java-client by jamesnetherton.

the class ZulipCommonsHttpClient method getRequestUri.

private URI getRequestUri(String path, Map<String, Object> parameters) throws ZulipClientException {
    URL zulipUrl = configuration.getZulipUrl();
    URIBuilder builder = new URIBuilder().setScheme(zulipUrl.getProtocol()).setHost(zulipUrl.getHost()).setPort(zulipUrl.getPort()).setPath(ZulipUrlUtils.API_BASE_PATH + "/" + path);
    if (parameters != null) {
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            if (entry.getValue() != null) {
                builder.addParameter(entry.getKey(), entry.getValue().toString());
            }
        }
    }
    try {
        return builder.build();
    } catch (URISyntaxException e) {
        throw new ZulipClientException(e);
    }
}
Also used : ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) URISyntaxException(java.net.URISyntaxException) Map(java.util.Map) URL(java.net.URL) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

ZulipClientException (com.github.jamesnetherton.zulip.client.exception.ZulipClientException)12 List (java.util.List)4 Test (org.junit.jupiter.api.Test)4 ZulipIntegrationTestBase (com.github.jamesnetherton.zulip.client.api.integration.ZulipIntegrationTestBase)3 Message (com.github.jamesnetherton.zulip.client.api.message.Message)3 Stream (com.github.jamesnetherton.zulip.client.api.stream.Stream)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 UUID (java.util.UUID)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 EventPoller (com.github.jamesnetherton.zulip.client.api.event.EventPoller)2 MessageEventListener (com.github.jamesnetherton.zulip.client.api.event.MessageEventListener)2 MessageService (com.github.jamesnetherton.zulip.client.api.message.MessageService)2 Narrow (com.github.jamesnetherton.zulip.client.api.narrow.Narrow)2 ProfileField (com.github.jamesnetherton.zulip.client.api.server.ProfileField)2 StreamService (com.github.jamesnetherton.zulip.client.api.stream.StreamService)2 StreamSubscriptionRequest (com.github.jamesnetherton.zulip.client.api.stream.StreamSubscriptionRequest)2 ZulipRateLimitExceededException (com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException)2 File (java.io.File)2