C#阿里云视频转码之消息接受
来源:互联网 发布:qq空间软件免费版 编辑:程序博客网 时间:2024/06/06 21:01
我们把视频传到阿里云去转码,转码是否成功怎么判断呢,这里就要用到阿里云的消息服务了。
我的做法是新建一个服务程序,然后安装到服务器上,这样就能开机自启,随时随地的接受消息, 另外我还给这个服务开启一个wcf通信接口,这样调一下这个,就能知道这个服务是否挂掉了。
首先还是要引用阿里云的Aliyun.MNS.dll,引用好了开整。
using Aliyun.MNS;using Aliyun.MNS.Model;using Aliyun.MNS.Sample.Model;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Diagnostics;using System.Linq;using System.ServiceModel;using System.ServiceProcess;using System.Text;using System.Threading;using System.Threading.Tasks;namespace NiuRenVodMessage{ public partial class Service1 : ServiceBase { #region Private Properties private static string NiuRen = System.Configuration.ConfigurationManager.AppSettings["niuren"]; //数据库连接字符串 private static string _accessKeyId = System.Configuration.ConfigurationManager.AppSettings["accessKeyId"]; //阿里云的accessKeyId private static string _secretAccessKey = System.Configuration.ConfigurationManager.AppSettings["secretAccessKey"]; //阿里云的secretAccessKey private static string _endpoint = System.Configuration.ConfigurationManager.AppSettings["endpoint"]; //阿里云消息服务的公网Endpoint private static string _folder = System.Configuration.ConfigurationManager.AppSettings["folder"]; //阿里云存放的路径 private static string _queueName = System.Configuration.ConfigurationManager.AppSettings["queueName"]; //消息队列的名称 private static HashSet<string> _secretHashSet = new HashSet<string>(); public static Thread myThread = new Thread(ReceiveMessage); //新开一个线程 private static bool isExit = false; //private const string _queueNamePrefix = "my"; private const int _receiveTimes = 1; //一条记录 // private const int _receiveInterval = 2; // private const int batchSize = 6; private static string _receiptHandle; static ServiceHost serviceHost = new ServiceHost(typeof(Service2)); #endregion public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { Log4Net.Write("windows服务开始启动"); isExit = true; serviceHost.Open(); IMNS client = new Aliyun.MNS.MNSClient(_accessKeyId, _secretAccessKey, _endpoint); myThread.Start(client); } protected override void OnStop() { Log4Net.Write("windows服务退出"); isExit = false; serviceHost.Close(); } public static void ReceiveMessage(object obj) { IMNS client = obj as IMNS; while (isExit) { try { var nativeQueue = client.GetNativeQueue(_queueName); for (int i = 0; i < _receiveTimes; i++) { var receiveMessageResponse = nativeQueue.ReceiveMessage(); Message message = receiveMessageResponse.Message; byte[] bpath = Convert.FromBase64String(message.Body); string str = Encoding.UTF8.GetString(bpath); Result result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result>(str); string pahtstr = "\"Object\": \"" + _folder + "/"; if (result.MediaWorkflowExecution.Input.ToString().IndexOf(pahtstr) != -1) { int starSize = result.MediaWorkflowExecution.Input.ToString().IndexOf(pahtstr) + pahtstr.Length; int endSize = result.MediaWorkflowExecution.Input.ToString().IndexOf("."); result.MediaWorkflowExecution.InputFile = result.MediaWorkflowExecution.Input.ToString().Substring(starSize, endSize - starSize); } result.MediaWorkflowExecution.EndTime = message.EnqueueTime; if (!_secretHashSet.Contains(result.MediaWorkflowExecution.InputFile)) { string sql = string.Format("update [Vod] set [RunId]='{0}', EndTime = '{1}' where InputFileName = '{2}'", result.MediaWorkflowExecution.RunId, result.MediaWorkflowExecution.EndTime, result.MediaWorkflowExecution.InputFile); SqlConnection connection = new SqlConnection(NiuRen); SqlCommand command = new SqlCommand(sql, connection); connection.Open(); int res = command.ExecuteNonQuery(); if (res > 0) { _secretHashSet.Add(result.MediaWorkflowExecution.InputFile); } connection.Close(); Log4Net.Write(string.Format("成功转码{0}条数据,文件名:{1}", res,result.MediaWorkflowExecution.InputFile)); } _receiptHandle = message.ReceiptHandle; //删除消息 var deleteMessageResponse = nativeQueue.DeleteMessage(_receiptHandle); Thread.Sleep(1000); } } catch (Exception ex) { Log4Net.Write("Receive message failed, exception info: " + ex.Message + ex.GetType().Name); Thread.Sleep(60000); } } } }}Log4Net.Write这个是我自己写的一个记录文字输出到txt的函数,这就不贴了,另外新开一个wcf通信接口随时来观察此服务是否挂掉的相关代码也不贴了,就几行代码。
代码写的不好,献丑了。
阅读全文
0 0
- C#阿里云视频转码之消息接受
- MQTT之阿里云接受推送消息
- c#对接阿里云视频转码服务之删除
- 阿里云mns 消息之短信搭建
- C# 中对话接受按键消息
- C# 接受MQTT服务器推送的消息
- Spring之JMS之接受消息
- Spring之JMS之接受消息
- Spring之JMS之接受消息
- 阿里云视频直播
- [整理]阿里云之消息队列的使用
- Android 之 阿里百川 -- 云消息推送服务
- c#按ESC退出 或者接受其他键盘消息
- c#按ESC退出 或者接受其他键盘消息
- 【短视频系列之二】阿里云高级技术专家带你揭秘短视频解决方案
- 阿里云SMS发送消息
- activeMQ之点对点(PTP),主动接受消息
- Apache kafka之旅——java 发送接受消息
- 跳跃表原理
- 调用FTPClient 的retrieveFileStream(String remote)方法后的处理
- LINUX中mysql命令
- (搬运).net起步 (用配置文件属性维护网站用户信息 2)
- 无监督学习:无监督降维
- C#阿里云视频转码之消息接受
- iOS 得到字典所有的键值对
- PHP 利用函数的静态变量 指定迭代次数
- Solr搜索引擎原理
- 苹果iOS11重磅改版App Store,开发者应该了解这些
- 基于嵌入式Linux/Qt 开发RFID智能仓储指纹管理系统
- 手机控制urdf小车模型运动(一)—安装indigo下的串口驱动
- 查找文件which,whereis,find的区别
- 各大邮箱每天发送数量限制