Рефракторинг и создание интерфесов для слушателя событий
This commit is contained in:
parent
92e8a893a7
commit
1ead58f169
@ -5,7 +5,7 @@ import com.google.gson.JsonObject;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ResponseData implements Observable {
|
public class ResponseDataVk implements Observable, ResponsibleData {
|
||||||
|
|
||||||
private List<Observer> observers = new ArrayList<Observer>();
|
private List<Observer> observers = new ArrayList<Observer>();
|
||||||
private List<JsonObject> jsonObjects = new ArrayList<JsonObject>();
|
private List<JsonObject> jsonObjects = new ArrayList<JsonObject>();
|
||||||
@ -26,4 +26,14 @@ public class ResponseData implements Observable {
|
|||||||
this.jsonObjects = jsonObjects;
|
this.jsonObjects = jsonObjects;
|
||||||
notifyObservers();
|
notifyObservers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(JsonObject jsonObject) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(JsonObject jsonObject) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
10
vk-bot/src/main/java/org/sadtech/vkbot/ResponsibleData.java
Normal file
10
vk-bot/src/main/java/org/sadtech/vkbot/ResponsibleData.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package org.sadtech.vkbot;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
public interface ResponsibleData {
|
||||||
|
|
||||||
|
void add(JsonObject jsonObject);
|
||||||
|
void remove(JsonObject jsonObject);
|
||||||
|
|
||||||
|
}
|
@ -6,9 +6,9 @@ import java.util.List;
|
|||||||
|
|
||||||
public class TestLogic implements Observer{
|
public class TestLogic implements Observer{
|
||||||
|
|
||||||
private ResponseData responseData;
|
private ResponseDataVk responseData;
|
||||||
|
|
||||||
public TestLogic(ResponseData responseData) {
|
public TestLogic(ResponseDataVk responseData) {
|
||||||
this.responseData = responseData;
|
this.responseData = responseData;
|
||||||
responseData.registerObserver(this);
|
responseData.registerObserver(this);
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,13 @@ import com.vk.api.sdk.client.VkApiClient;
|
|||||||
import com.vk.api.sdk.client.actors.GroupActor;
|
import com.vk.api.sdk.client.actors.GroupActor;
|
||||||
import lombok.extern.log4j.Log4j;
|
import lombok.extern.log4j.Log4j;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Component
|
|
||||||
@Log4j
|
@Log4j
|
||||||
|
@Component
|
||||||
public class VkOpenMethod {
|
public class VkOpenMethod {
|
||||||
|
|
||||||
private VkApiClient vk;
|
private VkApiClient vk;
|
||||||
|
|
||||||
private GroupActor actor;
|
private GroupActor actor;
|
||||||
|
|
||||||
public VkOpenMethod(VkApiClient vk, GroupActor actor) {
|
public VkOpenMethod(VkApiClient vk, GroupActor actor) {
|
||||||
|
@ -17,7 +17,6 @@ import java.util.Properties;
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
@ComponentScan("org.sadtech.vkbot")
|
|
||||||
@EnableJpaRepositories("org.sadtech.vkbot.repo")
|
@EnableJpaRepositories("org.sadtech.vkbot.repo")
|
||||||
@PropertySource("classpath:config.properties")
|
@PropertySource("classpath:config.properties")
|
||||||
public class DataConfig {
|
public class DataConfig {
|
||||||
|
@ -8,10 +8,9 @@ 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.httpclient.HttpTransportClient;
|
import com.vk.api.sdk.httpclient.HttpTransportClient;
|
||||||
import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse;
|
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.TestLogic;
|
||||||
import org.sadtech.vkbot.VkOpenMethod;
|
import org.sadtech.vkbot.VkOpenMethod;
|
||||||
import org.sadtech.vkbot.controller.MessagesListener;
|
|
||||||
import org.sadtech.vkbot.service.UserService;
|
import org.sadtech.vkbot.service.UserService;
|
||||||
import org.sadtech.vkbot.service.impl.UserServiceImpl;
|
import org.sadtech.vkbot.service.impl.UserServiceImpl;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
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.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@PropertySource("classpath:config.properties")
|
@PropertySource("classpath:config.properties")
|
||||||
@ComponentScan("org.sadtech.vkbot")
|
@ComponentScan("org.sadtech.vkbot")
|
||||||
public class SpringConfig {
|
@EnableAsync
|
||||||
|
public class SpringConfigVk {
|
||||||
|
|
||||||
@Value("${vk.groupID}")
|
@Value("${vk.groupID}")
|
||||||
private String groupId;
|
private String groupId;
|
||||||
@ -83,28 +84,19 @@ public class SpringConfig {
|
|||||||
return getLongPollServerResponse;
|
return getLongPollServerResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean(initMethod = "start")
|
|
||||||
public MessagesListener messagesListener() {
|
|
||||||
return new MessagesListener();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public UserService userService() {
|
public UserService userService() {
|
||||||
return new UserServiceImpl();
|
return new UserServiceImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ResponseData responseData() {
|
public ResponseDataVk responseDataVk() {
|
||||||
return new ResponseData();
|
return new ResponseDataVk();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public TestLogic testLogic() {
|
public TestLogic testLogic() {
|
||||||
return new TestLogic(responseData());
|
return new TestLogic(responseDataVk());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
public VkOpenMethod vkOpenMethod() {
|
|
||||||
return new VkOpenMethod(vkApiClient(), groupActor());
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -9,16 +9,15 @@ import org.springframework.web.servlet.DispatcherServlet;
|
|||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletRegistration;
|
import javax.servlet.ServletRegistration;
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan("org.sadtech.vkbot.controller")
|
@ComponentScan("org.sadtech.vkbot.listener")
|
||||||
public class WebConfig implements WebApplicationInitializer {
|
public class WebConfig implements WebApplicationInitializer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||||
context.register(SpringConfig.class, WebConfig.class, DataConfig.class);
|
context.register(SpringConfigVk.class, WebConfig.class, DataConfig.class);
|
||||||
context.setServletContext(servletContext);
|
context.setServletContext(servletContext);
|
||||||
|
|
||||||
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
|
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,6 @@
|
|||||||
|
package org.sadtech.vkbot.listener;
|
||||||
|
|
||||||
|
public interface EventListenable
|
||||||
|
{
|
||||||
|
void listen() throws Exception;
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user