Addition of taskmanager plus 50ms delay between queries.
diff --git java/org/l2jmobius/gameserver/TradeController.java java/org/l2jmobius/gameserver/TradeController.java
index 6b82a09..c0c6a9c 100644
--- java/org/l2jmobius/gameserver/TradeController.java
+++ java/org/l2jmobius/gameserver/TradeController.java
@@ -26,11 +26,11 @@
import java.util.logging.Logger;
import org.l2jmobius.Config;
-import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.commons.database.DatabaseFactory;
import org.l2jmobius.gameserver.datatables.ItemTable;
import org.l2jmobius.gameserver.model.StoreTradeList;
import org.l2jmobius.gameserver.model.items.instance.ItemInstance;
+import org.l2jmobius.gameserver.taskmanager.BuyListTaskManager;
/**
* @version $Revision: 1.5.4.13 $ $Date: 2005/04/06 16:13:38 $
@@ -43,31 +43,6 @@
private final Map<Integer, StoreTradeList> _lists;
private final Map<Integer, StoreTradeList> _listsTaskItem;
- /** Task launching the function for restore count of Item (Clan Hall) */
- public class RestoreCount implements Runnable
- {
- private final int _timer;
-
- public RestoreCount(int time)
- {
- _timer = time;
- }
-
- @Override
- public void run()
- {
- try
- {
- restoreCount(_timer);
- dataTimerSave(_timer);
- ThreadPool.schedule(new RestoreCount(_timer), _timer * 60 * 60 * 1000);
- }
- catch (Throwable t)
- {
- }
- }
- }
-
private TradeController()
{
boolean limitedItem = false;
@@ -214,11 +189,11 @@
savetimer = rset2.getLong("savetimer");
if ((savetimer - currentMillis) > 0)
{
- ThreadPool.schedule(new RestoreCount(time), savetimer - System.currentTimeMillis());
+ BuyListTaskManager.getInstance().addTime(time, savetimer);
}
else
{
- ThreadPool.schedule(new RestoreCount(time), 0);
+ BuyListTaskManager.getInstance().addTime(time, 0);
}
}
rset2.close();
@@ -375,11 +350,11 @@
savetimer = rset2.getLong("savetimer");
if ((savetimer - currentMillis) > 0)
{
- ThreadPool.schedule(new RestoreCount(time), savetimer - System.currentTimeMillis());
+ BuyListTaskManager.getInstance().addTime(time, savetimer);
}
else
{
- ThreadPool.schedule(new RestoreCount(time), 0);
+ BuyListTaskManager.getInstance().addTime(time, 0);
}
}
rset2.close();
@@ -436,7 +411,7 @@
return lists;
}
- protected void restoreCount(int time)
+ public void restoreCount(int time)
{
if (_listsTaskItem == null)
{
@@ -449,12 +424,12 @@
}
}
- protected void dataTimerSave(int time)
+ public void dataTimerSave(int time)
{
final long timerSave = System.currentTimeMillis() + (time * 60 * 60 * 1000);
try (Connection con = DatabaseFactory.getConnection())
{
- final PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET savetimer =? WHERE time =?");
+ final PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET savetimer=? WHERE time=?");
statement.setLong(1, timerSave);
statement.setInt(2, time);
statement.executeUpdate();
@@ -485,13 +460,13 @@
}
listId = list.getListId();
- for (ItemInstance Item : list.getItems())
+ for (ItemInstance item : list.getItems())
{
- if (Item.getCount() < Item.getInitCount()) // needed?
+ if (item.getCount() < item.getInitCount()) // needed?
{
statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount=? WHERE item_id=? AND shop_id=?");
- statement.setInt(1, Item.getCount());
- statement.setInt(2, Item.getItemId());
+ statement.setInt(1, item.getCount());
+ statement.setInt(2, item.getItemId());
statement.setInt(3, listId);
statement.executeUpdate();
statement.close();
diff --git java/org/l2jmobius/gameserver/taskmanager/BuyListTaskManager.java java/org/l2jmobius/gameserver/taskmanager/BuyListTaskManager.java
new file mode 100644
index 0000000..c30c3de
--- /dev/null
+++ java/org/l2jmobius/gameserver/taskmanager/BuyListTaskManager.java
@@ -0,0 +1,114 @@
+/*
+ * This file is part of the L2J Mobius project.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.l2jmobius.gameserver.taskmanager;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.l2jmobius.commons.concurrent.ThreadPool;
+import org.l2jmobius.gameserver.TradeController;
+
+/**
+ * @author Mobius
+ */
+public class BuyListTaskManager
+{
+ private static final Map<Integer, Long> REFRESH_TIME = new ConcurrentHashMap<>();
+ private static final List<Integer> PENDING_UPDATES = new ArrayList<>();
+ private static boolean _workingTimes = false;
+ private static boolean _workingSaves = false;
+
+ public BuyListTaskManager()
+ {
+ ThreadPool.scheduleAtFixedRate(() ->
+ {
+ if (_workingTimes)
+ {
+ return;
+ }
+ _workingTimes = true;
+
+ final long currentTime = System.currentTimeMillis();
+ for (Entry<Integer, Long> entry : REFRESH_TIME.entrySet())
+ {
+ if (currentTime > entry.getValue().longValue())
+ {
+ final Integer time = entry.getKey();
+ synchronized (PENDING_UPDATES)
+ {
+ PENDING_UPDATES.add(time);
+ }
+ REFRESH_TIME.put(time, currentTime + (time.intValue() * 60 * 60 * 1000L));
+ }
+ }
+
+ _workingTimes = false;
+ }, 1000, 60000);
+
+ ThreadPool.scheduleAtFixedRate(() ->
+ {
+ if (_workingSaves)
+ {
+ return;
+ }
+ _workingSaves = true;
+
+ if (!PENDING_UPDATES.isEmpty())
+ {
+ final Integer time;
+ synchronized (PENDING_UPDATES)
+ {
+ time = PENDING_UPDATES.get(0);
+ PENDING_UPDATES.remove(time);
+ }
+ TradeController.getInstance().restoreCount(time.intValue());
+ TradeController.getInstance().dataTimerSave(time.intValue());
+ }
+
+ _workingSaves = false;
+ }, 50, 50);
+ }
+
+ public void addTime(int time, long refreshTime)
+ {
+ if (refreshTime == 0)
+ {
+ synchronized (PENDING_UPDATES)
+ {
+ PENDING_UPDATES.add(time);
+ }
+ REFRESH_TIME.put(time, System.currentTimeMillis() + (time * 60 * 60 * 1000L));
+ }
+ else
+ {
+ REFRESH_TIME.put(time, refreshTime);
+ }
+ }
+
+ public static BuyListTaskManager getInstance()
+ {
+ return SingletonHolder.INSTANCE;
+ }
+
+ private static class SingletonHolder
+ {
+ protected static final BuyListTaskManager INSTANCE = new BuyListTaskManager();
+ }
+}