Send ordered messages
Sample Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using ons;
namespace test
{
public class OnscSharp
{
private static OrderProducer _orderproducer;
private static string Ons_Topic = "xxxxxxxxxxx";
private static string Ons_ProducerID = "xxxxxxxxxxx";
private static string Ons_AccessKey = "xxxxxxxxxxxx";
private static string Ons_SecretKey = "xxxxxxxxxxxxxxx";
private static string Ons_ConsumerId = "xxxxxxxxxxxx";
public static void SendOrderMessage(string msgBody, String tag = "RegisterLog", String key = "test")
{
Message msg = new Message(Ons_Topic, tag, msgBody);
msg.setKey(Guid.NewGuid().ToString());
try
{
SendResultONS sendResult = _orderproducer.send(msg, shardingKey);
Console.WriteLine("send success {0}", sendResult.getMessageId());
}
catch (Exception ex)
{
Console.WriteLine("send failure{0}", ex.ToString());
}
}
public static void StartOrderProducer()
{
_orderproducer.start();
}
public static void ShutdownOrderProducer()
{
_orderproducer.shutdown();
}
private static ONSFactoryProperty getFactoryProperty()
{
ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, Ons_AccessKey);
factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, Ons_SecretKey);
factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, Ons_ConsumerId);
factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, Ons_ProducerID);
factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, Ons_Topic);
return factoryInfo;
}
public static void CreateOrderProducer()
{
_orderproducer = ONSFactory.getInstance().createOrderProducer(getFactoryProperty());
}
}
class OrderProducerForEx
{
static void Main(string[] args)
{
OnscSharp.CreateOrderProducer();
OnscSharp.StartOrderProducer();
System.DateTime beforDt = System.DateTime.Now;
for (int i = 0;i < 1000; ++i)
{
OnscSharp.SendOrderMessage("Test", "Tag", "key");
}
System.DateTime endDt = System.DateTime.Now;
System.TimeSpan ts = endDt.Subtract(beforDt);
Console.WriteLine("per message:{0}ms.", ts.TotalMilliseconds / 10000);
OnscSharp.ShutdownOrderProducer();
}
}
}
Receive ordered messages
Sample code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using ons;
namespace test
{
public class MyMsgOrderListener : MessageOrderListener
{
public MyMsgOrderListener()
{
}
~MyMsgOrderListener()
{
}
public override ons.OrderAction consume(Message value, ConsumeOrderContext context)
{
Byte[] text = Encoding.Default.GetBytes(value.getBody());
Console.WriteLine(Encoding.UTF8.GetString(text));
return ons.OrderAction.Success;
}
}
public class OnscSharp
{
private static OrderConsumer _orderconsumer;
private static MyMsgOrderListener _order_listen;
private static string Ons_Topic = "xxxxxxxxxxx";
private static string Ons_ProducerID = "xxxxxxxxxxx";
private static string Ons_AccessKey = "xxxxxxxxxxxx";
private static string Ons_SecretKey = "xxxxxxxxxxxxxxx";
private static string Ons_ConsumerId = "xxxxxxxxxxxx";
public static void StartOrderConsumer()
{
_order_listen = new MyMsgOrderListener();
_orderconsumer.subscribe(Ons_Topic, "*", _order_listen);
_orderconsumer.start();
}
public static void shutdownOrderConsumer()
{
_orderconsumer.shutdown();
}
private static ONSFactoryProperty getFactoryProperty()
{
ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, Ons_AccessKey);
factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, Ons_SecretKey);
factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, Ons_ConsumerId);
factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, Ons_ProducerID);
factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, Ons_Topic);
factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "E://log");
return factoryInfo;
}
public static void CreateOrderConsumer()
{
_orderconsumer = ONSFactory.getInstance().createOrderConsumer(getFactoryProperty());
}
}
class OrderConsumerForEx
{
static void Main(string[] args)
{
OnscSharp.CreateOrderConsumer();
OnscSharp.StartOrderConsumer();
Thread.Sleep(1000 * 1000);
OnscSharp.shutdownOrderConsumer();
}
}
}