現在、多くのサーバー アプリケーションは、他のアプリケーションと連携して動作し、メッセージやデータを非同期で共有する必要があります。 そして、多くの場合、これは高トランザクション環境で行われます。 高トランザクション環境の場合、 NCache これらの理想的な候補です 非同期イベント駆動型通信.
NCache これらのアプリケーションのニーズを満たすための豊富なメッセージング機能とストリーム機能を提供します。 Pub / Subメッセージング, 継続的なクエリ, アイテムレベルのイベント, キャッシュレベルのイベント。 それぞれについて以下に説明します。
NCache 強力な Pub / Subメッセージング パブリッシャー アプリケーションがサブスクライバーが誰であるかを知らなくてもメッセージを送信できるアプリケーション用のプラットフォームです。 メッセージは次のように分類できます。 トピック その後、サブスクライバーは特定のトピックをサブスクライブし、これらのメッセージを非同期的に受信できます。
あなたが簡単にできる つかいます 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とJava アプリケーション、 NCache トランザクションの多いPub/Subメッセージング、連続クエリ、およびイベントを実行するための理想的な選択肢です。 これが理由です。
これで、あなたは使うことができます NCache 強力でありながらシンプルなメッセージングプラットフォームとして。 についてもっと読む NCache 以下のリンクから、完全に機能する 30 日間の試用版をダウンロードしてください。