Новый Subscribe, как-то работает

This commit is contained in:
Mark Struchkov 2019-05-23 14:31:52 +03:00
parent 4e69f4d561
commit d40a33bf80
6 changed files with 38 additions and 30 deletions

View File

@ -2,10 +2,15 @@ package org.sadtech.vkbot.core.distribution;
import java.util.Set;
public abstract class AbstractBasketSubscribe<S> implements EventSubscribe<S> {
public abstract class AbstractBasketSubscribe<S, C> {
private Set<AbstractBasketSubscribe> basketSubscribes;
private AbstractBasketSubscribe prioritySubscribe;
protected Convert<S, C> convert;
public AbstractBasketSubscribe() {
convert = (object) -> (C) object;
}
public Set<AbstractBasketSubscribe> getBasketSubscribes() {
return basketSubscribes;
@ -23,9 +28,17 @@ public abstract class AbstractBasketSubscribe<S> implements EventSubscribe<S> {
this.prioritySubscribe = prioritySubscribe;
}
public Convert getConvert() {
return convert;
}
public void setConvert(Convert convert) {
this.convert = convert;
}
protected abstract boolean check(S object);
private boolean goNextSubscribe(S object) {
private boolean goNextSubscribe(C object) {
boolean flag = false;
if (prioritySubscribe != null && prioritySubscribe.check(object)) {
prioritySubscribe.update(object);
@ -34,7 +47,6 @@ public abstract class AbstractBasketSubscribe<S> implements EventSubscribe<S> {
for (AbstractBasketSubscribe basketSubscribe : basketSubscribes) {
if (basketSubscribe.check(object)) {
basketSubscribe.update(object);
} else {
flag = true;
}
}
@ -42,12 +54,12 @@ public abstract class AbstractBasketSubscribe<S> implements EventSubscribe<S> {
return flag;
}
@Override
public void update(S object) {
if (!goNextSubscribe(object)) {
processing(object);
C newObject = convert.converting(object);
if (!goNextSubscribe(newObject)) {
processing(newObject);
}
}
public abstract void processing(S object);
public abstract void processing(C object);
}

View File

@ -3,7 +3,7 @@ package org.sadtech.vkbot.core.distribution;
import com.vk.api.sdk.objects.messages.Message;
import org.sadtech.bot.core.service.AccountService;
public class AccountSubscribe extends AbstractBasketSubscribe<Message> {
public class AccountSubscribe extends AbstractBasketSubscribe<Message, Message> {
private final AccountService accountService;

View File

@ -0,0 +1,8 @@
package org.sadtech.vkbot.core.distribution;
@FunctionalInterface
public interface Convert<T, C> {
C converting(T target);
}

View File

@ -4,7 +4,7 @@ import com.google.gson.JsonObject;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.service.RawEventService;
public class EventDistributor extends AbstractBasketSubscribe<JsonObject> implements Runnable {
public class EventDistributor extends AbstractBasketSubscribe<JsonObject, JsonObject> implements Runnable {
private static final Logger log = Logger.getLogger(EventDistributor.class);
@ -15,22 +15,16 @@ public class EventDistributor extends AbstractBasketSubscribe<JsonObject> implem
log.info("EventDistributor инициализирован");
}
public void update() {
@Override
public void run() {
while (true) {
if (rawEventService.getJsonObjects().peek() != null) {
JsonObject event = rawEventService.getJsonObjects().poll();
log.info("Главный дистрибьютор отправил событие дальше");
super.update(event);
}
}
}
@Override
public void run() {
log.info("EventDistributor запущен");
update();
}
@Override
protected boolean check(JsonObject object) {
return false;

View File

@ -1,7 +0,0 @@
package org.sadtech.vkbot.core.distribution;
public interface EventSubscribe<T> {
void update(T object);
}

View File

@ -17,7 +17,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
public class MailSubscriber extends AbstractBasketSubscribe<JsonObject> {
public class MailSubscriber extends AbstractBasketSubscribe<JsonObject, Message> {
private static final Logger log = Logger.getLogger(MailSubscriber.class);
@ -25,6 +25,10 @@ public class MailSubscriber extends AbstractBasketSubscribe<JsonObject> {
public MailSubscriber(MailService mailService) {
this.mailService = mailService;
this.convert = (object) -> {
Gson gson = new Gson();
return gson.fromJson(object.getAsJsonObject("object"), Message.class);
};
}
@Override
@ -34,11 +38,8 @@ public class MailSubscriber extends AbstractBasketSubscribe<JsonObject> {
}
@Override
public void processing(JsonObject object) {
Gson gson = new Gson();
Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class);
log.info(userMessage);
mailService.add(createMail(userMessage));
public void processing(Message object) {
mailService.add(createMail(object));
}
private Mail createMail(Message message) {