MassTransit: Creating a callback queue in consumer - c#

New to MassTransit and still playing around with some of the tutorial projects. I'm going to have a service that will run for maybe 20 minutes and I need to do something when it's done. Because it can take so long I don't want to follow the request/response pattern and await the response, holding up the thread. I think my other option is the create another queue just for the consumer to publish to when the job is done. I've looked at this post: MassTransit3 how to make request from consumer, but I'm not sure how to implement this. My projects, again from this tutorial, looks like this:
Publisher:
static void Main(string[] args)
{
var bus = Bus.Factory.CreateUsingRabbitMq(x =>
x.Host(new Uri("rabbitmq://localhost/"), h => {}));
var busHandle = bus.Start();
var text = ""'
Console.WriteLine("Publisher");
while(text != "quit")
{
Console.Write("Enter a message: ");
text = Console.ReadLine();
var message = new SomethingHappenedMessage()
{
What = text,
When = DateTime.Now
}
bus.Publish(message);
}
busHandle.Stop();
}
Subscriber:
static void Main(string[] args)
{
var bus = Bus.Factory.CreateUsingRabbitMq(x =>
{
var host = x.Host(new Uri("rabbitmq://localhost"), h => {});
x.ReceiveEndpoint(host, "MtPubSubExample_TestSubscriber", e =>
e.Consumer<SomethingHappenedConsumer>());
});
Console.WriteLine("Subscriber");
var busHandle = bus.Start();
Console.ReadKey();
busHandle.Stop();
}
Consumer:
class SomethingHappenedConsumer : IConsumer<ISomethingHappened>
{
public Task Consume(ConsumeContext<ISomethingHappened> context)
{
Console.Write("TXT: " + context.Message.What);
Console.Write(" SENT: " + context.Message.When);
Console.Write(" PROCESSED: " + DateTime.Now);
Console.WriteLine(" (" + System.Threading.Thread.CurrentThread.ManagedThreadId + ")");
return Task.FromResult(0);
}
}
How would I go about creating a callback queue in the consumer?

In your consumer, just Bus.Publish(new ResponseMessage()); (or whatever you call your response) and have your publisher register a consumer for that message type. You publisher doesn't appear to have been bound to a queue, just so make up a queue name and bind it to a queue as well.

Thanks again to #Travis for the help. Just wanted to show the final code I ended up with for anyone in the future. The messaging looks funny for the response but it is correctly posting back to the publisher.
Publisher:
static void Main(string[] args)
{
var bus = Bus.Factory.CreateUsingRabbitMq(x =>
{
var host = x.Host(new Uri("rabbitmq://localhost/"), h => { });
x.ReceiveEndpoint(host, "MtPubSubExample_TestPublisher", e =>
e.Consumer<ResponseConsumer>());
});
var busHandle = bus.Start();
var text = "";
Console.WriteLine("Publisher");
while(text != "quit")
{
Console.Write("Enter a message: ");
text = Console.ReadLine();
var message = new SomethingHappenedMessage()
{
What = text,
When = DateTime.Now
};
bus.Publish(message);
}
busHandle.Stop();
}
Response Consumer:
class ResponseConsumer : IConsumer<IResponse>
{
public Task Consume(ConsumeContext<IResponse> context)
{
Console.WriteLine("RESPONSE MESSAGE: " + context.Message.Message);
return Task.FromResult(0);
}
}
Subscriber:
static void Main(string[] args)
{
var bus = Bus.Factory.CreateUsingRabbitMq(x =>
{
var host = x.Host(new Uri("rabbitmq://localhost/"), h => { });
x.ReceiveEndpoint(host, "MtPubSubExample_TestSubscriber", e =>
e.Consumer<SomethingHappenedConsumer>());
});
Console.WriteLine("Subscriber");
var busHandle = bus.Start();
Console.ReadKey();
busHandle.Stop();
}
Subscriber Consumer:
class SomethingHappenedConsumer : IConsumer<ISomethingHappened>
{
private IBusControl bus = Bus.Factory.CreateUsingRabbitMq(x =>
x.Host(new Uri("rabbitmq://localhost/"), h => { }));
public Task Consume(ConsumeContext<ISomethingHappened> context)
{
var now = DateTime.Now;
Console.Write("TXT: " + context.Message.What);
Console.Write(" SENT: " + context.Message.When);
Console.Write(" PROCESSED: " + now);
Console.WriteLine(" (" + System.Threading.Thread.CurrentThread.ManagedThreadId + ")");
var response = new ResponseMessage()
{
Message = "The request was processed at " + now
};
bus.Publish(response);
return Task.FromResult(0);
}
}

