Создаю бота на spring boot для telegram, есть ArrayList с именем postLinks, ArrayList заполняется элементами в takeAllPosts, это видно по логам, но при запросе размера ArrayList в checker(), размер равен 0. В чем я не прав?
@Component
public class Bot extends TelegramLongPollingBot {
private final String BOT_USERNAME = "BOT_USERNAME";
private final String BOT_TOKEN = "BOT_TOKEN";
private final String PARSE_LINK = "https://www.bolha.com/oddaja-stanovanja/maribor";
private final String CHANNEL_ID = "CHANNEL_ID";
private final long MY_ID = MY_ID;
private final Logger log = LoggerFactory.getLogger(Bot.class);
private ArrayList <String> postLinks = new ArrayList<>();
@Override
public void onUpdateReceived(Update update) {
if (update.hasMessage() && update.getMessage().hasText() && update.getMessage().getChatId().equals(MY_ID)) {
if (update.getMessage().getText().equals("/start")) {
sendMsg(update.getMessage().getChatId(), "Bot stared.");
takeAllPosts();
sendMsg(update.getMessage().getChatId(), "Take " + postLinks.size() + " posts.");//РАЗМЕР НОРМАЛЬНЫЙ
} else {
sendMsg(update.getMessage().getChatId(), "Wrong command.");
}
} else {
sendMsg(update.getMessage().getChatId(), "Access denied.");
}
}
@Override
public String getBotUsername() {
return BOT_USERNAME;
}
@Override
public String getBotToken() {
return BOT_TOKEN;
}
// Check new post every 10 sec
@Scheduled(fixedDelay = 10000, initialDelay = 5000 * 5)
public void checker() {
String link = "";
try {
Document page = Jsoup.connect(PARSE_LINK).get();
Elements linkElements = page.select("h3.entity-title > a");
link = linkElements.get(3).attr("href");
} catch (Exception e) {
e.printStackTrace();
}
log.info("size is:" + postLinks.size()); //РАЗМЕР 0, ПОЧЕМУ???
if (!link.equals(postLinks.get(postLinks.size() - 1))) {
postLinks.add(link);
sendPost("https://www.bolha.com".concat(link));
}
log.info("link is: " + link);
}
private synchronized void sendMsg(Long chatId, String msg) {
SendMessage helloMessage = new SendMessage();
helloMessage.setChatId(chatId);
helloMessage.setText(msg);
try {
execute(helloMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
private void takeAllPosts() {
int pageNumber = 7;
for (int i = 1 ; i <= pageNumber; i++) {
try {
Document page = Jsoup.connect("https://www.bolha.com/oddaja-stanovanja/maribor?page=" + i).get();
Elements linkElements = page.select("h3.entity-title > a");
for (int j = (i == 1 ? 2 : 0); j < linkElements.size() - 6; j++) {
String link = linkElements.get(j).attr("href");
postLinks.add(link);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Collections.reverse(postLinks);
}
}