Subscribing to messages

Our subscription to messages follows the same paradigm we've used in other microservices. We will:

  1. Create our Bus
  2. Create exchange and queue
  3. Bind them together

The interesting thing about this microservice, which you will also see in the microservice manager, is that we are now subscribing to multiple messages instead of just one. The unique code is highlighted as follows:

public void Subscribe()
{
1.Bus = RabbitHutch.CreateBus("host=localhost",
x => x.Register<IConventions, AttributeBasedConventions>());
2. IExchange exchange = Bus.Advanced.ExchangeDeclare("EvolvedAI", ExchangeType.Topic);
IQueue queue = Bus.Advanced.QueueDeclare("Financial");
3. Bus.Advanced.Bind(exchange, queue, "");

// functions to process subscription messages
Bus.Subscribe<CreditDefaultSwapRequestMessage>("CDSRequest", msg =>{ ProcessCDSMessage(msg); },
config => config.WithTopic("CDSRequest"));
Bus.Subscribe<CreditDefaultSwapResponseMessage>("CDSResponse", msgR => { ProcessCDSResponse(msgR); },
config => config.WithTopic("CDSResponse"));
Bus.Subscribe<BondsRequestMessage>("BondRequest", msg => { ProcessBondsMessage(msg); },
config => config.WithTopic("BondRequest"));
Bus.Subscribe<BondsResponseMessage>("BondResponse", msgR => { ProcessBondsResponse(msgR); },
config => config.WithTopic("BondResponse"));}
  1. Create our connection to the RabbitMQ service
  2. Create our exchange
  3. Create our queue and bind it to the exchange

The code highlighted is our request to process each message. We have four functions set up and ready to process messages: ProcessCDSResponse/request and ProcessBondsResponse/request.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset