From 58d2e7d3bc95027a75ad5c33d884ad1c50848dd4 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Mon, 31 Dec 2018 01:12:25 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB=20Ar?= =?UTF-8?q?rayList=20=D0=BD=D0=B0=20=D0=BF=D0=BE=D1=82=D0=BE=D0=BA=D0=BE?= =?UTF-8?q?=D0=B1=D0=B5=D0=B7=D0=BE=D0=BF=D0=B0=D1=81=D0=BD=D1=83=D1=8E=20?= =?UTF-8?q?Queue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Заменил ArrayList собираемых данных с poll сервера на потокобезопасную Queue, что позволило запустить паралельно с слушателем событий обработчик событий, при этом не теряя сообщения --- .../vkbot/listener/data/ResponsibleData.java | 4 ++-- .../vkbot/listener/data/impl/ResponseDataVk.java | 10 +++++----- .../vkbot/listener/handlers/DispetcherHandler.java | 14 ++++++-------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/ResponsibleData.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/ResponsibleData.java index 4ef3475..1c10736 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/ResponsibleData.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/ResponsibleData.java @@ -2,7 +2,7 @@ package org.sadtech.vkbot.listener.data; import com.google.gson.JsonObject; -import java.util.List; +import java.util.Queue; public interface ResponsibleData { @@ -12,6 +12,6 @@ public interface ResponsibleData { void cleanAll(); - List getJsonObjects(); + Queue getJsonObjects(); } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/impl/ResponseDataVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/impl/ResponseDataVk.java index 6fca8a2..1630991 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/impl/ResponseDataVk.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/data/impl/ResponseDataVk.java @@ -4,17 +4,17 @@ import com.google.gson.JsonObject; import org.sadtech.vkbot.listener.data.ResponsibleData; import org.springframework.stereotype.Component; -import java.util.ArrayList; -import java.util.List; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; @Component public class ResponseDataVk implements ResponsibleData { - private List jsonObjects = new ArrayList(); + private Queue jsonObjects = new ConcurrentLinkedQueue(); @Override public void add(JsonObject jsonObject) { - jsonObjects.add(jsonObject); + jsonObjects.offer(jsonObject); } @Override @@ -27,7 +27,7 @@ public class ResponseDataVk implements ResponsibleData { jsonObjects.clear(); } - public List getJsonObjects() { + public Queue getJsonObjects() { return jsonObjects; } } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/DispetcherHandler.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/DispetcherHandler.java index c120a03..a4f4a58 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/DispetcherHandler.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/DispetcherHandler.java @@ -5,6 +5,7 @@ import lombok.extern.log4j.Log4j; import org.sadtech.vkbot.listener.Observable; import org.sadtech.vkbot.listener.Observer; import org.sadtech.vkbot.listener.data.ResponsibleData; +import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -17,21 +18,18 @@ public class DispetcherHandler implements Observable { private ResponsibleData date; private List observers = new ArrayList(); - private List objects; public DispetcherHandler(ResponsibleData date) { this.date = date; } - @Scheduled(fixedRate = 5000) + @Async public void packaging() { - - objects = new ArrayList(date.getJsonObjects()); - date.cleanAll(); - for (JsonObject object : objects) { - log.info(object); + while (true) { + if (date.getJsonObjects().peek() != null) { + log.info(date.getJsonObjects().poll()); + } } - } @Override