config manager: defer submitting config changes to config service
This lesses the spam generated from config changes from JSpinners
This commit is contained in:
@@ -45,11 +45,13 @@ import java.nio.channels.FileLock;
|
|||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
@@ -71,8 +73,7 @@ public class ConfigManager
|
|||||||
@Inject
|
@Inject
|
||||||
EventBus eventBus;
|
EventBus eventBus;
|
||||||
|
|
||||||
@Inject
|
private final ScheduledExecutorService executor;
|
||||||
ScheduledExecutorService executor;
|
|
||||||
|
|
||||||
private AccountSession session;
|
private AccountSession session;
|
||||||
private ConfigClient client;
|
private ConfigClient client;
|
||||||
@@ -80,10 +81,15 @@ public class ConfigManager
|
|||||||
|
|
||||||
private final ConfigInvocationHandler handler = new ConfigInvocationHandler(this);
|
private final ConfigInvocationHandler handler = new ConfigInvocationHandler(this);
|
||||||
private final Properties properties = new Properties();
|
private final Properties properties = new Properties();
|
||||||
|
private final Map<String, String> pendingChanges = new HashMap<>();
|
||||||
|
|
||||||
public ConfigManager()
|
@Inject
|
||||||
|
public ConfigManager(ScheduledExecutorService scheduledExecutorService)
|
||||||
{
|
{
|
||||||
|
this.executor = scheduledExecutorService;
|
||||||
this.propertiesFile = getPropertiesFile();
|
this.propertiesFile = getPropertiesFile();
|
||||||
|
|
||||||
|
executor.scheduleWithFixedDelay(this::sendConfig, 30, 30, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void switchSession(AccountSession session)
|
public final void switchSession(AccountSession session)
|
||||||
@@ -296,9 +302,9 @@ public class ConfigManager
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (client != null)
|
synchronized (pendingChanges)
|
||||||
{
|
{
|
||||||
client.set(groupName + "." + key, value);
|
pendingChanges.put(groupName + "." + key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Runnable task = () ->
|
Runnable task = () ->
|
||||||
@@ -339,9 +345,9 @@ public class ConfigManager
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (client != null)
|
synchronized (pendingChanges)
|
||||||
{
|
{
|
||||||
client.unset(groupName + "." + key);
|
pendingChanges.remove(groupName + "." + key);
|
||||||
}
|
}
|
||||||
|
|
||||||
Runnable task = () ->
|
Runnable task = () ->
|
||||||
@@ -554,4 +560,29 @@ public class ConfigManager
|
|||||||
}
|
}
|
||||||
return object.toString();
|
return object.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendConfig()
|
||||||
|
{
|
||||||
|
synchronized (pendingChanges)
|
||||||
|
{
|
||||||
|
if (client != null)
|
||||||
|
{
|
||||||
|
for (Map.Entry<String, String> entry : pendingChanges.entrySet())
|
||||||
|
{
|
||||||
|
String key = entry.getKey();
|
||||||
|
String value = entry.getValue();
|
||||||
|
|
||||||
|
if (Strings.isNullOrEmpty(value))
|
||||||
|
{
|
||||||
|
client.unset(key);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
client.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pendingChanges.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user