Search in sources :

Example 1 with TopicMetadata

use of org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata 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 2 with TopicMetadata

use of org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata in project incubator-eventmesh by apache.

the class StandaloneBroker method createTopicIfAbsent.

/**
 * if topic not exist, create a topic
 *
 * @param topicName topicName
 * @return messageQueue and offset
 */
public Pair<MessageQueue, AtomicLong> createTopicIfAbsent(String topicName) {
    TopicMetadata topicMetadata = new TopicMetadata(topicName);
    MessageQueue messageQueue = messageContainer.computeIfAbsent(topicMetadata, k -> new MessageQueue());
    AtomicLong offset = offsetMap.computeIfAbsent(topicMetadata, k -> new AtomicLong());
    return Pair.of(messageQueue, offset);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) TopicMetadata(org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata)

Example 3 with TopicMetadata

use of org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata in project incubator-eventmesh by apache.

the class StandaloneBroker method putMessage.

/**
 * put message
 *
 * @param topicName topic name
 * @param message   message
 * @throws InterruptedException
 */
public MessageEntity putMessage(String topicName, CloudEvent message) throws InterruptedException {
    Pair<MessageQueue, AtomicLong> pair = createTopicIfAbsent(topicName);
    AtomicLong topicOffset = pair.getRight();
    MessageQueue messageQueue = pair.getLeft();
    MessageEntity messageEntity = new MessageEntity(new TopicMetadata(topicName), message, topicOffset.getAndIncrement(), System.currentTimeMillis());
    messageQueue.put(messageEntity);
    return messageEntity;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageEntity(org.apache.eventmesh.connector.standalone.broker.model.MessageEntity) TopicMetadata(org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata)

Example 4 with TopicMetadata

use of org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata in project incubator-eventmesh by apache.

the class StandaloneBroker method getMessage.

/**
 * Get the message by offset
 *
 * @param topicName topic name
 * @param offset    offset
 * @return CloudEvent
 */
public CloudEvent getMessage(String topicName, long offset) {
    TopicMetadata topicMetadata = new TopicMetadata(topicName);
    MessageEntity messageEntity = messageContainer.computeIfAbsent(topicMetadata, k -> new MessageQueue()).getByOffset(offset);
    if (messageEntity == null) {
        return null;
    }
    return messageEntity.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)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)4 TopicMetadata (org.apache.eventmesh.connector.standalone.broker.model.TopicMetadata)4 MessageEntity (org.apache.eventmesh.connector.standalone.broker.model.MessageEntity)3 CloudEvent (io.cloudevents.CloudEvent)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Pair (org.apache.commons.lang3.tuple.Pair)2 HistoryMessageClearTask (org.apache.eventmesh.connector.standalone.broker.task.HistoryMessageClearTask)2