From 1ead58f1698dfc8ab8698c61a8d0aa3c8865ad2a Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Mon, 24 Dec 2018 00:51:28 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D1=80=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=BD=D0=B3=20=D0=B8=20=D1=81=D0=BE=D0=B7?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D1=84=D0=B5=D1=81=D0=BE=D0=B2=20=D0=B4=D0=BB=D1=8F=20=D1=81?= =?UTF-8?q?=D0=BB=D1=83=D1=88=D0=B0=D1=82=D0=B5=D0=BB=D1=8F=20=D1=81=D0=BE?= =?UTF-8?q?=D0=B1=D1=8B=D1=82=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...{ResponseData.java => ResponseDataVk.java} | 12 ++- .../org/sadtech/vkbot/ResponsibleData.java | 10 +++ .../java/org/sadtech/vkbot/TestLogic.java | 4 +- .../java/org/sadtech/vkbot/VkOpenMethod.java | 4 +- .../org/sadtech/vkbot/config/DataConfig.java | 1 - ...{SpringConfig.java => SpringConfigVk.java} | 24 ++---- .../org/sadtech/vkbot/config/WebConfig.java | 5 +- .../vkbot/controller/MessagesListener.java | 62 ---------------- .../vkbot/listener/EventListenable.java | 6 ++ .../vkbot/listener/EventListenerVk.java | 74 +++++++++++++++++++ 10 files changed, 115 insertions(+), 87 deletions(-) rename vk-bot/src/main/java/org/sadtech/vkbot/{ResponseData.java => ResponseDataVk.java} (75%) create mode 100644 vk-bot/src/main/java/org/sadtech/vkbot/ResponsibleData.java rename vk-bot/src/main/java/org/sadtech/vkbot/config/{SpringConfig.java => SpringConfigVk.java} (84%) delete mode 100644 vk-bot/src/main/java/org/sadtech/vkbot/controller/MessagesListener.java create mode 100644 vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenable.java create mode 100644 vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenerVk.java diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/ResponseData.java b/vk-bot/src/main/java/org/sadtech/vkbot/ResponseDataVk.java similarity index 75% rename from vk-bot/src/main/java/org/sadtech/vkbot/ResponseData.java rename to vk-bot/src/main/java/org/sadtech/vkbot/ResponseDataVk.java index e6008b2..c778882 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/ResponseData.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/ResponseDataVk.java @@ -5,7 +5,7 @@ import com.google.gson.JsonObject; import java.util.ArrayList; import java.util.List; -public class ResponseData implements Observable { +public class ResponseDataVk implements Observable, ResponsibleData { private List observers = new ArrayList(); private List jsonObjects = new ArrayList(); @@ -26,4 +26,14 @@ public class ResponseData implements Observable { this.jsonObjects = jsonObjects; notifyObservers(); } + + @Override + public void add(JsonObject jsonObject) { + + } + + @Override + public void remove(JsonObject jsonObject) { + + } } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/ResponsibleData.java b/vk-bot/src/main/java/org/sadtech/vkbot/ResponsibleData.java new file mode 100644 index 0000000..0c2e8fa --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/ResponsibleData.java @@ -0,0 +1,10 @@ +package org.sadtech.vkbot; + +import com.google.gson.JsonObject; + +public interface ResponsibleData { + + void add(JsonObject jsonObject); + void remove(JsonObject jsonObject); + +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/TestLogic.java b/vk-bot/src/main/java/org/sadtech/vkbot/TestLogic.java index 8e8ed9e..561eaeb 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/TestLogic.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/TestLogic.java @@ -6,9 +6,9 @@ import java.util.List; public class TestLogic implements Observer{ - private ResponseData responseData; + private ResponseDataVk responseData; - public TestLogic(ResponseData responseData) { + public TestLogic(ResponseDataVk responseData) { this.responseData = responseData; responseData.registerObserver(this); } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java b/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java index 87ee357..ab62ad9 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java @@ -4,13 +4,13 @@ import com.vk.api.sdk.client.VkApiClient; import com.vk.api.sdk.client.actors.GroupActor; import lombok.extern.log4j.Log4j; import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; -@Component @Log4j +@Component public class VkOpenMethod { private VkApiClient vk; - private GroupActor actor; public VkOpenMethod(VkApiClient vk, GroupActor actor) { diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/config/DataConfig.java b/vk-bot/src/main/java/org/sadtech/vkbot/config/DataConfig.java index ebca4c1..bd5202d 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/config/DataConfig.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/config/DataConfig.java @@ -17,7 +17,6 @@ import java.util.Properties; @Configuration @EnableTransactionManagement -@ComponentScan("org.sadtech.vkbot") @EnableJpaRepositories("org.sadtech.vkbot.repo") @PropertySource("classpath:config.properties") public class DataConfig { diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfig.java b/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfigVk.java similarity index 84% rename from vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfig.java rename to vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfigVk.java index 1162f1d..3877aea 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfig.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfigVk.java @@ -8,10 +8,9 @@ import com.vk.api.sdk.exceptions.ApiException; import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.httpclient.HttpTransportClient; import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse; -import org.sadtech.vkbot.ResponseData; +import org.sadtech.vkbot.ResponseDataVk; import org.sadtech.vkbot.TestLogic; import org.sadtech.vkbot.VkOpenMethod; -import org.sadtech.vkbot.controller.MessagesListener; import org.sadtech.vkbot.service.UserService; import org.sadtech.vkbot.service.impl.UserServiceImpl; import org.springframework.beans.factory.annotation.Value; @@ -19,11 +18,13 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; +import org.springframework.scheduling.annotation.EnableAsync; @Configuration @PropertySource("classpath:config.properties") @ComponentScan("org.sadtech.vkbot") -public class SpringConfig { +@EnableAsync +public class SpringConfigVk { @Value("${vk.groupID}") private String groupId; @@ -83,28 +84,19 @@ public class SpringConfig { return getLongPollServerResponse; } - @Bean(initMethod = "start") - public MessagesListener messagesListener() { - return new MessagesListener(); - } - @Bean public UserService userService() { return new UserServiceImpl(); } @Bean - public ResponseData responseData() { - return new ResponseData(); + public ResponseDataVk responseDataVk() { + return new ResponseDataVk(); } @Bean public TestLogic testLogic() { - return new TestLogic(responseData()); - } - - @Bean - public VkOpenMethod vkOpenMethod() { - return new VkOpenMethod(vkApiClient(), groupActor()); + return new TestLogic(responseDataVk()); } + } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/config/WebConfig.java b/vk-bot/src/main/java/org/sadtech/vkbot/config/WebConfig.java index b808784..4670ac2 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/config/WebConfig.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/config/WebConfig.java @@ -9,16 +9,15 @@ import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; -import javax.sql.DataSource; @Configuration -@ComponentScan("org.sadtech.vkbot.controller") +@ComponentScan("org.sadtech.vkbot.listener") public class WebConfig implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.register(SpringConfig.class, WebConfig.class, DataConfig.class); + context.register(SpringConfigVk.class, WebConfig.class, DataConfig.class); context.setServletContext(servletContext); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/controller/MessagesListener.java b/vk-bot/src/main/java/org/sadtech/vkbot/controller/MessagesListener.java deleted file mode 100644 index 76ee76b..0000000 --- a/vk-bot/src/main/java/org/sadtech/vkbot/controller/MessagesListener.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.sadtech.vkbot.controller; - -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.objects.groups.responses.GetLongPollServerResponse; -import lombok.extern.log4j.Log4j; -import org.sadtech.vkbot.ResponseData; -import org.springframework.beans.factory.annotation.Autowired; - -@Log4j -public class MessagesListener extends Thread{ - - @Autowired - private VkApiClient vk; - - @Autowired - private GroupActor actor; - - @Autowired - private ResponseData responseData; - - public void run() { - GetLongPollServerResponse serverResponse = null; - try { - serverResponse = vk.groups().getLongPollServer(actor).execute(); - } catch (ApiException e) { - e.printStackTrace(); - } catch (ClientException e) { - e.printStackTrace(); - } - - String key = serverResponse.getServer(); - String server = serverResponse.getKey(); - Integer ts = serverResponse.getTs(); - - LongPoll longPoll = new LongPoll(vk); - GetLongPollEventsQuery longPollEventsQuery = longPoll.getEvents(server, key, ts).waitTime(20); - - GetLongPollEventsResponse eventsResponse = null; - do { - try { - eventsResponse = longPollEventsQuery.execute(); - } catch (ApiException e) { - e.printStackTrace(); - } catch (ClientException e) { - e.printStackTrace(); - } - if (eventsResponse.getUpdates().toArray().length != 0) { - - responseData.setJsonObjects(eventsResponse.getUpdates()); - - } - longPollEventsQuery = longPoll.getEvents(server, key, eventsResponse.getTs()).waitTime(20); - } while (true); - } - -} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenable.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenable.java new file mode 100644 index 0000000..0faccd8 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenable.java @@ -0,0 +1,6 @@ +package org.sadtech.vkbot.listener; + +public interface EventListenable +{ + void listen() throws Exception; +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenerVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenerVk.java new file mode 100644 index 0000000..00aadb0 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenerVk.java @@ -0,0 +1,74 @@ +package org.sadtech.vkbot.listener; + +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.objects.groups.responses.GetLongPollServerResponse; +import lombok.extern.log4j.Log4j; +import org.sadtech.vkbot.ResponsibleData; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; + +@Log4j +@Service +public class EventListenerVk implements EventListenable, Runnable { + + private VkApiClient vk; + private GroupActor actor; + private ResponsibleData responseData; + + private GetLongPollEventsQuery longPollEventsQuery; + private LongPoll longPoll; + private GetLongPollServerResponse server; + + public EventListenerVk(VkApiClient vk, GroupActor actor, ResponsibleData responseData) { + this.vk = vk; + this.actor = actor; + this.responseData = responseData; + longPoll = new LongPoll(vk); + } + + public void run() { + try { + listen(); + } catch (ClientException e) { + e.printStackTrace(); + } catch (ApiException e) { + e.printStackTrace(); + } + } + + 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); + } + + @Async + @PostConstruct + public void listen() throws ClientException, ApiException { + initServer(); + do { + GetLongPollEventsResponse eventsResponse; + eventsResponse = longPollEventsQuery.execute(); + if (eventsResponse.getUpdates().toArray().length != 0) { + + //responseData.setJsonObjects(eventsResponse.getUpdates()); + log.info(eventsResponse.getUpdates()); + } + longPollEventsQuery = longPoll.getEvents(server.getServer(), server.getKey(), eventsResponse.getTs()).waitTime(20); + } while (true); + } + + +}