发布/订阅主题
A 主题 包含消息本身,以及其他订阅者和发布者信息。 这 ITopic/Topic
interface in Pub/Sub facilitates 创建一个主题, 创建时设置主题优先级, 找个话题, 简单地删除一个主题 和 异步.
备注
此功能也可用于 NCache Professional.
先决条件
创建主题
创建主题 方法在缓存中创建一个具有指定名称的主题。 如果主题已经存在,则返回主题的实例 ITopic
。 每当在某个主题上发布消息时,都会根据消息首选项将其传递给在该主题上注册的订阅者。 以下示例创建一个主题 订单主题.
// Precondition: Cache is already connected
// Mention the name of the topic
string topicName = "orderTopic";
// Create the topic
ITopic topic = cache.MessagingService.CreateTopic(topicName);
// Precondition: Cache is already connected
// Mention the name of the topic
String topicName = "orderTopic";
// Create the topic
Topic topic = cache.getMessagingService().createTopic(topicName);
// This is an async method
// Precondition: Cache is already connected
// Mention the name of the topic
let topicName = "orderTopic";
// Create the topic
let topic = await ncache.getMessagingService().createTopic(topicName);
# Precondition: Cache is already connected
# Mention the name of the topic
topic_name = "orderTopic"
# Create the topic
topic = cache.get_messaging_service().create_topic(topic_name)
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
创建优先主题
NCache Pub/Sub allows you to set 主题优先 优先传递关键消息。 优先级是使用指定的 TopicPriority
财产作为 Low
, Normal
及 High
。 如果启用驱逐,则首先驱逐优先级最低的主题,最后驱逐优先级高的主题。 未指定优先级创建的主题将使用默认优先级(即“正常”)创建。
重要
主题的优先级只能在创建主题时指定,之后无法修改。
以下示例创建一个主题, 订单主题,优先级尽可能高,以避免提前驱逐(如果启用了驱逐)。
// Mention the name of the topic
string topicName = "orderTopic";
// Create the topic with priority
ITopic topic = cache.MessagingService.CreateTopic(topicName, Alachisoft.NCache.Runtime.Messaging.TopicPriority.High);
// Mention name of the topic
String topicName = "orderTopic";
// Create topic with priority
Topic topic = cache.getMessagingService().createTopic(topicName, TopicPriority.High);
// This is an async method
// Mention name of the topic
let topicName = "orderTopic";
// Create topic with priority
let topic = await ncache
.getMessagingService()
.createTopic(topicName, ncache.TopicPriority.High);
# Mention the name of the topic
topic_name = "orderTopic"
# Create the topic
topic = cache.get_messaging_service().create_topic(topic_name, ncache.TopicPriority.HIGH)
获取主题
获取主题 方法从缓存中获取指定主题的实例。 如果主题存在,则返回,否则抛出异常。 以下示例获取现有主题 订单主题 从缓存中。
// Mention the name of the topic
string topicName = "orderTopic";
// Get the topic from the cache
ITopic orderTopic = cache.MessagingService.GetTopic(topicName);
// Verify successful topic retrieval
if (orderTopic != null)
{
// orderTopic will be used for receiving and/or publishing messages
}
else
{
// No topic exists
}
// Mention name of the topic
String topicName = "orderTopic";
// Get the topic
Topic orderTopic = cache.getMessagingService().getTopic(topicName);
// Verify successful topic retrieval
if (orderTopic != null) {
// orderTopic will be used for receiving and/or publishing messages
} else {
// No topic exists
}
// This is an async method
// Mention name of the topic
let topicName = "orderTopic";
// Get the topic
let orderTopic = await ncache.getMessagingService().getTopic(topicName);
// Verify successful topic retrieval
if (!(orderTopic == null)) {
// orderTopic will be used for receiving and/or publishing messages
} else {
// No topic exists
}
# Mention name of the topic
topic_name = "orderTopic"
# Get the topic
order_topic = cache.get_messaging_service().get_topic(topic_name)
# Verify successful topic retrieval
if order_topic is not None:
# order_topic will be used for receiving and/or publishing messages
print("Topic found")
else:
# No topic exists
print("Topic not found")
删除主题
删除主题 方法从缓存中取消注册主题并删除与该主题关联的所有消息。 如果已注册,则主题删除回调 OnTopicDeleted
将在此方法调用时触发。
以下示例删除现有主题 订单主题 从缓存中删除并删除与该主题关联的所有消息。 如果 OnTopicDeleted
回调已注册,它将在此方法调用时触发。
// Define the topic to be deleted
string topicName = "orderTopic";
// Delete the topic "orderTopic"
cache.MessagingService.DeleteTopic(topicName);
// Callback will be triggered if registered
// Mention the topic to be deleted
String topicName = "orderTopic";
// Delete the topic
cache.getMessagingService().deleteTopic(topicName);
// Callback will be triggered if registered
// Mention the topic to be deleted
let topicName = "orderTopic";
// Delete the topic
ncache.getMessagingService().deleteTopic(topicName);
// Callback will be triggered if registered
# Mention the topic to be deleted
topic_name = "orderTopic"
# Delete the topic
cache.get_messaging_service().delete_topic(topic_name)
# Callback will be triggered if registered
异步删除主题
删除主题异步 方法异步删除主题。 每当一个主题被异步删除时, Task
返回给用户以执行进一步的任务,而无需等待主题被删除。 以下示例展示了主题的异步删除 orderTopic
.
// Define the topic to be deleted
string topicName = "orderTopic";
// Delete the topic "orderTopic"
Task task = cache.MessagingService.DeleteTopicAsync(topicName);
// Use task to perform further operations according to business logic
// Callback will be triggered if registered
// Mention the topic to be deleted
String topicName = "orderTopic";
// Delete the topic asynchronously
TimeScheduler.Task task = (TimeScheduler.Task) cache.getMessagingService().deleteTopicAsync(topicName);
// Use tasks to perform further operations according to business logic
// Callback will be triggered if registered
# Mention the topic to be deleted
topic_name = "orderTopic"
# Delete the topic asynchronously
async def delete_async():
task = cache.get_messaging_service().delete_topic_async(topic_name)
result = await task
asyncio.run(delete _async())
# Use tasks to perform further operations according to business logic
# Callback will be triggered if registered
ITopic 接口的属性
成员 |
Type |
产品描述 |
MessageDeliveryFailure |
MessageDeliveryFailureCallback |
Event on topic so that publisher receives all failed messages that are not delivered to any subscriber or maybe messages are expired or evicted before delivery. |
Name |
string |
创建主题时指定的主题名称。 |
OnTopicDeleted |
TopicDeletedCallback |
处理发布者和订阅者删除主题的事件。 |
ExpirationTime |
TimeSpan |
如果未提供消息级别过期,则此主题级别过期默认使主题中的消息过期。 值为 TimeSpan.MaxValue 默认情况下。 |
IsClosed |
bool |
在执行任何操作之前,请检查主题是否已处理。 |
Dispose |
IDisposable |
从缓存服务器中删除已注册主题的订阅。 |
更多资讯
NCache 为 Pub/Sub 提供了一个示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache.运行时.缓存 命名空间。
Java的: COM。alachisoft.ncache.runtime.caching 命名空间。
节点.js: 话题 类。
Python: ncache.client.services 类。