Заменил ArrayList на потокобезопасную Queue

Заменил ArrayList собираемых данных с poll сервера на потокобезопасную Queue, что позволило запустить паралельно с слушателем событий обработчик событий, при этом не теряя сообщения
This commit is contained in:
Mark Struchkov 2018-12-31 01:12:25 +03:00
parent edebc5c4fa
commit 58d2e7d3bc
3 changed files with 13 additions and 15 deletions

View File

@ -2,7 +2,7 @@ package org.sadtech.vkbot.listener.data;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import java.util.List; import java.util.Queue;
public interface ResponsibleData { public interface ResponsibleData {
@ -12,6 +12,6 @@ public interface ResponsibleData {
void cleanAll(); void cleanAll();
List<JsonObject> getJsonObjects(); Queue<JsonObject> getJsonObjects();
} }

View File

@ -4,17 +4,17 @@ import com.google.gson.JsonObject;
import org.sadtech.vkbot.listener.data.ResponsibleData; import org.sadtech.vkbot.listener.data.ResponsibleData;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.Queue;
import java.util.List; import java.util.concurrent.ConcurrentLinkedQueue;
@Component @Component
public class ResponseDataVk implements ResponsibleData { public class ResponseDataVk implements ResponsibleData {
private List<JsonObject> jsonObjects = new ArrayList<JsonObject>(); private Queue<JsonObject> jsonObjects = new ConcurrentLinkedQueue<JsonObject>();
@Override @Override
public void add(JsonObject jsonObject) { public void add(JsonObject jsonObject) {
jsonObjects.add(jsonObject); jsonObjects.offer(jsonObject);
} }
@Override @Override
@ -27,7 +27,7 @@ public class ResponseDataVk implements ResponsibleData {
jsonObjects.clear(); jsonObjects.clear();
} }
public List<JsonObject> getJsonObjects() { public Queue<JsonObject> getJsonObjects() {
return jsonObjects; return jsonObjects;
} }
} }

View File

@ -5,6 +5,7 @@ import lombok.extern.log4j.Log4j;
import org.sadtech.vkbot.listener.Observable; import org.sadtech.vkbot.listener.Observable;
import org.sadtech.vkbot.listener.Observer; import org.sadtech.vkbot.listener.Observer;
import org.sadtech.vkbot.listener.data.ResponsibleData; import org.sadtech.vkbot.listener.data.ResponsibleData;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -17,21 +18,18 @@ public class DispetcherHandler implements Observable {
private ResponsibleData date; private ResponsibleData date;
private List<Observer> observers = new ArrayList<Observer>(); private List<Observer> observers = new ArrayList<Observer>();
private List<JsonObject> objects;
public DispetcherHandler(ResponsibleData date) { public DispetcherHandler(ResponsibleData date) {
this.date = date; this.date = date;
} }
@Scheduled(fixedRate = 5000) @Async
public void packaging() { public void packaging() {
while (true) {
objects = new ArrayList<JsonObject>(date.getJsonObjects()); if (date.getJsonObjects().peek() != null) {
date.cleanAll(); log.info(date.getJsonObjects().poll());
for (JsonObject object : objects) { }
log.info(object);
} }
} }
@Override @Override