com.arangodb.vst.internal.MessageStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vst-protocol Show documentation
Show all versions of vst-protocol Show documentation
VST Protocol module for ArangoDB Java Driver
/*
* DISCLAIMER
*
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright holder is ArangoDB GmbH, Cologne, Germany
*/
package com.arangodb.vst.internal;
import com.arangodb.ArangoDBException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.FutureTask;
/**
* @author Mark Vollmary
*/
public class MessageStore {
private static final Logger LOGGER = LoggerFactory.getLogger(MessageStore.class);
private final Map> task;
private final Map response;
private final Map error;
public MessageStore() {
super();
task = new ConcurrentHashMap<>();
response = new ConcurrentHashMap<>();
error = new ConcurrentHashMap<>();
}
public void storeMessage(final long messageId, final FutureTask future) {
task.put(messageId, future);
}
public void consume(final Message message) {
final FutureTask future = task.remove(message.getId());
if (future != null) {
response.put(message.getId(), message);
future.run();
}
}
public Message get(final long messageId) {
final Message result = response.remove(messageId);
if (result == null) {
final Exception e = error.remove(messageId);
if (e != null) {
throw ArangoDBException.of(e);
}
}
return result;
}
public void cancel(final long messageId) {
final FutureTask future = task.remove(messageId);
if (future != null) {
LOGGER.error("Cancel Message unexpected (id={}).", messageId);
future.cancel(true);
}
}
public synchronized void clear(final Exception e) {
if (!task.isEmpty()) {
LOGGER.error(e.getMessage(), e);
}
for (final Entry> entry : task.entrySet()) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Exceptionally complete Message (id=%s).", entry.getKey()));
}
error.put(entry.getKey(), e);
entry.getValue().run();
}
task.clear();
}
public boolean isEmpty() {
return task.isEmpty();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy