Исправил проблему с longpoll сервером

Ключ становился недействительным спустя несколько часов, необходимо было запрашивать новый ключ, чтобы получать события от вк
This commit is contained in:
Mark Struchkov 2019-02-09 02:43:18 +03:00
parent b325a2e924
commit 25cde5aeb6
2 changed files with 21 additions and 28 deletions

View File

@ -6,7 +6,7 @@
<groupId>org.sadtech.vkbot</groupId> <groupId>org.sadtech.vkbot</groupId>
<artifactId>core</artifactId> <artifactId>core</artifactId>
<version>0.4.1-RELEASE</version> <version>0.4.2-RELEASE</version>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

View File

@ -1,13 +1,12 @@
package org.sadtech.vkbot.core.listener; package org.sadtech.vkbot.core.listener;
import com.google.gson.JsonObject; 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.callback.longpoll.responses.GetLongPollEventsResponse;
import com.vk.api.sdk.client.VkApiClient; import com.vk.api.sdk.client.VkApiClient;
import com.vk.api.sdk.client.actors.GroupActor; import com.vk.api.sdk.client.actors.GroupActor;
import com.vk.api.sdk.exceptions.ApiException; import com.vk.api.sdk.exceptions.ApiException;
import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.exceptions.LongPollServerKeyExpiredException;
import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse; import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.VkConnect; import org.sadtech.vkbot.core.VkConnect;
@ -24,22 +23,16 @@ public class EventListenerVk implements EventListener, Runnable {
private RawEventService rawEventService; private RawEventService rawEventService;
private GetLongPollEventsQuery longPollEventsQuery;
private LongPoll longPoll;
private GetLongPollServerResponse server;
public EventListenerVk(VkConnect vkConnect) { public EventListenerVk(VkConnect vkConnect) {
vk = vkConnect.getVkApiClient(); vk = vkConnect.getVkApiClient();
actor = vkConnect.getGroupActor(); actor = vkConnect.getGroupActor();
rawEventService = new RawEventServiceImpl(new EventRepositoryQueue()); rawEventService = new RawEventServiceImpl(new EventRepositoryQueue());
longPoll = new LongPoll(vk);
} }
public EventListenerVk(VkConnect vkConnect, RawEventService rawEventService) { public EventListenerVk(VkConnect vkConnect, RawEventService rawEventService) {
this.vk = vkConnect.getVkApiClient(); this.vk = vkConnect.getVkApiClient();
this.actor = vkConnect.getGroupActor(); this.actor = vkConnect.getGroupActor();
this.rawEventService = rawEventService; this.rawEventService = rawEventService;
longPoll = new LongPoll(vk);
} }
public RawEventService getRawEventService() { public RawEventService getRawEventService() {
@ -47,29 +40,29 @@ public class EventListenerVk implements EventListener, Runnable {
} }
public void listen() throws ClientException, ApiException { public void listen() throws ClientException, ApiException {
initServer(); GetLongPollServerResponse longPollServer = getLongPollServer();
log.info("LongPollServer инициализирован"); int lastTimeStamp = longPollServer.getTs();
do { while (true) {
GetLongPollEventsResponse eventsResponse; try {
eventsResponse = longPollEventsQuery.execute(); GetLongPollEventsResponse eventsResponse = vk.longPoll().getEvents(longPollServer.getServer(), longPollServer.getKey(), lastTimeStamp).waitTime(25).execute();
if (eventsResponse.getUpdates().toArray().length != 0) { for (JsonObject jsonObject: eventsResponse.getUpdates()) {
log.info("Полученно событие от ВК"); log.info("Новое событие от LongPoll\n" + jsonObject);
log.info(eventsResponse.getUpdates()); rawEventService.add(jsonObject);
for (JsonObject update : eventsResponse.getUpdates()) {
rawEventService.add(update);
} }
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 { private GetLongPollServerResponse getLongPollServer() throws ClientException, ApiException {
server = vk.groups().getLongPollServer(actor).execute(); log.info("LongPoll сервер инициализирован");
String key = server.getKey(); if (actor != null) {
String serverUrl = server.getServer(); return vk.groups().getLongPollServer(actor).execute();
Integer ts = server.getTs(); }
longPoll = new LongPoll(vk); return null;
longPollEventsQuery = longPoll.getEvents(serverUrl, key, ts).waitTime(20);
} }
@Override @Override