Новый Subscribe, как-то работает
This commit is contained in:
parent
4e69f4d561
commit
d40a33bf80
@ -2,10 +2,15 @@ package org.sadtech.vkbot.core.distribution;
|
|||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public abstract class AbstractBasketSubscribe<S> implements EventSubscribe<S> {
|
public abstract class AbstractBasketSubscribe<S, C> {
|
||||||
|
|
||||||
private Set<AbstractBasketSubscribe> basketSubscribes;
|
private Set<AbstractBasketSubscribe> basketSubscribes;
|
||||||
private AbstractBasketSubscribe prioritySubscribe;
|
private AbstractBasketSubscribe prioritySubscribe;
|
||||||
|
protected Convert<S, C> convert;
|
||||||
|
|
||||||
|
public AbstractBasketSubscribe() {
|
||||||
|
convert = (object) -> (C) object;
|
||||||
|
}
|
||||||
|
|
||||||
public Set<AbstractBasketSubscribe> getBasketSubscribes() {
|
public Set<AbstractBasketSubscribe> getBasketSubscribes() {
|
||||||
return basketSubscribes;
|
return basketSubscribes;
|
||||||
@ -23,9 +28,17 @@ public abstract class AbstractBasketSubscribe<S> implements EventSubscribe<S> {
|
|||||||
this.prioritySubscribe = prioritySubscribe;
|
this.prioritySubscribe = prioritySubscribe;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Convert getConvert() {
|
||||||
|
return convert;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConvert(Convert convert) {
|
||||||
|
this.convert = convert;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract boolean check(S object);
|
protected abstract boolean check(S object);
|
||||||
|
|
||||||
private boolean goNextSubscribe(S object) {
|
private boolean goNextSubscribe(C object) {
|
||||||
boolean flag = false;
|
boolean flag = false;
|
||||||
if (prioritySubscribe != null && prioritySubscribe.check(object)) {
|
if (prioritySubscribe != null && prioritySubscribe.check(object)) {
|
||||||
prioritySubscribe.update(object);
|
prioritySubscribe.update(object);
|
||||||
@ -34,7 +47,6 @@ public abstract class AbstractBasketSubscribe<S> implements EventSubscribe<S> {
|
|||||||
for (AbstractBasketSubscribe basketSubscribe : basketSubscribes) {
|
for (AbstractBasketSubscribe basketSubscribe : basketSubscribes) {
|
||||||
if (basketSubscribe.check(object)) {
|
if (basketSubscribe.check(object)) {
|
||||||
basketSubscribe.update(object);
|
basketSubscribe.update(object);
|
||||||
} else {
|
|
||||||
flag = true;
|
flag = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,12 +54,12 @@ public abstract class AbstractBasketSubscribe<S> implements EventSubscribe<S> {
|
|||||||
return flag;
|
return flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void update(S object) {
|
public void update(S object) {
|
||||||
if (!goNextSubscribe(object)) {
|
C newObject = convert.converting(object);
|
||||||
processing(object);
|
if (!goNextSubscribe(newObject)) {
|
||||||
|
processing(newObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void processing(S object);
|
public abstract void processing(C object);
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package org.sadtech.vkbot.core.distribution;
|
|||||||
import com.vk.api.sdk.objects.messages.Message;
|
import com.vk.api.sdk.objects.messages.Message;
|
||||||
import org.sadtech.bot.core.service.AccountService;
|
import org.sadtech.bot.core.service.AccountService;
|
||||||
|
|
||||||
public class AccountSubscribe extends AbstractBasketSubscribe<Message> {
|
public class AccountSubscribe extends AbstractBasketSubscribe<Message, Message> {
|
||||||
|
|
||||||
private final AccountService accountService;
|
private final AccountService accountService;
|
||||||
|
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
package org.sadtech.vkbot.core.distribution;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Convert<T, C> {
|
||||||
|
|
||||||
|
C converting(T target);
|
||||||
|
|
||||||
|
}
|
@ -4,7 +4,7 @@ import com.google.gson.JsonObject;
|
|||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.sadtech.bot.core.service.RawEventService;
|
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);
|
private static final Logger log = Logger.getLogger(EventDistributor.class);
|
||||||
|
|
||||||
@ -15,22 +15,16 @@ public class EventDistributor extends AbstractBasketSubscribe<JsonObject> implem
|
|||||||
log.info("EventDistributor инициализирован");
|
log.info("EventDistributor инициализирован");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
@Override
|
||||||
|
public void run() {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (rawEventService.getJsonObjects().peek() != null) {
|
if (rawEventService.getJsonObjects().peek() != null) {
|
||||||
JsonObject event = rawEventService.getJsonObjects().poll();
|
JsonObject event = rawEventService.getJsonObjects().poll();
|
||||||
log.info("Главный дистрибьютор отправил событие дальше");
|
|
||||||
super.update(event);
|
super.update(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
log.info("EventDistributor запущен");
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean check(JsonObject object) {
|
protected boolean check(JsonObject object) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package org.sadtech.vkbot.core.distribution;
|
|
||||||
|
|
||||||
public interface EventSubscribe<T> {
|
|
||||||
|
|
||||||
void update(T object);
|
|
||||||
|
|
||||||
}
|
|
@ -17,7 +17,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.TimeZone;
|
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);
|
private static final Logger log = Logger.getLogger(MailSubscriber.class);
|
||||||
|
|
||||||
@ -25,6 +25,10 @@ public class MailSubscriber extends AbstractBasketSubscribe<JsonObject> {
|
|||||||
|
|
||||||
public MailSubscriber(MailService mailService) {
|
public MailSubscriber(MailService mailService) {
|
||||||
this.mailService = mailService;
|
this.mailService = mailService;
|
||||||
|
this.convert = (object) -> {
|
||||||
|
Gson gson = new Gson();
|
||||||
|
return gson.fromJson(object.getAsJsonObject("object"), Message.class);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -34,11 +38,8 @@ public class MailSubscriber extends AbstractBasketSubscribe<JsonObject> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void processing(JsonObject object) {
|
public void processing(Message object) {
|
||||||
Gson gson = new Gson();
|
mailService.add(createMail(object));
|
||||||
Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class);
|
|
||||||
log.info(userMessage);
|
|
||||||
mailService.add(createMail(userMessage));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mail createMail(Message message) {
|
private Mail createMail(Message message) {
|
||||||
|
Reference in New Issue
Block a user