Рефракторинг и создание интерфесов для слушателя событий

This commit is contained in:
Mark Struchkov 2018-12-24 00:51:28 +03:00
parent 92e8a893a7
commit 1ead58f169
10 changed files with 115 additions and 87 deletions

View File

@ -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<Observer> observers = new ArrayList<Observer>();
private List<JsonObject> jsonObjects = new ArrayList<JsonObject>();
@ -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) {
}
}

View 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);
}

View File

@ -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);
}

View File

@ -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) {

View File

@ -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 {

View File

@ -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());
}
}

View File

@ -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));

View File

@ -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);
}
}

View File

@ -0,0 +1,6 @@
package org.sadtech.vkbot.listener;
public interface EventListenable
{
void listen() throws Exception;
}

View File

@ -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);
}
}