diff --git a/core/src/main/java/org/sadtech/consultant/entity/Message.java b/core/src/main/java/org/sadtech/consultant/entity/Message.java new file mode 100644 index 0000000..454188f --- /dev/null +++ b/core/src/main/java/org/sadtech/consultant/entity/Message.java @@ -0,0 +1,18 @@ +package org.sadtech.consultant.entity; + +import lombok.Data; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Data +@Entity +public class Message { + + @Id + private Long idMess; + private Long idUser; + private String text; + + +} diff --git a/core/src/main/java/org/sadtech/consultant/repository/MessageRepository.java b/core/src/main/java/org/sadtech/consultant/repository/MessageRepository.java new file mode 100644 index 0000000..aa0ec3b --- /dev/null +++ b/core/src/main/java/org/sadtech/consultant/repository/MessageRepository.java @@ -0,0 +1,8 @@ +package org.sadtech.consultant.repository; + +import org.sadtech.consultant.entity.Message; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface MessageRepository extends JpaRepository { + +} diff --git a/core/src/main/java/org/sadtech/consultant/service/MessageService.java b/core/src/main/java/org/sadtech/consultant/service/MessageService.java new file mode 100644 index 0000000..b40658e --- /dev/null +++ b/core/src/main/java/org/sadtech/consultant/service/MessageService.java @@ -0,0 +1,8 @@ +package org.sadtech.consultant.service; + +import org.sadtech.consultant.entity.Message; + +public interface MessageService { + + void addMessage(Message message); +} diff --git a/core/src/main/java/org/sadtech/consultant/service/impl/MessageServiceImpl.java b/core/src/main/java/org/sadtech/consultant/service/impl/MessageServiceImpl.java new file mode 100644 index 0000000..e752d1e --- /dev/null +++ b/core/src/main/java/org/sadtech/consultant/service/impl/MessageServiceImpl.java @@ -0,0 +1,16 @@ +package org.sadtech.consultant.service.impl; + +import org.sadtech.consultant.entity.Message; +import org.sadtech.consultant.repository.MessageRepository; +import org.sadtech.consultant.service.MessageService; +import org.springframework.beans.factory.annotation.Autowired; + +public class MessageServiceImpl implements MessageService { + + @Autowired + private MessageRepository repository; + + public void addMessage(Message message) { + repository.saveAndFlush(message); + } +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/Observable.java b/vk-bot/src/main/java/org/sadtech/vkbot/Observable.java deleted file mode 100644 index b9a0acc..0000000 --- a/vk-bot/src/main/java/org/sadtech/vkbot/Observable.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.sadtech.vkbot; - -public interface Observable { - void registerObserver(org.sadtech.vkbot.Observer o); -// void removeObserver(Observer o); - void notifyObservers(); -} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/ResponseDataVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/ResponseDataVk.java deleted file mode 100644 index c778882..0000000 --- a/vk-bot/src/main/java/org/sadtech/vkbot/ResponseDataVk.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.sadtech.vkbot; - -import com.google.gson.JsonObject; - -import java.util.ArrayList; -import java.util.List; - -public class ResponseDataVk implements Observable, ResponsibleData { - - private List observers = new ArrayList(); - private List jsonObjects = new ArrayList(); - - @Override - public void registerObserver(Observer o) { - observers.add(o); - } - - @Override - public void notifyObservers() { - for (Observer observer:observers) { - observer.update(jsonObjects); - } - } - - public void setJsonObjects(List jsonObjects) { - 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 deleted file mode 100644 index 0c2e8fa..0000000 --- a/vk-bot/src/main/java/org/sadtech/vkbot/ResponsibleData.java +++ /dev/null @@ -1,10 +0,0 @@ -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 deleted file mode 100644 index 561eaeb..0000000 --- a/vk-bot/src/main/java/org/sadtech/vkbot/TestLogic.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.sadtech.vkbot; - -import com.google.gson.JsonObject; - -import java.util.List; - -public class TestLogic implements Observer{ - - private ResponseDataVk responseData; - - public TestLogic(ResponseDataVk responseData) { - this.responseData = responseData; - responseData.registerObserver(this); - } - - @Override - public void update(List jsonObjects) { - System.out.println(jsonObjects); - } -} 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 ab62ad9..d26c51b 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java @@ -4,7 +4,6 @@ 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; @Log4j @Component 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 bd5202d..5360255 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 @@ -3,7 +3,6 @@ package org.sadtech.vkbot.config; import org.hibernate.jpa.HibernatePersistenceProvider; import org.springframework.beans.factory.annotation.Value; 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.data.jpa.repository.config.EnableJpaRepositories; @@ -39,14 +38,13 @@ public class DataConfig { @Value("${db.hibernate.hbm2ddl.auto}") private String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO; - @Bean + @Bean(name = "entityManagerFactory") public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(dataSource()); entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class); entityManagerFactoryBean.setPackagesToScan(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN); entityManagerFactoryBean.setJpaProperties(hibernateProp()); - return entityManagerFactoryBean; } @@ -54,25 +52,22 @@ public class DataConfig { public JpaTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); - return transactionManager; } @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName(PROPERTY_NAME_DATABASE_DRIVER); dataSource.setUrl(PROPERTY_NAME_DATABASE_URL); dataSource.setUsername(PROPERTY_NAME_DATABASE_USERNAME); dataSource.setPassword(PROPERTY_NAME_DATABASE_PASSWORD); - return dataSource; } private Properties hibernateProp() { Properties properties = new Properties(); - properties.put("hibernate.dialect", PROPERTY_NAME_HIBERNATE_DIALECT); + properties.put("hibernate.dialect", PROPERTY_NAME_HIBERNATE_DIALECT); properties.put("hibernate.show_sql", PROPERTY_NAME_HIBERNATE_SHOW_SQL); properties.put("hibernate.hbm2ddl.auto", PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO); return properties; diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfigVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfigVk.java index 3877aea..8519254 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfigVk.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfigVk.java @@ -8,9 +8,8 @@ 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.ResponseDataVk; -import org.sadtech.vkbot.TestLogic; -import org.sadtech.vkbot.VkOpenMethod; +import org.sadtech.vkbot.listener.data.impl.ResponseDataVk; +import org.sadtech.vkbot.listener.impl.EventListenerVk; import org.sadtech.vkbot.service.UserService; import org.sadtech.vkbot.service.impl.UserServiceImpl; import org.springframework.beans.factory.annotation.Value; @@ -18,12 +17,19 @@ 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.core.task.TaskExecutor; import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor; + +import java.util.concurrent.Executors; + @Configuration -@PropertySource("classpath:config.properties") -@ComponentScan("org.sadtech.vkbot") @EnableAsync +@PropertySource("classpath:config.properties") +@ComponentScan("org.sadtech.vkbot.listener") +@EnableScheduling public class SpringConfigVk { @Value("${vk.groupID}") @@ -80,23 +86,14 @@ public class SpringConfigVk { } catch (ClientException e) { e.printStackTrace(); } -// System.out.println("server: "+getLongPollServerResponse); return getLongPollServerResponse; } - @Bean - public UserService userService() { - return new UserServiceImpl(); - } @Bean - public ResponseDataVk responseDataVk() { - return new ResponseDataVk(); + public TaskExecutor taskExecutor() { + return new ConcurrentTaskExecutor(Executors.newFixedThreadPool(5)); } - @Bean - public TestLogic testLogic() { - return new TestLogic(responseDataVk()); - } - + } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/config/TestMain.java b/vk-bot/src/main/java/org/sadtech/vkbot/config/TestMain.java new file mode 100644 index 0000000..de506c8 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/config/TestMain.java @@ -0,0 +1,17 @@ +package org.sadtech.vkbot.config; + +import org.sadtech.vkbot.listener.EventListenable; +import org.sadtech.vkbot.listener.Observable; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + + +public class TestMain { + public static void main(String[] args) throws Exception { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigVk.class); + context.register(DataConfig.class); + EventListenable eventListener = context.getBean(EventListenable.class); + eventListener.listen(); + Observable dispetcherHandler = context.getBean(Observable.class); + dispetcherHandler.packaging(); + } +} 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 4670ac2..d13c088 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 @@ -1,27 +1,32 @@ -package org.sadtech.vkbot.config; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.WebApplicationInitializer; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.servlet.DispatcherServlet; - -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRegistration; - -@Configuration -@ComponentScan("org.sadtech.vkbot.listener") -public class WebConfig implements WebApplicationInitializer { - - @Override - public void onStartup(ServletContext servletContext) throws ServletException { - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.register(SpringConfigVk.class, WebConfig.class, DataConfig.class); - context.setServletContext(servletContext); - - ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); - dispatcher.setLoadOnStartup(1); - dispatcher.addMapping("/"); - } -} +//package org.sadtech.vkbot.config; +// +//import com.vk.api.sdk.exceptions.ApiException; +//import com.vk.api.sdk.exceptions.ClientException; +//import org.sadtech.vkbot.listener.EventListenable; +//import org.sadtech.vkbot.listener.Observable; +//import org.sadtech.vkbot.listener.impl.EventListenerVk; +//import org.springframework.context.annotation.ComponentScan; +//import org.springframework.context.annotation.Configuration; +//import org.springframework.web.WebApplicationInitializer; +//import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +//import org.springframework.web.servlet.DispatcherServlet; +// +//import javax.servlet.ServletContext; +//import javax.servlet.ServletException; +// +//@Configuration +//public class WebConfig implements WebApplicationInitializer { +// +// @Override +// public void onStartup(ServletContext servletContext) throws ServletException { +// AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); +// context.register(SpringConfigVk.class, DataConfig.class, WebConfig.class); +// +//// context.setServletContext(servletContext); +//// +//// +//// ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); +//// dispatcher.setLoadOnStartup(1); +//// dispatcher.addMapping("/"); +// } +//} 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 index 0faccd8..5af0eb1 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenable.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenable.java @@ -1,6 +1,5 @@ package org.sadtech.vkbot.listener; -public interface EventListenable -{ +public interface EventListenable { void listen() throws Exception; } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/Observable.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/Observable.java new file mode 100644 index 0000000..fc0d341 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/Observable.java @@ -0,0 +1,10 @@ +package org.sadtech.vkbot.listener; + +public interface Observable { + void registerObserver(Observer o); + + // void removeObserver(Observer o); + void notifyObservers(); + + void packaging() throws Exception; +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/Observer.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/Observer.java similarity index 79% rename from vk-bot/src/main/java/org/sadtech/vkbot/Observer.java rename to vk-bot/src/main/java/org/sadtech/vkbot/listener/Observer.java index 8cca6ce..7c27ddf 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/Observer.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/Observer.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot; +package org.sadtech.vkbot.listener; import com.google.gson.JsonObject; diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/ResponsibleData.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/ResponsibleData.java new file mode 100644 index 0000000..4ef3475 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/ResponsibleData.java @@ -0,0 +1,17 @@ +package org.sadtech.vkbot.listener.data; + +import com.google.gson.JsonObject; + +import java.util.List; + +public interface ResponsibleData { + + void add(JsonObject jsonObject); + + void remove(int id); + + void cleanAll(); + + List getJsonObjects(); + +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/impl/ResponseDataVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/impl/ResponseDataVk.java new file mode 100644 index 0000000..6fca8a2 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/impl/ResponseDataVk.java @@ -0,0 +1,33 @@ +package org.sadtech.vkbot.listener.data.impl; + +import com.google.gson.JsonObject; +import org.sadtech.vkbot.listener.data.ResponsibleData; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class ResponseDataVk implements ResponsibleData { + + private List jsonObjects = new ArrayList(); + + @Override + public void add(JsonObject jsonObject) { + jsonObjects.add(jsonObject); + } + + @Override + public void remove(int id) { + jsonObjects.remove(id); + } + + @Override + public void cleanAll() { + jsonObjects.clear(); + } + + public List getJsonObjects() { + return jsonObjects; + } +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/DispetcherHandler.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/DispetcherHandler.java new file mode 100644 index 0000000..f47bfbf --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/DispetcherHandler.java @@ -0,0 +1,48 @@ +package org.sadtech.vkbot.listener.handlers; + +import com.google.gson.JsonObject; +import lombok.extern.log4j.Log4j; +import org.sadtech.vkbot.listener.Observable; +import org.sadtech.vkbot.listener.Observer; +import org.sadtech.vkbot.listener.data.ResponsibleData; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Log4j +@Component +public class DispetcherHandler implements Observable { + + private ResponsibleData date; + private List observers = new ArrayList(); + private List objects; + + public DispetcherHandler(ResponsibleData date) { + this.date = date; + } + + @Scheduled(fixedRate = 5000) + public void packaging() { + + objects = new ArrayList(date.getJsonObjects()); + date.cleanAll(); + for (JsonObject object : objects) { + System.out.println(object); + } + + } + + @Override + public void registerObserver(Observer o) { + observers.add(o); + } + + @Override + public void notifyObservers() { + for (Observer observer : observers) { + //observer.update(); + } + } +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/MessageHandlerVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/MessageHandlerVk.java new file mode 100644 index 0000000..8efc101 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/MessageHandlerVk.java @@ -0,0 +1,21 @@ +package org.sadtech.vkbot.listener.handlers; + +import com.google.gson.JsonObject; +import org.sadtech.vkbot.listener.Observer; + +import java.util.List; + +public class MessageHandlerVk implements Observer { + + private DispetcherHandler dispetcherHandler; + + public MessageHandlerVk(DispetcherHandler dispetcherHandler) { + this.dispetcherHandler = dispetcherHandler; + dispetcherHandler.registerObserver(this); + } + + @Override + public void update(List jsonObjects) { + + } +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenerVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/impl/EventListenerVk.java similarity index 75% rename from vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenerVk.java rename to vk-bot/src/main/java/org/sadtech/vkbot/listener/impl/EventListenerVk.java index 00aadb0..6f6c413 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/listener/EventListenerVk.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/impl/EventListenerVk.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.listener; +package org.sadtech.vkbot.listener.impl; import com.vk.api.sdk.actions.LongPoll; import com.vk.api.sdk.callback.longpoll.queries.GetLongPollEventsQuery; @@ -9,15 +9,14 @@ 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.sadtech.vkbot.listener.EventListenable; +import org.sadtech.vkbot.listener.data.ResponsibleData; import org.springframework.scheduling.annotation.Async; -import org.springframework.stereotype.Service; - -import javax.annotation.PostConstruct; +import org.springframework.stereotype.Component; @Log4j -@Service -public class EventListenerVk implements EventListenable, Runnable { +@Component +public class EventListenerVk implements EventListenable { private VkApiClient vk; private GroupActor actor; @@ -34,41 +33,28 @@ public class EventListenerVk implements EventListenable, Runnable { 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(); + log.info(eventsResponse); if (eventsResponse.getUpdates().toArray().length != 0) { - - //responseData.setJsonObjects(eventsResponse.getUpdates()); - log.info(eventsResponse.getUpdates()); + responseData.add(eventsResponse.getUpdates().get(0)); +// log.info(eventsResponse.getUpdates()); } longPollEventsQuery = longPoll.getEvents(server.getServer(), server.getKey(), eventsResponse.getTs()).waitTime(20); } while (true); } - }