我們把視頻傳到阿里雲去轉碼,轉碼是否成功怎麼判斷呢,這裏就要用到阿里雲的消息服務了。
我的做法是新建一個服務程序,然後安裝到服務器上,這樣就能開機自啓,隨時隨地的接受消息, 另外我還給這個服務開啓一個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通信接口隨時來觀察此服務是否掛掉的相關代碼也不貼了,就幾行代碼。代碼寫的不好,獻醜了。