From 25cde5aeb6e60406cbf403edc96787c87e62f273 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 9 Feb 2019 02:43:18 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB=D0=B5=D0=BC=D1=83=20?= =?UTF-8?q?=D1=81=20longpoll=20=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80=D0=BE?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ключ становился недействительным спустя несколько часов, необходимо было запрашивать новый ключ, чтобы получать события от вк --- pom.xml | 2 +- .../vkbot/core/listener/EventListenerVk.java | 47 ++++++++----------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/pom.xml b/pom.xml index ee40b86..80d9c62 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.vkbot core - 0.4.1-RELEASE + 0.4.2-RELEASE diff --git a/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java b/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java index 6092007..13528a3 100644 --- a/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java +++ b/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java @@ -1,13 +1,12 @@ package org.sadtech.vkbot.core.listener; import com.google.gson.JsonObject; -import com.vk.api.sdk.actions.LongPoll; -import com.vk.api.sdk.callback.longpoll.queries.GetLongPollEventsQuery; import com.vk.api.sdk.callback.longpoll.responses.GetLongPollEventsResponse; import com.vk.api.sdk.client.VkApiClient; import com.vk.api.sdk.client.actors.GroupActor; import com.vk.api.sdk.exceptions.ApiException; import com.vk.api.sdk.exceptions.ClientException; +import com.vk.api.sdk.exceptions.LongPollServerKeyExpiredException; import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse; import org.apache.log4j.Logger; import org.sadtech.vkbot.core.VkConnect; @@ -24,22 +23,16 @@ public class EventListenerVk implements EventListener, Runnable { private RawEventService rawEventService; - private GetLongPollEventsQuery longPollEventsQuery; - private LongPoll longPoll; - private GetLongPollServerResponse server; - public EventListenerVk(VkConnect vkConnect) { vk = vkConnect.getVkApiClient(); actor = vkConnect.getGroupActor(); rawEventService = new RawEventServiceImpl(new EventRepositoryQueue()); - longPoll = new LongPoll(vk); } public EventListenerVk(VkConnect vkConnect, RawEventService rawEventService) { this.vk = vkConnect.getVkApiClient(); this.actor = vkConnect.getGroupActor(); this.rawEventService = rawEventService; - longPoll = new LongPoll(vk); } public RawEventService getRawEventService() { @@ -47,29 +40,29 @@ public class EventListenerVk implements EventListener, Runnable { } public void listen() throws ClientException, ApiException { - initServer(); - log.info("LongPollServer инициализирован"); - do { - GetLongPollEventsResponse eventsResponse; - eventsResponse = longPollEventsQuery.execute(); - if (eventsResponse.getUpdates().toArray().length != 0) { - log.info("Полученно событие от ВК"); - log.info(eventsResponse.getUpdates()); - for (JsonObject update : eventsResponse.getUpdates()) { - rawEventService.add(update); + GetLongPollServerResponse longPollServer = getLongPollServer(); + int lastTimeStamp = longPollServer.getTs(); + while (true) { + try { + GetLongPollEventsResponse eventsResponse = vk.longPoll().getEvents(longPollServer.getServer(), longPollServer.getKey(), lastTimeStamp).waitTime(25).execute(); + for (JsonObject jsonObject: eventsResponse.getUpdates()) { + log.info("Новое событие от LongPoll\n" + jsonObject); + rawEventService.add(jsonObject); } + lastTimeStamp = eventsResponse.getTs(); + } catch (LongPollServerKeyExpiredException e) { + longPollServer = getLongPollServer(); } - longPollEventsQuery = longPoll.getEvents(server.getServer(), server.getKey(), eventsResponse.getTs()).waitTime(20); - } while (true); + } + } - private void initServer() throws ClientException, ApiException { - server = vk.groups().getLongPollServer(actor).execute(); - String key = server.getKey(); - String serverUrl = server.getServer(); - Integer ts = server.getTs(); - longPoll = new LongPoll(vk); - longPollEventsQuery = longPoll.getEvents(serverUrl, key, ts).waitTime(20); + private GetLongPollServerResponse getLongPollServer() throws ClientException, ApiException { + log.info("LongPoll сервер инициализирован"); + if (actor != null) { + return vk.groups().getLongPollServer(actor).execute(); + } + return null; } @Override