오늘날 많은 서버 애플리케이션은 다른 애플리케이션과의 작업을 조정하고 비동기 방식으로 메시지와 데이터를 공유해야 합니다. 그리고 이는 종종 트랜잭션이 많은 환경에서 수행됩니다. 높은 트랜잭션 환경의 경우, NCache 이들에게 이상적인 후보입니다 비동기 이벤트 중심 통신.
NCache 다음을 포함하여 이러한 애플리케이션의 요구 사항을 충족할 수 있는 풍부한 메시징 및 스트림 기능 세트를 제공합니다. 게시/구독 메시징, 연속 쿼리, 아이템 레벨 이벤트및 캐시 레벨 이벤트. 각각은 아래에 설명되어 있습니다.
NCache 강력한 제공 게시/구독 메시징 게시자 애플리케이션이 구독자가 누구인지 알지 못한 채 메시지를 보낼 수 있는 애플리케이션용 플랫폼입니다. 메시지는 다음과 같이 분류될 수 있습니다. 이상의 주제 그런 다음 구독자는 특정 주제를 구독하고 이러한 메시지를 비동기적으로 받을 수 있습니다.
당신은 쉽게 할 수 사용 NCache 분산 애플리케이션을 위한 Pub/Sub 메시징 Pub/Sub 메시징의 구성요소와 작동 방식을 이해한 후
아래는 에 대한 소스 코드 예입니다. 게시자가 메시지를 게시하는 방법 주제로 이동한 다음 구독자가 주제를 구독합니다..
ITopic topic = _cache.MessagingService.CreateTopic(topicName)
// Creating new message containing a “payload”
var message = new Message(payload);
// Set the expiration TimeSpan of the message
message.ExpirationTime = TimeSpan.FromSeconds(5000);
// Publishing the above created message.
topic.Publish(message, DeliveryOption.All, true);
// Get the already created topic to subscribe to it
ITopic orderTopic = cache.MessagingService.GetTopic(topic);
// Create and register subscribers for the topic created
// MessageRecieved callback is registered
orderTopic.CreateSubscription(MessageReceived);
String topicName = "orderUpdates";
Topic topic = cache.getMessagingService().createTopic(topicName);
// Creating new message containing a “payload”
Customer payload = new Customer("David Jones", "XYZCompany");
var message = new Message(payload);
// Set the expiration TimeSpan of the message
message.setExpirationTime(TimeSpan.FromSeconds(5000));
// Publishing the above created message.
topic.publish(message, DeliveryOption.All, true);
// Get the already created topic to subscribe to it
Topic orderTopic = cache.getMessagingService().getTopic(topicName);
// Create and register subscribers for the topic created
orderTopic.createSubscription(new MessageReceivedListener() {
@Override
public void onMessageReceived(Object sender, MessageEventArgs args) {
// message received
}
});
연속 쿼리 의 강력한 특징이다 NCache 이를 통해 SQL과 유사한 쿼리를 기반으로 분산 캐시의 "데이터 세트"에 대한 변경 사항을 모니터링할 수 있습니다. 캐시에 대해 SQL과 유사한 기준을 사용하여 연속 쿼리(CQ)를 생성하고 이를 NCache. NCache 그때 이 "데이터 세트"에 대한 변경 사항을 모니터링하기 시작합니다. 다음을 포함하는 캐시에:
다음은 특정 기준으로 연속 쿼리를 만들고 서버에 등록하는 방법입니다.
// Query for required operation
string query = "SELECT $VALUE$ FROM FQN.Customer WHERE Country = ?";
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("Country", "USA");
// Create Continuous Query
var cQuery = new ContinuousQuery(queryCommand);
// Register to be notified when a qualified item is added to the cache
cQuery.RegisterNotification(new QueryDataNotificationCallback(QueryItemCallBack), EventType.ItemAdded, EventDataFilter.None);
// Register continuousQuery on server
cache.MessagingService.RegisterCQ(cQuery);
String cacheName = "demoCache";
// Initialize an instance of the cache to begin performing operations:
Cache cache = CacheManager.getCache(cacheName);
// Query for required operation
String query = "SELECT $VALUE$ FROM com.alachisoft.ncache.sample.Customer WHERE country = ?";
var queryCommand = new QueryCommand(query);
queryCommand.getParameters().put("Country", "USA");
// Create Continuous Query
var cQuery = new ContinuousQuery(queryCommand);
// Register to be notified when a qualified item is added to the cache
cQuery.addDataModificationListener(new QueryDataModificationListener() {
@Override
public void onQueryDataModified(String key, CQEventArg args) {
switch (args.getEventType())
{
case ItemAdded:
// 'key' has been added to the cache
break;
}
}
}, EnumSet.allOf(EventType.class), EventDataFilter.None);
// Register continuousQuery on server
cache.getMessagingService().registerCQ(cQuery);
자세한 내용은 다음을 참조하십시오. 캐시 문서에서 연속 쿼리를 사용하는 방법.
클라이언트는 다음으로 등록합니다. NCache 수신 이벤트 알림 그들은 콜백 기능을 제공함으로써 관심을 갖고 있습니다. NCache 이벤트와 함께 반환되는 정보의 양을 지정하는 데 사용되는 이벤트와 함께 필터를 제공합니다. 이 필터는 없음(기본값), 메타 데이터 과 데이터와메타데이터. 다음 유형의 이벤트가 지원됩니다.
아래 코드는 아이템 레벨 이벤트 등록을 보여줍니다.
public static void RegisterItemLevelEvents()
{
// Get cache
string cacheName = "demoCache";
var cache = CacheManager.GetCache(cacheName);
// Register Item level events
string key = "Product:1001";
cache.MessagingService.RegisterCacheNotification(key, CacheDataNotificationCallbackImpl, EventType.ItemUpdated | EventType.ItemUpdated | EventType.ItemRemoved, EventDataFilter.DataWithMetadata);
}
public static void CacheDataNotificationCallbackImpl(string key, CacheEventArg cacheEventArgs)
{
switch (cacheEventArgs.EventType)
{
case EventType.ItemAdded:
// 'key' has been added to the cache
break;
case EventType.ItemUpdated:
// 'key' has been updated in the cache
break;
case EventType.ItemRemoved:
// 'key' has been removed from the cache
break;
}
}
private static void RegisterItemLevelEvents() throws Exception {
String cacheName = "demoCache";
// Initialize an instance of the cache to begin performing operations:
Cache cache = CacheManager.getCache(cacheName);
String key = "Product:1001";
cache.getMessagingService().addCacheNotificationListener(key, new CacheDataModificationListenerImpl(), EnumSet.allOf(EventType.class), EventDataFilter.DataWithMetadata);
}
private static class CacheDataModificationListenerImpl implements CacheDataModificationListener {
@Override
public void onCacheDataModified(String key, CacheEventArg eventArgs) {
switch (eventArgs.getEventType())
{
case ItemAdded:
// 'key' has been added to the cache
break;
case ItemUpdated:
// 'key' has been updated in the cache
break;
case ItemRemoved:
// 'key' has been removed from the cache
break;
}
}
@Override
public void onCacheCleared(String cacheName) {
//cacheName cleared
}
}
캐시 수준 이벤트의 경우 항목 수준 이벤트에 사용된 것과 동일한 콜백을 사용할 수 있습니다.
public static void RegisterCacheLevelEvents()
{
// Get cache
string cacheName = "demoCache";
var cache = CacheManager.GetCache(cacheName);
// Register cache level events
cache.MessagingService.RegisterCacheNotification(EventsSample.CacheDataNotificationCallbackImpl, EventType.ItemUpdated | EventType.ItemUpdated | EventType.ItemRemoved, EventDataFilter.DataWithMetadata);
}
private static void RegisterCacheLevelEvents() throws Exception {
String cacheName = "demoCache";
// Initialize an instance of the cache to begin performing operations:
Cache cache = CacheManager.getCache(cacheName);
CacheEventDescriptor eventDescriptor = cache.getMessagingService().addCacheNotificationListener(new CacheDataModificationListenerImpl(), EnumSet.allOf(EventType.class), EventDataFilter.DataWithMetadata);
}
럭셔리 .NET 및 자바 응용 프로그램 NCache 높은 트랜잭션 Pub/Sub 메시징, 연속 쿼리 및 이벤트를 수행하는 데 이상적인 선택입니다. 여기 이유가 있습니다.
이를 통해 다음을 사용할 수 있습니다. NCache 강력하면서도 단순한 메시징 플랫폼으로 에 대해 자세히 알아보기 NCache 아래 링크에서 완전히 작동하는 30일 평가판을 다운로드하십시오.