Related

Get message twice with MQTT with C# uPLibrary.Networking.M2Mqtt

My problem is, it all works when transmitter and Subscribe are connected. All messages are transmitted correctly.
But if I disconnect the connection, so the Subscribe is no longer connected and then reconnect.
I get all the messages in between but the last one
Double.
The double received is wrong. Why is it like that ?
And how can I solve this?
Publisher
public class Programm
{
static MqttClient mqttClient;
static async Task Main(string[] args)
{
var locahlost = true;
var clientName = "Sender 1";
Console.WriteLine($"{clientName} Startet");
var servr = locahlost ? "localhost" : "test.mosquitto.org";
mqttClient = new MqttClient(servr);
mqttClient.Connect(clientName, null, null, false, 60);
Task.Run(() =>
{
if (mqttClient != null && mqttClient.IsConnected)
{
for (int i = 0; i < 100; i++)
{
var Message = $"{clientName} ->Test {i}";
mqttClient.Publish("Application1/NEW_Message", Encoding.UTF8.GetBytes($"{Message}"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
Console.WriteLine(Message);
Thread.Sleep(i * 1000);
}
}
});
Console.WriteLine($"{clientName} End");
}
}
subscriber
public class Programm
{
static MqttClient mqttClient;
static async Task Main(string[] args)
{
var clientName = "subscriber 1";
var locahlost = true;
Console.WriteLine($"Start of {clientName}");
Task.Run(() =>
{
var servr = locahlost ? "localhost" : "test.mosquitto.org";
mqttClient = new MqttClient(servr);
mqttClient.MqttMsgPublishReceived += MqttClient_MqttMsgPublishReceived;
mqttClient.Subscribe(new string[] { "Application1/NEW_Message" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
mqttClient.Connect(clientName, null, null, false, 60);
//mqttClient.Connect(clientName);
});
// client.UseConnecedHandler(e=> {Console.WriteLine("Verbunden") });
Console.ReadLine();
Console.WriteLine($"end of {clientName}");
Console.ReadLine();
}
private static void MqttClient_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
{
var message = Encoding.UTF8.GetString(e.Message);
Console.WriteLine(message);
}
}
server
static async Task Main(string[] args)
{
Console.WriteLine("Server");
MqttServerOptionsBuilder options = new MqttServerOptionsBuilder()
// set endpoint to localhost
.WithDefaultEndpoint()
// port used will be 707
.WithDefaultEndpointPort(1883)
// handler for new messages
.WithConnectionValidator(OnNewConnection)
.WithApplicationMessageInterceptor(OnNewMessage)
.WithClientMessageQueueInterceptor(OnOut)
.WithDefaultCommunicationTimeout(TimeSpan.FromMinutes(5))
.WithMaxPendingMessagesPerClient(10)
.WithPersistentSessions()
.WithStorage(storage)
;
// creates a new mqtt server
IMqttServer mqttServer = new MqttFactory().CreateMqttServer();
// start the server with options
mqttServer.StartAsync(options.Build()).GetAwaiter().GetResult();
// keep application running until user press a key
Console.ReadLine();
}
private static void OnOut(MqttClientMessageQueueInterceptorContext context)
{
var payload = context.ApplicationMessage?.Payload == null ? null : Encoding.UTF8.GetString(context.ApplicationMessage?.Payload);
Out_MessageCounter++;
var messageValue = Encoding.UTF8.GetString(context?.ApplicationMessage?.Payload);
Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("Messsage OUT :->");
Console.WriteLine($"MessageId: {Out_MessageCounter} - TimeStamp: {DateTime.Now} -- Message: ClientId = {context.ReceiverClientId}, Topic = {context.ApplicationMessage?.Topic}, Payload = {payload}, QoS = {context.ApplicationMessage?.QualityOfServiceLevel}, Retain-Flag = {context.ApplicationMessage?.Retain} Message {messageValue}");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
}
public static void OnNewConnection(MqttConnectionValidatorContext context)
{
Console.WriteLine(
$"New connection: ClientId = {context.ClientId}, Endpoint = {context.Endpoint}");
}
public static void OnNewMessage(MqttApplicationMessageInterceptorContext context)
{
var payload = context.ApplicationMessage?.Payload == null ? null : Encoding.UTF8.GetString(context.ApplicationMessage?.Payload);
In_MessageCounter++;
Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("Messsage IN :->");
Console.WriteLine($"MessageId: {In_MessageCounter} - TimeStamp: {DateTime.Now} -- Message: ClientId = {context.ClientId}, Topic = {context.ApplicationMessage?.Topic}, Payload = {payload}, QoS = {context.ApplicationMessage?.QualityOfServiceLevel}, Retain-Flag = {context.ApplicationMessage?.Retain} Message {Encoding.UTF8.GetString(context?.ApplicationMessage?.Payload)}");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
}
From the source:
/// Publish a message asynchronously
/// <param name="topic">Message topic</param>
/// <param name="message">Message data (payload)</param>
/// <param name="qosLevel">QoS Level</param>
/// <param name="retain">Retain flag</param>
/// <returns>Message Id related to PUBLISH message</returns>
public ushort Publish(string topic, byte[] message, byte qosLevel, bool retain)
You probably want to set the last value to false
mqttClient.Publish("Application1/NEW_Message", Encoding.UTF8.GetBytes($"{Message}"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
For more details about retained messages see here:
https://www.hivemq.com/blog/mqtt-essentials-part-8-retained-messages/

Using "Queue" in C# for sending SMS

I am trying to send SMS to clients from transactional data. It works fine but sometimes the staging table gets tanked due to huge data load. I was thinking if there is any way to queue data in a First-In-First-Out (FIFO) order. I am still learning C#. Please help me to implement the following code in a smart way.
I am sharing what I did.
private static void Main(string[] args)
{
var datetime = DateTime.Now;
var hr = datetime.Hour;
var mm = datetime.Minute + 1;
var ss = double.Parse(ConfigurationManager.AppSettings["SMSTIME"]);
// For Interval in Seconds
// This Scheduler will start at current time + 1 minute and call after every 3 Seconds
// IntervalInSeconds(start_hour, start_minute, seconds)
SchedulerTasks.IntervalInSeconds(hr, mm, ss,
() =>
{
CreateNewSms.Instance.PostNewSmsText();
});
Console.ReadLine();
}
public async void PostNewSmsText()
{
var smsDt = await Task.Run(() => DataAccessLayer.Instance.InsertSmsDataToLocalDb());
if (smsDt.Rows.Count != 0)
{
var smsData = GetSmsData(smsDt);
var xmlRespDoc = new XmlDocument();
await Task.Run(() =>
{
foreach (var sms in smsData)
{
var smsText = GetSmsText(sms.Channel, sms.AccountNumber, sms.Amount, sms.DrCr, sms.CurrentBalance, sms.BranchName,
sms.CurrencyName, sms.TrnDescription, sms.UbsTrnDateTime);
var smsSubmitTime = DateTime.Now.ToString(CultureInfo.InvariantCulture);
var response = ExecuteSmsService.Instance.PostSmsText(sms.MobileNo, smsText, sms.TrnRefNo);
var smsDeliveryTime = DateTime.Now.ToString(CultureInfo.InvariantCulture);
var operatorInfo = ConfigurationManager.AppSettings["Operator"];
xmlRespDoc.LoadXml(response);
var smsCsmsId = xmlRespDoc.SelectSingleNode("//SMSINFO/CSMSID")?.InnerText;
var smsRefNo = xmlRespDoc.SelectSingleNode("//SMSINFO/REFERENCEID")?.InnerText;
var smsDeliveryStatus = xmlRespDoc.SelectSingleNode("//SMSINFO/MSISDNSTATUS")?.InnerText;
if (smsRefNo != null) //Save response data if sms delivery successful
{
DataAccessLayer.Instance.InsertSmsResponseDetails(
sms.EntrySerialNo,
smsCsmsId,
smsRefNo,
smsText,
smsSubmitTime,
smsDeliveryTime,
true,
"Success",
operatorInfo);
DataAccessLayer.Instance.UpdateSmsStatus(sms.EntrySerialNo);
DataAccessLayer.Instance.DeleteSmsData(sms.EntrySerialNo, true);
Console.WriteLine("SMS successfully sent to: " + sms.MobileNo);
}
else
{
//If any sms delivery failed here we set a counter to retry again in next date
DataAccessLayer.Instance.SetSmsCounter(sms.EntrySerialNo);
//Save response data if sms delivery unsuccessful with failed status
DataAccessLayer.Instance.InsertSmsResponseDetails(
sms.EntrySerialNo,
smsCsmsId,
null,
smsText,
smsSubmitTime,
smsDeliveryTime,
false,
smsDeliveryStatus,
operatorInfo);
DataAccessLayer.Instance.DeleteSmsData(sms.EntrySerialNo, false);
Console.WriteLine("Invalid mobile no: " + sms.MobileNo);
}
}
});
}
else
{
Console.WriteLine("Waiting for transactions.....");
}
}
I was searching on Queue Implementations and found this but confused how to use it here in my code.
public class SendSmsQueue
{
private ConcurrentQueue<object> _jobs = new ConcurrentQueue<object>();
public SendSmsQueue()
{
var thread = new Thread(new ThreadStart(OnStart));
thread.IsBackground = true;
thread.Start();
}
public void Enqueue(object job)
{
_jobs.Enqueue(job);
}
private void OnStart()
{
while (true)
{
if (_jobs.TryDequeue(out object result))
{
Console.WriteLine(result);
}
}
}
}

M2MQTT client disconnecting without an exception or error message

I'm trying to create an API that consumes various topics.
For this, I'm trying to multi-thread things, so that the whole thing can be scalable into multiple APIs, later on, but that's very besides the point.
I'm using ASP.net Core 4.0, if that's got anything to do with it. Entity Framework as well.
My problem is based on my connection to my Mosquitto server being broken without throwing an exception or anything of the like, after a minute or so. It doesn't matter how big the messages are, or how many are exchanged. I have no idea of how I can create a callback or anything of the kind to know what's going on with my connection. Can anyone help?
I'll link the code I use to establish a connection and subscribe to a connection below. Using the Subscribe method or doing it manually also changes nothing. I'm at a loss, here.
Thanks in advance!
Main.cs:
Task.Factory.StartNew(() => DataflowController.ResumeQueuesAsync());
BuildWebHost(args).Run();
DataflowController.cs:
public static Boolean Subscribe(String topic)
{
Console.WriteLine("Hello from " + topic);
MqttClient mqttClient = new MqttClient(brokerAddress);
byte code = mqttClient.Connect(Guid.NewGuid().ToString());
// Register to message received
mqttClient.MqttMsgPublishReceived += client_recievedMessageAsync;
string clientId = Guid.NewGuid().ToString();
mqttClient.Connect(clientId);
// Subscribe to topic
mqttClient.Subscribe(new String[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
System.Console.ReadLine();
return true;
}
public static async Task ResumeQueuesAsync()
{
var mongoClient = new MongoClient(connectionString);
var db = mongoClient.GetDatabase(databaseName);
var topics = db.GetCollection<BsonDocument>(topicCollection);
var filter = new BsonDocument();
List<BsonDocument> result = topics.Find(filter).ToList();
var resultSize = result.Count;
Task[] subscriptions = new Task[resultSize];
MqttClient mqttClient = new MqttClient(brokerAddress);
byte code = mqttClient.Connect(Guid.NewGuid().ToString());
// Register to message received
mqttClient.MqttMsgPublishReceived += client_recievedMessageAsync;
string clientId = Guid.NewGuid().ToString();
mqttClient.Connect(clientId);
int counter = 0;
foreach(var doc in result)
{
subscriptions[counter] = new Task(() =>
{
Console.WriteLine("Hello from " + doc["topic"].ToString());
// Subscribe to topic
mqttClient.Subscribe(new String[] { doc["topic"].ToString() }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
System.Console.ReadLine();
});
counter++;
}
foreach(Task task in subscriptions)
{
task.Start();
}
}
static async void client_recievedMessageAsync(object sender, MqttMsgPublishEventArgs e)
{
// Handle message received
var message = System.Text.Encoding.Default.GetString(e.Message);
var topic = e.Topic;
var id = topic.Split("/")[2];
BsonDocument doc = new BsonDocument {
{"Plug ID", id },
{"Consumption", message }
};
await Save(doc, "smartPDM_consumption");
System.Console.WriteLine("Message received from " + topic + " : " + message);
}
This line was the issue:
byte code = mqttClient.Connect(Guid.NewGuid().ToString());
Deleted it, and it just worked.

HTTP 502 bad gateway c# with wamp on poloniex

I want to retrieve ticks from Poloniex in real time. They use wamp for that. I installed via nugget WampSharp and found this code :
static async void MainAsync(string[] args)
{
var channelFactory = new DefaultWampChannelFactory();
var channel = channelFactory.CreateMsgpackChannel("wss://api.poloniex.com", "realm1");
await channel.Open();
var realmProxy = channel.RealmProxy;
Console.WriteLine("Connection established");
int received = 0;
IDisposable subscription = null;
subscription =
realmProxy.Services.GetSubject("ticker")
.Subscribe(x =>
{
Console.WriteLine("Got Event: " + x);
received++;
if (received > 5)
{
Console.WriteLine("Closing ..");
subscription.Dispose();
}
});
Console.ReadLine();
}
but no matter at the await channel.open() I have the following error : HHTP 502 bad gateway
Do you have an idea where is the problem
thank you in advance
The Poloniex service seems not to be able to handle so many connections. That's why you get the HTTP 502 bad gateway error. You can try to use the reconnector mechanism in order to try connecting periodically.
static void Main(string[] args)
{
var channelFactory = new DefaultWampChannelFactory();
var channel = channelFactory.CreateJsonChannel("wss://api.poloniex.com", "realm1");
Func<Task> connect = async () =>
{
await Task.Delay(30000);
await channel.Open();
var tickerSubject = channel.RealmProxy.Services.GetSubject("ticker");
var subscription = tickerSubject.Subscribe(evt =>
{
var currencyPair = evt.Arguments[0].Deserialize<string>();
var last = evt.Arguments[1].Deserialize<decimal>();
Console.WriteLine($"Currencypair: {currencyPair}, Last: {last}");
},
ex => {
Console.WriteLine($"Oh no! {ex}");
});
};
WampChannelReconnector reconnector =
new WampChannelReconnector(channel, connect);
reconnector.Start();
Console.WriteLine("Press a key to exit");
Console.ReadKey();
}
This is based on this code sample.
Get rid of the Console.WriteLine it is interfering with your code.

WebSockets using Fleck library

I try to create simple chat room using websockets and Fleck library. Now I can send messages to all users. Here is my code:
FleckLog.Level = LogLevel.Info;
var allsockets = new List<IWebSocketConnection>();
var server = new WebSocketServer("ws://localhost:8181");
server.Start(socket =>
{
socket.OnOpen = () =>
{ //See socket.ConnectionInfo.* for additional informations
Console.WriteLine(String.Empty);
Console.WriteLine("[NEW CLIENT CONNECTION]======================");
Console.WriteLine("GUID: " + socket.ConnectionInfo.Id);
Console.WriteLine("IP: " + socket.ConnectionInfo.ClientIpAddress);
Console.WriteLine("Port: " + socket.ConnectionInfo.ClientPort);
Console.WriteLine("=============================================");
Console.WriteLine(String.Empty);
allsockets.Add(socket);
};
socket.OnClose = () =>
{
Console.WriteLine(String.Empty);
Console.WriteLine("[DISCONNECTED CLIENT]=======================");
Console.WriteLine("GUID: " + socket.ConnectionInfo.Id);
Console.WriteLine("IP: " + socket.ConnectionInfo.ClientIpAddress);
Console.WriteLine("Port: " + socket.ConnectionInfo.ClientPort);
Console.WriteLine("=============================================");
Console.WriteLine(String.Empty);
allsockets.Remove(socket);
};
socket.OnMessage = (message) =>
{
//TODO: Json.Net Deserialize
Console.WriteLine("[JSON MESSAGE] " + message);
allsockets.ToList().ForEach(s => s.Send(message));
};
});
Client code (java script):
// Websocket Endpoint url
var URL = 'ws://localhost:8181';
var chatClient = null;
function connect () {
chatClient = new WebSocket(URL);
chatClient.onmessage = function (event) {
var messagesArea = document.getElementById("messages");
var jsonObj = JSON.parse(event.data);
var message = "<"+ jsonObj.user + "> " + jsonObj.message + "\r\n";
messagesArea.value = messagesArea.value + message;
messagesArea.scrollTop = messagesArea.scrollHeight;
};
}
function disconnect () {
chatClient.close();
}
function sendMessage() {
var user = document.getElementById("userName").value.trim();
if (user === "")
alert ("Please enter your name!");
var inputElement = document.getElementById("messageInput");
var message = inputElement.value.trim();
if (message !== "") {
var jsonObj = {"user" : user, "message" : message};
chatClient.send(JSON.stringify(jsonObj));
inputElement.value = "";
}
inputElement.focus();
}
How to add to this code secure layer WSS?
Thank you very much!
Have you checked https://github.com/statianzo/Fleck ?
You need to have an certificate and update your client url with wss://

Categories