Created
August 13, 2020 11:03
-
-
Save ptupitsyn/42e6e6c3b20147d04f0f1bda9e1e1464 to your computer and use it in GitHub Desktop.
Ignite RemoteListen workaround with Messaging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp3.1</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Apache.Ignite" Version="2.8.1" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using Apache.Ignite.Core; | |
using Apache.Ignite.Core.Cache.Expiry; | |
using Apache.Ignite.Core.Events; | |
using Apache.Ignite.Core.Messaging; | |
namespace IgniteExpiredEventTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var cfg = new IgniteConfiguration | |
{ | |
IncludedEventTypes = new[] {EventType.CacheObjectExpired}, | |
AutoGenerateIgniteInstanceName = true | |
}; | |
var clientCfg = new IgniteConfiguration(cfg) | |
{ | |
ClientMode = true | |
}; | |
using var server = Ignition.Start(cfg); | |
using var client = Ignition.Start(clientCfg); | |
server.GetEvents().LocalListen(new EventListener(server.GetMessaging()), EventType.CacheObjectExpired); | |
var cache = client.GetOrCreateCache<int, int>("c") | |
.WithExpiryPolicy(new ExpiryPolicy(TimeSpan.FromSeconds(1), null, null)); | |
Console.WriteLine(); | |
Console.WriteLine(">>>>>> STARTED"); | |
Console.WriteLine(); | |
client.GetMessaging().LocalListen(new MessageListener(), "Expired"); | |
cache[1] = 1; | |
Thread.Sleep(-1); | |
} | |
public class MessageListener : IMessageListener<object> | |
{ | |
public bool Invoke(Guid nodeId, object message) | |
{ | |
Console.WriteLine("Message received"); | |
Console.WriteLine("Expired: " + message); | |
return true; | |
} | |
} | |
public class EventListener : IEventListener<CacheEvent> | |
{ | |
private readonly IMessaging _messaging; | |
public EventListener(IMessaging messaging) | |
{ | |
_messaging = messaging; | |
} | |
public bool Invoke(CacheEvent evt) | |
{ | |
Console.WriteLine("Sending message..."); | |
_messaging.Send(evt.Key, "Expired"); | |
return true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment