* Добавлен ENUM для определения источника события

* Добавлен кастомный SQL метод возвращающий сообщения из БД новее запрошеной даты
* Добавлен глобальный обработчик сообщений
* Исрпавлены конфигурации spring, которые приводили к ошибкам в создании бинов
* Добавлен статический класс с методами вк апи
This commit is contained in:
Mark Struchkov 2019-01-05 00:45:57 +03:00
parent 1a19f1f88d
commit d6b902f2b9
14 changed files with 100 additions and 60 deletions

17
pom.xml
View File

@ -10,6 +10,18 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>consultant-core</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<persistence.ver>1.0.2</persistence.ver>
@ -43,6 +55,11 @@
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</project>

View File

@ -2,20 +2,20 @@ package org.sadtech.consultant.database.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.*;
@Data
@Entity
public class Message {
public class Messages {
@Id
@GeneratedValue
private Long idMess;
private Long idUser;
private String text;
private String date;
private Long date;
@Enumerated(EnumType.STRING)
private SourceMessage source;
}

View File

@ -1,17 +0,0 @@
package org.sadtech.consultant.database.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
@Data
@Entity
public class SocialNetworks {
@Id
private Long id;
private Long idVk;
private Long idFacebook;
}

View File

@ -0,0 +1,5 @@
package org.sadtech.consultant.database.entity;
public enum SourceMessage {
VK, FACEBOOK;
}

View File

@ -2,8 +2,7 @@ package org.sadtech.consultant.database.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.*;
import java.util.List;
@Entity
@ -16,7 +15,10 @@ public class User {
private String token;
private String lastName;
private String city;
// private List<SocialNetworks> socialNetworks;
@ElementCollection
@CollectionTable(name="SocialNetworks", joinColumns=@JoinColumn(name="id"))
private List<String> socialNetworks;
}

View File

@ -1,8 +1,16 @@
package org.sadtech.consultant.database.repository;
import org.sadtech.consultant.database.entity.Message;
import org.sadtech.consultant.database.entity.Messages;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface MessageRepository extends JpaRepository<Message, Long> {
import java.util.Collection;
import java.util.List;
public interface MessageRepository extends JpaRepository<Messages, Long> {
@Query("SELECT u FROM Messages u WHERE u.date > :date")
Collection<Messages> getMessagesByRange(@Param("date") Long date);
}

View File

@ -3,8 +3,7 @@ package org.sadtech.consultant.database.repository;
import org.sadtech.consultant.database.entity.NextUnit;
import org.springframework.data.jpa.repository.JpaRepository;
public interface NextUnitRepositoriy extends JpaRepository<NextUnit, Long> {
public interface NextUnitRepository extends JpaRepository<NextUnit, Long> {
}

View File

@ -1,7 +0,0 @@
package org.sadtech.consultant.database.repository;
import org.sadtech.consultant.database.entity.SocialNetworks;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SocialNetworksRepositoriy extends JpaRepository<SocialNetworks, Long> {
}

View File

@ -1,8 +1,12 @@
package org.sadtech.consultant.database.service;
import org.sadtech.consultant.database.entity.Message;
import org.sadtech.consultant.database.entity.Messages;
import java.util.List;
public interface MessageService {
void addMessage(Message message);
void addMessage(Messages message);
List<Messages> getMessageRange(Long date);
}

View File

@ -1,4 +0,0 @@
package org.sadtech.consultant.database.service;
public interface SocialNetworksService {
}

View File

@ -1,18 +1,25 @@
package org.sadtech.consultant.database.service.impl;
import org.sadtech.consultant.database.entity.Message;
import org.sadtech.consultant.database.entity.Messages;
import org.sadtech.consultant.database.repository.MessageRepository;
import org.sadtech.consultant.database.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
private MessageRepository repository;
public void addMessage(Message message) {
public void addMessage(Messages message) {
repository.saveAndFlush(message);
}
@Override
public List<Messages> getMessageRange(Long date) {
return (List<Messages>) repository.getMessagesByRange(date);
}
}

View File

@ -1,6 +1,6 @@
package org.sadtech.consultant.database.service.impl;
import org.sadtech.consultant.database.repository.NextUnitRepositoriy;
import org.sadtech.consultant.database.repository.NextUnitRepository;
import org.sadtech.consultant.database.service.NextUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -9,6 +9,6 @@ import org.springframework.stereotype.Service;
public class NextUnitServiceImpl implements NextUnitService {
@Autowired
private NextUnitRepositoriy repositoriy;
private NextUnitRepository repository;
}

View File

@ -1,14 +0,0 @@
package org.sadtech.consultant.database.service.impl;
import org.sadtech.consultant.database.repository.SocialNetworksRepositoriy;
import org.sadtech.consultant.database.service.SocialNetworksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SocialNetworksServiceImpl implements SocialNetworksService {
@Autowired
private SocialNetworksRepositoriy repositoriy;
}

View File

@ -0,0 +1,40 @@
package org.sadtech.consultant.processing;
import lombok.extern.log4j.Log4j;
import org.sadtech.consultant.database.entity.Messages;
import org.sadtech.consultant.database.service.MessageService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
@Log4j
@Component
public class MessageHandler {
private MessageService service;
private long data;
public MessageHandler(MessageService service) {
this.service = service;
data = new Date().getTime() / 1000;
}
@Async
public void processing() {
while (true) {
List<Messages> messages = service.getMessageRange(data);
for (Messages message : messages) {
System.out.println(message);
data = message.getDate();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}