Фикс LazyInitializationException
All checks were successful
Build rosetta-wss / build (push) Successful in 2m11s
All checks were successful
Build rosetta-wss / build (push) Successful in 2m11s
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package im.rosetta.database.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Session;
|
||||
|
||||
import im.rosetta.database.HibernateUtil;
|
||||
import im.rosetta.database.Repository;
|
||||
import im.rosetta.database.entity.PushToken;
|
||||
|
||||
public class PushTokenRepository extends Repository<PushToken> {
|
||||
|
||||
public PushTokenRepository() {
|
||||
super(PushToken.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Найти все токены для заданного публичного ключа
|
||||
* @param publicKey публичный ключ пользователя
|
||||
* @return список токенов для заданного публичного ключа
|
||||
*/
|
||||
public List<PushToken> findTokens(String publicKey) {
|
||||
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
|
||||
return session.createQuery("SELECT pt FROM PushToken pt JOIN pt.device d WHERE d.publicKey = :publicKey", PushToken.class)
|
||||
.setParameter("publicKey", publicKey)
|
||||
.getResultList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Найти все токены для заданного списка публичных ключей
|
||||
* @param publicKeys список публичных ключей пользователей
|
||||
* @return список токенов для заданного списка публичных ключей
|
||||
*/
|
||||
public List<PushToken> findTokens(List<String> publicKeys) {
|
||||
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
|
||||
return session.createQuery("SELECT pt FROM PushToken pt JOIN pt.device d WHERE d.publicKey IN :publicKeys", PushToken.class)
|
||||
.setParameter("publicKeys", publicKeys)
|
||||
.getResultList();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,19 +5,16 @@ import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import im.rosetta.database.entity.Device;
|
||||
import im.rosetta.database.entity.PushToken;
|
||||
import im.rosetta.database.repository.DeviceRepository;
|
||||
import im.rosetta.database.repository.PushTokenRepository;
|
||||
import im.rosetta.packet.runtime.TokenType;
|
||||
import im.rosetta.service.dispatch.push.dispatchers.FCM;
|
||||
import im.rosetta.service.dispatch.push.dispatchers.VoIPApns;
|
||||
import im.rosetta.service.services.DeviceService;
|
||||
|
||||
public class PushNotifyDispatcher {
|
||||
|
||||
private DeviceRepository deviceRepository = new DeviceRepository();
|
||||
private DeviceService deviceService = new DeviceService(deviceRepository);
|
||||
private final ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
private final PushTokenRepository pushTokenRepository = new PushTokenRepository();
|
||||
|
||||
private final HashMap<TokenType, Pusher> pushers = new HashMap<>() {{
|
||||
put(TokenType.FCM, new FCM());
|
||||
@@ -29,38 +26,15 @@ public class PushNotifyDispatcher {
|
||||
return this.pushers.get(tokenType);
|
||||
}
|
||||
|
||||
private List<RuntimeToken> findPushTokens(String publicKey) {
|
||||
List<Device> devices = this.deviceService.getDevicesByPK(publicKey);
|
||||
List<RuntimeToken> rtTokens = new java.util.ArrayList<>();
|
||||
for(Device device : devices){
|
||||
for(PushToken pushToken : device.getTokens()){
|
||||
rtTokens.add(new RuntimeToken(pushToken.getType(), pushToken.getToken()));
|
||||
}
|
||||
}
|
||||
return rtTokens;
|
||||
}
|
||||
|
||||
private List<RuntimeToken> findPushTokens(List<String> publicKeys) {
|
||||
List<RuntimeToken> rtTokens = new java.util.ArrayList<>();
|
||||
for(String publicKey : publicKeys){
|
||||
List<Device> devices = this.deviceService.getDevicesByPK(publicKey);
|
||||
for(Device device : devices){
|
||||
for(PushToken pushToken : device.getTokens()){
|
||||
rtTokens.add(new RuntimeToken(pushToken.getType(), pushToken.getToken()));
|
||||
}
|
||||
}
|
||||
}
|
||||
return rtTokens;
|
||||
}
|
||||
/**
|
||||
* Отправить уведомление пользователю с publicKey, используя все его токены для отправки уведомления, если таковые имеются
|
||||
* @param publicKey публичный ключ пользователя, которому нужно отправить уведомление
|
||||
* @param data данные уведомления
|
||||
*/
|
||||
public void sendPush(String publicKey, HashMap<String, String> data) {
|
||||
List<RuntimeToken> pushTokens = this.findPushTokens(publicKey);
|
||||
List<PushToken> pushTokens = this.pushTokenRepository.findTokens(publicKey);
|
||||
executor.execute(() -> {
|
||||
for(RuntimeToken pushToken : pushTokens){
|
||||
for(PushToken pushToken : pushTokens){
|
||||
Pusher pusher = this.findPusher(pushToken.getType());
|
||||
if(pusher != null){
|
||||
pusher.sendPush(pushToken.getToken(), data);
|
||||
@@ -75,9 +49,9 @@ public class PushNotifyDispatcher {
|
||||
* @param data данные уведомления
|
||||
*/
|
||||
public void sendPush(List<String> publicKeys, HashMap<String, String> data) {
|
||||
List<RuntimeToken> pushTokens = this.findPushTokens(publicKeys);
|
||||
List<PushToken> pushTokens = this.pushTokenRepository.findTokens(publicKeys);
|
||||
executor.execute(() -> {
|
||||
for(RuntimeToken pushToken : pushTokens){
|
||||
for(PushToken pushToken : pushTokens){
|
||||
Pusher pusher = this.findPusher(pushToken.getType());
|
||||
if(pusher != null){
|
||||
pusher.sendPush(pushToken.getToken(), data);
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package im.rosetta.service.dispatch.push;
|
||||
|
||||
import im.rosetta.packet.runtime.TokenType;
|
||||
|
||||
public class RuntimeToken {
|
||||
|
||||
private TokenType type;
|
||||
private String token;
|
||||
|
||||
public RuntimeToken(TokenType type, String token) {
|
||||
this.type = type;
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public TokenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user