com.jn.dmmq.core.MessageTopicDispatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dmmq-core Show documentation
Show all versions of dmmq-core Show documentation
A memory , mini Message Queue built by disruptor
The newest version!
package com.jn.dmmq.core;
import com.jn.dmmq.core.event.TopicEvent;
import com.jn.dmmq.core.event.TopicEventType;
import com.jn.langx.annotation.NonNull;
import com.jn.langx.event.EventPublisher;
import com.jn.langx.lifecycle.Lifecycle;
import com.jn.langx.util.Objects;
import com.jn.langx.util.Preconditions;
import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.function.Consumer2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
public class MessageTopicDispatcher implements Lifecycle {
private static final Logger logger = LoggerFactory.getLogger(MessageTopicDispatcher.class);
private final Map topicMap = Collects.emptyHashMap();
private EventPublisher topicEventPublisher;
private volatile boolean running = false;
public MessageTopicDispatcher() {
}
public EventPublisher getTopicEventPublisher() {
return topicEventPublisher;
}
public void setTopicEventPublisher(EventPublisher topicEventPublisher) {
this.topicEventPublisher = topicEventPublisher;
}
public List getTopicNames() {
return Collects.newArrayList(topicMap.keySet());
}
public void registerTopic(@NonNull MessageTopic messageTopic) {
Preconditions.checkNotNull(messageTopic);
topicMap.put(messageTopic.getName(), messageTopic);
topicEventPublisher.publish(new TopicEvent(messageTopic, TopicEventType.ADD));
}
public void unregisterTopic(String name) {
Preconditions.checkNotNull(name);
MessageTopic topic = topicMap.remove(name);
if (topic != null) {
topicEventPublisher.publish(new TopicEvent(topic, TopicEventType.REMOVE));
}
}
public void publish(String topicName, final Object message) {
if (!running) {
logger.warn("Publish message to topic {} fail, the message topic dispatcher is not running", topicName);
}
if ("*".equals(topicName)) {
Collects.forEach(topicMap, new Consumer2() {
@Override
public void accept(String key, MessageTopic topic) {
topic.publish(message);
}
});
}
MessageTopic topic = topicMap.get(topicName);
if (Objects.isNull(topic)) {
logger.warn("Can't find the specified topic : {}", topicName);
} else {
topic.publish(message);
}
}
public void subscribe(String topicName, final Consumer consumer, final String... dependencies) {
if ("*".equals(topicName)) {
Collects.forEach(topicMap, new Consumer2() {
@Override
public void accept(String key, MessageTopic topic) {
topic.subscribe(consumer, dependencies);
}
});
} else {
MessageTopic topic = topicMap.get(topicName);
if (Objects.isNull(topic)) {
logger.warn("Can't find a topic : {}", topicName);
} else {
topic.subscribe(consumer, dependencies);
}
}
}
@Override
public void startup() {
if (!running) {
Collects.forEach(topicMap, new Consumer2() {
@Override
public void accept(String key, MessageTopic value) {
value.startup();
}
});
running = true;
}
}
@Override
public void shutdown() {
running = false;
Collects.forEach(topicMap, new Consumer2() {
@Override
public void accept(String key, MessageTopic value) {
try {
value.shutdown();
} catch (Throwable ex) {
logger.error("error:{}, stack:{}", ex.getMessage(), ex);
}
}
});
}
}