我们把视频传到阿里云去转码,转码是否成功怎么判断呢,这里就要用到阿里云的消息服务了。
我的做法是新建一个服务程序,然后安装到服务器上,这样就能开机自启,随时随地的接受消息, 另外我还给这个服务开启一个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通信接口随时来观察此服务是否挂掉的相关代码也不贴了,就几行代码。
代码写的不好,献丑了。