Поправил баги у сабскрайберов, переименовал методы евентсервиса

This commit is contained in:
Mark Struchkov 2019-08-01 23:41:19 +03:00
parent 7b7fb629fa
commit 9207713665
11 changed files with 46 additions and 55 deletions

View File

@ -1,41 +1,32 @@
package org.sadtech.vkbot.core.distribution;
import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.sadtech.vkbot.core.distribution.subscriber.AbstractBasketSubscribe;
import org.sadtech.vkbot.core.service.RawEventService;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
@Slf4j
@RequiredArgsConstructor
public class EventDistributor implements Runnable {
private final RawEventService rawEventService;
private final Set<AbstractBasketSubscribe> basketSubscribes;
public EventDistributor(RawEventService rawEventService, Set<AbstractBasketSubscribe> basketSubscribes) {
this.rawEventService = rawEventService;
this.basketSubscribes = basketSubscribes;
log.info("EventDistributor инициализирован");
}
private final Set<AbstractBasketSubscribe<JsonObject, ?>> basketSubscribes;
@Override
public void run() {
while (true) {
Optional.ofNullable(rawEventService.getJsonObjects())
Optional.ofNullable(rawEventService.getNewEvent())
.ifPresent(events -> events.forEach(this::goNextSubscribe));
}
}
private boolean goNextSubscribe(JsonObject object) {
AtomicBoolean flag = new AtomicBoolean(false);
private void goNextSubscribe(JsonObject object) {
basketSubscribes.stream()
.filter(basketSubscribe -> basketSubscribe.check(object))
.forEach(basketSubscribe -> {
basketSubscribe.update(object);
flag.set(true);
});
return flag.get();
.forEach(basketSubscribe -> basketSubscribe.update(object));
}
}

View File

@ -1,4 +1,4 @@
package org.sadtech.vkbot.core.distribution;
package org.sadtech.vkbot.core.distribution.subscriber;
import org.sadtech.vkbot.core.convert.Convert;
@ -15,7 +15,7 @@ public abstract class AbstractBasketSubscribe<S, C> {
convert = (object) -> (C) object;
}
protected abstract boolean check(S object);
public abstract boolean check(S object);
public void update(S object) {
C newObject = convert.converting(object);

View File

@ -1,4 +1,4 @@
package org.sadtech.vkbot.core.distribution;
package org.sadtech.vkbot.core.distribution.subscriber;
import com.vk.api.sdk.objects.messages.Message;
import com.vk.api.sdk.objects.messages.MessageAttachmentType;
@ -23,7 +23,7 @@ public class AccountSubscribe extends AbstractBasketSubscribe<Message, Message>
}
@Override
protected boolean check(Message userMessage) {
public boolean check(Message userMessage) {
return userMessage.getAttachments().size() > 0
&& MessageAttachmentType.LINK.equals(userMessage.getAttachments().get(0).getType())
&& "Payment awaiting acceptance".equals(userMessage.getAttachments().get(0).getLink().getCaption());

View File

@ -1,4 +1,4 @@
package org.sadtech.vkbot.core.distribution;
package org.sadtech.vkbot.core.distribution.subscriber;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
@ -53,7 +53,7 @@ public class BoardCommentSubscribe extends AbstractBasketSubscribe<JsonObject, T
}
@Override
protected boolean check(JsonObject object) {
public boolean check(JsonObject object) {
String type = object.get("type").getAsString();
return "board_post_new".equals(type);
}

View File

@ -1,4 +1,4 @@
package org.sadtech.vkbot.core.distribution;
package org.sadtech.vkbot.core.distribution.subscriber;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
@ -22,7 +22,7 @@ public class MailSubscriber extends AbstractBasketSubscribe<JsonObject, Message>
}
@Override
protected boolean check(JsonObject object) {
public boolean check(JsonObject object) {
String type = object.get("type").getAsString();
return "message_new".equals(type);
}

View File

@ -15,6 +15,6 @@ public interface RawEventRepository {
void cleanAll();
Set<JsonObject> getEventQueue();
Set<JsonObject> findNewEvent();
}

View File

@ -1,26 +0,0 @@
//package org.sadtech.vkbot.core.repository.impl;
//
//import com.google.gson.JsonObject;
//import org.sadtech.vkbot.core.repository.RawEventRepository;
//
//import java.util.Queue;
//import java.util.concurrent.ConcurrentLinkedQueue;
//
//public class RawEventRepositoryQueue implements RawEventRepository {
//
// private final Queue<JsonObject> jsonObjects = new ConcurrentLinkedQueue<>();
//
// @Override
// public void add(JsonObject jsonObject) {
// jsonObjects.offer(jsonObject);
// }
//
// @Override
// public void cleanAll() {
// jsonObjects.clear();
// }
//
// public Set<JsonObject> getEventQueue() {
// return jsonObjects;
// }
//}

View File

@ -0,0 +1,26 @@
package org.sadtech.vkbot.core.repository.impl;
import com.google.gson.JsonObject;
import org.sadtech.vkbot.core.repository.RawEventRepository;
import java.util.HashSet;
import java.util.Set;
public class RawEventRepositorySet implements RawEventRepository {
private final Set<JsonObject> jsonObjects = new HashSet<>();
@Override
public void add(JsonObject jsonObject) {
jsonObjects.add(jsonObject);
}
@Override
public void cleanAll() {
jsonObjects.clear();
}
public Set<JsonObject> findNewEvent() {
return jsonObjects;
}
}

View File

@ -38,7 +38,7 @@ public class RawEventRepositoryJpaImpl implements RawEventRepository {
}
@Override
public Set<JsonObject> getEventQueue() {
public Set<JsonObject> findNewEvent() {
if (addFlag) {
List<JsonObjectId> allEvent = rawEventRepositoryJpa.findAll();
rawEventRepositoryJpa.deleteAll(allEvent);

View File

@ -15,6 +15,6 @@ public interface RawEventService {
void add(JsonObject jsonObject);
Set<JsonObject> getJsonObjects();
Set<JsonObject> getNewEvent();
}

View File

@ -27,8 +27,8 @@ public class RawEventServiceImpl implements RawEventService {
}
@Override
public Set<JsonObject> getJsonObjects() {
return rawEventRepository.getEventQueue();
public Set<JsonObject> getNewEvent() {
return rawEventRepository.findNewEvent();
}
}