static void Main(string[] args) { try { // 循环发送4条消息 for (int i = 0; i < 4; i++) { TopicMessage sendMsg; if (i % 2 == 0) { sendMsg = new TopicMessage("dfadfadfadf"); // 设置属性 sendMsg.PutProperty("a", i.ToString()); // 设置KEY sendMsg.MessageKey = "MessageKey"; } else { sendMsg = new TopicMessage("dfadfadfadf", "tag"); // 设置属性 sendMsg.PutProperty("a", i.ToString()); // 定时消息, 定时时间为10s后 sendMsg.StartDeliverTime = AliyunSDKUtils.GetNowTimeStamp() + 10 * 1000; } TopicMessage result = producer.PublishMessage(sendMsg); Console.WriteLine("publis message success:" + result); } } catch (Exception ex) { Console.Write(ex); } }
static void Main(string[] args) { try { // 循环发送8条消息 for (int i = 0; i < 8; i++) { TopicMessage sendMsg = new TopicMessage("dfadfadfadf", "tag"); sendMsg.PutProperty("a", i.ToString()); sendMsg.ShardingKey = (i % 2).ToString(); TopicMessage result = producer.PublishMessage(sendMsg); Console.WriteLine("publis message success:" + result); } } catch (Exception ex) { Console.Write(ex); } }
static void Main(string[] args) { // 客户端需要有一个线程或者进程来消费没有确认的事务消息 // 示例这里启动一个线程来检查没有确认的事务消息 Thread consumeHalfThread = new Thread(ConsumeHalfMessage); consumeHalfThread.Start(); try { // 循环发送4条事务消息, 第一条直接在发送完提交事务, 其它三条根据条件处理 for (int i = 0; i < 4; i++) { TopicMessage sendMsg = new TopicMessage("trans_msg"); sendMsg.MessageTag = "a"; sendMsg.MessageKey = "MessageKey"; sendMsg.PutProperty("a", i.ToString()); // 设置事务第一次回查的时间表征该条消息为事务消息,为相对时间,单位:秒,范围为10~300s之间 // 第一次事务回查后如果消息没有commit或者rollback,则之后每隔10s左右会回查一次,总共回查一天 sendMsg.TransCheckImmunityTime = 10; TopicMessage result = transProducer.PublishMessage(sendMsg); Console.WriteLine("publis message success:" + result); try { if (!string.IsNullOrEmpty(result.ReceiptHandle) && i == 0) { // 发送完事务消息后能获取到半消息句柄,可以直接commit/rollback事务消息 transProducer.Commit(result.ReceiptHandle); Console.WriteLine("Id:" + result.Id + ", commit"); } } catch (Exception ackError) { ProcessAckError(ackError); } } } catch (Exception ex) { Console.Write(ex); } consumeHalfThread.Join(); }