Process operations asynchronously (#15801)

Resolves #14927

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2023-11-03 19:29:05 +01:00 committed by GitHub
parent b503382e93
commit 2df66bfee2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,10 +21,13 @@ import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.common.NamedThreadFactory;
import org.openhab.core.config.core.ConfigurableService;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.items.GroupItem;
@ -71,6 +74,9 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
private final ItemRegistry itemRegistry;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1,
new NamedThreadFactory(JdbcPersistenceServiceConstants.SERVICE_ID));
@Activate
public JdbcPersistenceService(final @Reference ItemRegistry itemRegistry,
final @Reference TimeZoneProvider timeZoneProvider) {
@ -131,21 +137,21 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
@Override
public void store(Item item) {
internalStore(item, null, item.getState());
scheduler.execute(() -> internalStore(item, null, item.getState()));
}
@Override
public void store(Item item, @Nullable String alias) {
// alias is not supported
internalStore(item, null, item.getState());
scheduler.execute(() -> internalStore(item, null, item.getState()));
}
@Override
public void store(Item item, ZonedDateTime date, State state) {
internalStore(item, date, state);
scheduler.execute(() -> internalStore(item, date, state));
}
private void internalStore(Item item, @Nullable ZonedDateTime date, State state) {
private synchronized void internalStore(Item item, @Nullable ZonedDateTime date, State state) {
// Do not store undefined/uninitialized data
if (state instanceof UnDefType) {
logger.debug("JDBC::store: ignore Item '{}' because it is UnDefType", item.getName());