Архитектурное разбиение на независимые проекты

This commit is contained in:
Mark Struchkov 2019-01-09 17:18:20 +03:00
parent 92c2c4fd7b
commit 00a964a9fd
13 changed files with 90 additions and 60 deletions

View File

@ -0,0 +1,7 @@
package org.sadtech.consultant;
public interface MessageSender {
void send(Integer idNetSoc, String text);
}

View File

@ -6,7 +6,7 @@ import javax.persistence.*;
@Data
@Entity
public class Message {
public class Mail {
@Id
@GeneratedValue
@ -14,11 +14,10 @@ public class Message {
private String text;
private Long date;
private String sourceMessage;
private String sex;
@ManyToOne
@JoinColumn(name = "idUser", nullable = false)
private Person user;
@JoinColumn(name = "idPerson", nullable = false)
private Person person;
}

View File

@ -24,10 +24,13 @@ public class Person {
private String city;
private String token;
private String email;
@ElementCollection
@CollectionTable(name = "PersonSocialNetworks")
@MapKeyColumn(name = "KeysSocialNetworks", length = 20)
private Map<String, Integer> socialNetworks = new HashMap<>();
}

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.*;
@Entity
@Data
@ -13,4 +12,8 @@ public class SaveUnit {
private Long id;
private Long idStage;
@OneToOne
@JoinColumn(name = "idPerson", nullable = false)
private Person user;
}

View File

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

View File

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

View File

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

View File

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

View File

@ -1,25 +0,0 @@
package org.sadtech.consultant.database.service.impl;
import org.sadtech.consultant.database.entity.Message;
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) {
repository.saveAndFlush(message);
}
@Override
public List<Message> getMessageRange(Long date) {
return (List<Message>) repository.getMessagesByRange(date);
}
}

View File

@ -1,7 +1,7 @@
package org.sadtech.consultant.database.service.impl;
import org.sadtech.consultant.database.entity.Person;
import org.sadtech.consultant.database.repository.PersonRepository;
import org.sadtech.consultant.database.entity.Person;
import org.sadtech.consultant.database.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

View File

@ -1,7 +1,7 @@
package org.sadtech.consultant.database.service.impl;
import org.sadtech.consultant.database.entity.Unit;
import org.sadtech.consultant.database.repository.UnitRepositoriy;
import org.sadtech.consultant.database.entity.Unit;
import org.sadtech.consultant.database.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

View File

@ -1,12 +1,17 @@
package org.sadtech.consultant.processing;
import lombok.extern.log4j.Log4j;
import org.sadtech.consultant.database.entity.Message;
import org.sadtech.consultant.database.entity.Mail;
import org.sadtech.consultant.MessageSender;
import org.sadtech.consultant.database.entity.Person;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Log4j
@Component
@ -14,6 +19,7 @@ public class MessageHandler {
private MessageLogicService messageLogicService;
private PersonLogicService userLogicService;
private Map<String, MessageSender> senderHashMap = new HashMap<>();
private long data;
public MessageHandler(MessageLogicService messageLogicService, PersonLogicService userLogicService) {
@ -22,13 +28,25 @@ public class MessageHandler {
data = new Date().getTime() / 1000;
}
public void addMessageSendler(String type, MessageSender messageSender) {
senderHashMap.put(type, messageSender);
}
@Async
@Transactional
public void processing() {
log.info("process");
while (true) {
List<Message> messages = messageLogicService.getMessageRange(data);
for (Message message : messages) {
log.info(message);
data = message.getDate();
List<Mail> messages = messageLogicService.getMessageRange(data);
if (messages.size()>0) {
for (Mail message : messages) {
log.info(message);
Person person = message.getPerson();
Integer idNetSoc = person.getSocialNetworks().get(message.getSourceMessage());
senderHashMap.get(message.getSourceMessage()).send(idNetSoc, "Тестовое сообщение");
data = message.getDate();
}
}
}
}

View File

@ -1,7 +1,7 @@
package org.sadtech.consultant.processing;
import org.sadtech.consultant.database.entity.Message;
import org.sadtech.consultant.database.service.MessageService;
import org.sadtech.consultant.database.entity.Mail;
import org.sadtech.consultant.database.service.MailService;
import org.springframework.stereotype.Component;
import java.util.List;
@ -9,17 +9,17 @@ import java.util.List;
@Component
public class MessageLogicService {
private MessageService messageService;
private MailService messageService;
public MessageLogicService(MessageService messageService) {
public MessageLogicService(MailService messageService) {
this.messageService = messageService;
}
public void addMessage(Message message) {
public void addMessage(Mail message) {
messageService.addMessage(message);
}
public List<Message> getMessageRange(long data) {
public List<Mail> getMessageRange(long data) {
return messageService.getMessageRange(data);
}
}