use of org.apache.eventmesh.connector.standalone.broker.model.MessageEntity in project incubator-eventmesh by apache.
the class MessageQueue method dequeue.
private MessageEntity dequeue() {
MessageEntity item = items[takeIndex++];
if (takeIndex == items.length) {
takeIndex = 0;
}
notFull.signal();
return item;
}
use of org.apache.eventmesh.connector.standalone.broker.model.MessageEntity in project incubator-eventmesh by apache.
the class StandaloneBroker method getMessage.
/**
* Get the message, if the queue is empty return null
*
* @param topicName
*/
public CloudEvent getMessage(String topicName) {
TopicMetadata topicMetadata = new TopicMetadata(topicName);
MessageEntity head = messageContainer.computeIfAbsent(topicMetadata, k -> new MessageQueue()).getHead();
if (head == null) {
return null;
}
return head.getMessage();
}
use of org.apache.eventmesh.connector.standalone.broker.model.MessageEntity in project incubator-eventmesh by apache.
the class HistoryMessageClearTask method run.
@Override
public void run() {
while (true) {
messageContainer.forEach((topicMetadata, messageQueue) -> {
long currentTimeMillis = System.currentTimeMillis();
MessageEntity oldestMessage = messageQueue.getHead();
if (oldestMessage == null) {
return;
}
if (currentTimeMillis - oldestMessage.getCreateTimeMills() >= MESSAGE_STORE_WINDOW) {
messageQueue.removeHead();
}
});
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
} catch (InterruptedException e) {
logger.error("Thread is interrupted, thread name: {}", Thread.currentThread().getName(), e);
Thread.currentThread().interrupt();
}
}
}
use of org.apache.eventmesh.connector.standalone.broker.model.MessageEntity in project incubator-eventmesh by apache.
the class StandaloneProducer method publish.
public SendResult publish(CloudEvent cloudEvent) {
Preconditions.checkNotNull(cloudEvent);
try {
MessageEntity messageEntity = standaloneBroker.putMessage(cloudEvent.getSubject(), cloudEvent);
SendResult sendResult = new SendResult();
sendResult.setTopic(cloudEvent.getSubject());
sendResult.setMessageId(String.valueOf(messageEntity.getOffset()));
return sendResult;
} catch (Exception e) {
logger.error("send message error, topic: {}", cloudEvent.getSubject(), e);
throw new ConnectorRuntimeException(String.format("Send message error, topic: %s", cloudEvent.getSubject()));
}
}
use of org.apache.eventmesh.connector.standalone.broker.model.MessageEntity in project incubator-eventmesh by apache.
the class MessageQueue method getByOffset.
/**
* Get the message by offset, since the offset is increment, so we can get the first message in this queue
* and calculate the index of this offset
*
* @param offset
* @return MessageEntity
*/
public MessageEntity getByOffset(long offset) {
ReentrantLock lock = this.lock;
lock.lock();
try {
MessageEntity head = getHead();
if (head == null) {
return null;
}
if (head.getOffset() > offset) {
throw new RuntimeException(String.format("The message has been deleted, offset: %s", offset));
}
MessageEntity tail = getTail();
if (tail == null || tail.getOffset() < offset) {
return null;
}
int offsetDis = (int) (head.getOffset() - offset);
int offsetIndex = takeIndex - offsetDis;
if (offsetIndex < 0) {
offsetIndex += items.length;
}
return itemAt(offsetIndex);
} finally {
lock.unlock();
}
}
Aggregations