Search in sources :

Example 1 with MessageEntity

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;
}
Also used : MessageEntity(org.apache.eventmesh.connector.standalone.broker.model.MessageEntity)

Example 2 with MessageEntity

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();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Pair(org.apache.commons.lang3.tuple.Pair) CloudEvent(io.cloudevents.CloudEvent) MessageEntity(org.apache.eventmesh.connector.standalone.broker.model.MessageEntity) HistoryMessageClearTask(org.apache.eventmesh.connector.standalone.broker.task.HistoryMessageClearTask) TopicMetadata(org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MessageEntity(org.apache.eventmesh.connector.standalone.broker.model.MessageEntity) TopicMetadata(org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata)

Example 3 with MessageEntity

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();
        }
    }
}
Also used : MessageEntity(org.apache.eventmesh.connector.standalone.broker.model.MessageEntity)

Example 4 with MessageEntity

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()));
    }
}
Also used : ConnectorRuntimeException(org.apache.eventmesh.api.exception.ConnectorRuntimeException) MessageEntity(org.apache.eventmesh.connector.standalone.broker.model.MessageEntity) SendResult(org.apache.eventmesh.api.SendResult) ConnectorRuntimeException(org.apache.eventmesh.api.exception.ConnectorRuntimeException)

Example 5 with MessageEntity

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();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) MessageEntity(org.apache.eventmesh.connector.standalone.broker.model.MessageEntity)

Aggregations

MessageEntity (org.apache.eventmesh.connector.standalone.broker.model.MessageEntity)8 CloudEvent (io.cloudevents.CloudEvent)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 TopicMetadata (org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Pair (org.apache.commons.lang3.tuple.Pair)2 HistoryMessageClearTask (org.apache.eventmesh.connector.standalone.broker.task.HistoryMessageClearTask)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 SendResult (org.apache.eventmesh.api.SendResult)1 ConnectorRuntimeException (org.apache.eventmesh.api.exception.ConnectorRuntimeException)1 Test (org.junit.Test)1