static void Main(string[] args) { Console.WriteLine("Setting up MessageBus"); var messageBus = new MessageBusBuilder().UseLogger(new DelegateLogger(Console.WriteLine)).Build(); var t1 = messageBus.InitializeRabbitMq("host=vboxguest.asinetwork.local;username=guest;password=guest"); // Messages from local, with topic "send" are sent to rabbit var t2 = messageBus.Subscribe <MyTestPayload>(b => b .WithTopic("send") .ForwardToRabbitMq(r => r.UseDefaultQueue()) .OnWorker() ); messageBus.Subscribe <MyTestPayload>(b => b .WithTopic("send") .Invoke(s => Console.WriteLine("Sending: " + s.Text))); // Subscript to "send" topic on rabbit, and forward those messages locally on the "received" topic var t3 = messageBus.PullRabbitMqToLocal <MyTestPayload>(r => r .ForAllRemoteTopics() .ReceiveDefaultFormat() .ForwardToLocalTopic("received") .UseSharedQueueName() .AutoExpireQueue()); // Messages from rabbit, with topic "received" get printed on the console var t4 = messageBus.Subscribe <MyTestPayload>(b => b .WithTopic("received") .InvokeEnvelope(m => { Console.WriteLine("Received: " + m.Payload.Text); })); //Console.WriteLine("Sending message (should print 'whatever' if successful)"); for (int i = 0; i < 100; i++) { messageBus.Publish("send", new MyTestPayload { Text = "test message " + i }); Thread.Sleep(1000); } Console.WriteLine("Press any key to continue"); Console.ReadKey(); messageBus.Dispose(); Environment.Exit(0); }
static void Main(string[] args) { // ToDo: Provider your event hub names var eventHubNameForAddCommand = "add-command"; var eventHubNameForMultiplyRequest = "multiply-request"; var eventHubNameForResponses = "responses"; // ToDo: Provide consumer group name var consumerGroup = "consoleapp"; // ToDo: Provide connection string to your event hub namespace var eventHubConnectionString = ""; // ToDo: Provide connection string to your storage account var storageConnectionString = ""; /* * Azure setup notes: * 1. Remember to create 3 event hubs in Azure: * add-command * multiply-request * responses * * 2. Remember to create 'consoleapp' group consumer in each event hub. */ // Create message bus IMessageBus messageBus = new MessageBusBuilder() // Pub / Sub .Publish <AddCommand>(x => x.DefaultTopic(eventHubNameForAddCommand)) // By default messages of type 'AddCommand' will go to event hub named 'topica' (or topic if Kafka is chosen) .SubscribeTo <AddCommand>(x => x.Topic(eventHubNameForAddCommand).Group(consumerGroup).WithSubscriber <AddCommandConsumer>()) // Req / Resp .Publish <MultiplyRequest>(x => x.DefaultTopic(eventHubNameForMultiplyRequest)) // By default messages of type 'AddCommand' will go to event hub named 'topica' (or topic if Kafka is chosen) .Handle <MultiplyRequest, MultiplyResponse>(x => x.Topic(eventHubNameForMultiplyRequest).Group(consumerGroup).WithHandler <MultiplyRequestHandler>()) .ExpectRequestResponses(x => { x.Group("consoleapp"); x.ReplyToTopic(eventHubNameForResponses); // All responses from req/resp will return on this topic (event hub) }) .WithSerializer(new JsonMessageSerializer()) // Use JSON for message serialization .WithDependencyResolver(new LookupDependencyResolver(type => { if (type == typeof(AddCommandConsumer)) { return(new AddCommandConsumer()); } if (type == typeof(MultiplyRequestHandler)) { return(new MultiplyRequestHandler()); } throw new InvalidOperationException(); })) .WithProviderEventHub(new EventHubMessageBusSettings(eventHubConnectionString, storageConnectionString)) // Use Azure Event Hub as provider //.WithProviderKafka(new KafkaMessageBusSettings("<kafka-broker-list-here>")) // Or use Apache Kafka as provider .Build(); try { var addTask = Task.Factory.StartNew(() => AddLoop(messageBus), TaskCreationOptions.LongRunning); var multiplyTask = Task.Factory.StartNew(() => MultiplyLoop(messageBus), TaskCreationOptions.LongRunning); Console.WriteLine("Press any key to stop..."); Console.ReadKey(); _stop = true; Task.WaitAll(addTask, multiplyTask); } finally { messageBus.Dispose(); } }
static void Main(string[] args) { // ToDo: Provider your event hub names var topicForAddCommand = "add-command"; var topicForMultiplyRequest = "multiply-request"; // Note: Each running instance (node) of ConsoleApp should have its own unique response queue (i.e. responses-1) var topicForResponses = "responses"; // ToDo: Provide consumer group name var consumerGroup = "consoleapp"; var responseGroup = "consoleapp-1"; // ToDo: Provide connection string to your event hub namespace var eventHubConnectionString = ""; // ToDo: Provide connection string to your storage account var storageConnectionString = ""; // ToDo: Ensure your Kafka broker is running var kafkaBrokers = "localhost:9092"; /* * Azure setup notes: * Create 3 event hubs in Azure: * 1. 'add-command' with 'consoleapp' group consumer * 2. 'multiply-request' with 'consoleapp' group consumer * 3. 'responses' with 'consoleapp-1' group consumer */ // Create message bus using the fluent builder interface IMessageBus messageBus = new MessageBusBuilder() // Pub/Sub example .Publish <AddCommand>(x => x.DefaultTopic(topicForAddCommand)) // By default AddCommand messages will go to event hub named 'add-command' (or topic if Kafka is chosen) .SubscribeTo <AddCommand>(x => x.Topic(topicForAddCommand) .Group(consumerGroup) .WithSubscriber <AddCommandConsumer>()) // Req/Resp example .Publish <MultiplyRequest>(x => { // By default AddCommand messages will go to event hub named 'multiply-request' (or topic if Kafka is chosen) x.DefaultTopic(topicForMultiplyRequest); // Message key could be set for the message x.KeyProvider((request, topic) => Encoding.ASCII.GetBytes((request.Left + request.Right).ToString())); }) .Handle <MultiplyRequest, MultiplyResponse>(x => x.Topic(topicForMultiplyRequest) // topic to expect the requests .Group(consumerGroup) .WithHandler <MultiplyRequestHandler>()) // Configure response message queue (on topic) when using req/resp .ExpectRequestResponses(x => { x.Group(responseGroup); x.ReplyToTopic(topicForResponses); // All responses from req/resp will return on this topic (the EventHub name) x.DefaultTimeout(TimeSpan.FromSeconds(20)); // Timeout request sender if response won't arrive within 10 seconds. }) .WithSerializer(new JsonMessageSerializer()) // Use JSON for message serialization .WithDependencyResolver(new LookupDependencyResolver(type => { // Simulate a dependency container if (type == typeof(AddCommandConsumer)) { return(new AddCommandConsumer()); } if (type == typeof(MultiplyRequestHandler)) { return(new MultiplyRequestHandler()); } throw new InvalidOperationException(); })) //.WithProviderEventHub(new EventHubMessageBusSettings(eventHubConnectionString, storageConnectionString)) // Use Azure Event Hub as provider .WithProviderKafka(new KafkaMessageBusSettings(kafkaBrokers)) // Or use Apache Kafka as provider .Build(); try { var addTask = Task.Factory.StartNew(() => AddLoop(messageBus), TaskCreationOptions.LongRunning); var multiplyTask = Task.Factory.StartNew(() => MultiplyLoop(messageBus), TaskCreationOptions.LongRunning); Console.WriteLine("Press any key to stop..."); Console.ReadKey(); _stop = true; Task.WaitAll(addTask, multiplyTask); } finally { messageBus.Dispose(); } }