我不是程序員,但我試圖通過給他們一些指導來幫助他們。我們在msmq上不再有內部專業知識。我們正在嘗試使用它來將一些功能與調度應用程序集成。在2008R2上正確使用Message Queue
調度應用程序通過使用自定義內置dll進行網絡調用來關閉工作。該dll調用weburl。網絡應用程序將運行其任務並將更新發送到網站,以瞭解其執行的任務。網站將消息寫入隊列。調用該站點的dll正在監視隊列中是否有分配給該作業的標籤的消息。當它收到最終狀態消息時,它會關閉。
我們每隔幾個小時就會收到以下消息。我們每小時運行近100個工作,使用這種方法。在底部列出的代碼中,jobid對應於消息隊列中消息的標籤。每個作業在開始時都會發佈一個jobid,並將其用作發送給該作業的msmq的每條消息的標籤。
System.Messaging.MessageQueueException (0x80004005): Message that the cursor is currently pointing to has been removed from the queue by another process or by another call to Receive without the use of this cursor.
at System.Messaging.MessageQueue.ReceiveCurrent(TimeSpan timeout, Int32 action, CursorHandle cursor, MessagePropertyFilter filter, MessageQueueTransaction internalTransaction, MessageQueueTransactionType transactionType)
at System.Messaging.MessageEnumerator.get_Current()
這是它的代碼。
while (running)
{
// System.Console.WriteLine("Begin Peek");
messageQueue.Peek();
//System.Console.WriteLine("End Peek");
messageQueue.MessageReadPropertyFilter.SetAll();
using (MessageEnumerator enumerator = messageQueue.GetMessageEnumerator2())
{
enumerator.Reset();
while (enumerator.MoveNext())
{
Message msg = enumerator.Current;
if (msg.Label.Equals(this.jobid))
{
StringBuilder sb = new StringBuilder();
/*
try
{
sb.Append("Message Source: ");
//sb.Append(msg.SourceMachine);
sb.Append(" Sent: ");
sb.Append(msg.SentTime);
sb.Append(" Label ");
sb.Append(msg.Label);
sb.Append(" ID: ");
sb.Append(msg.Id);
sb.Append(" CorrelationID: ");
sb.Append(msg.CorrelationId);
sb.Append(" Body Type: ");
sb.Append(msg.BodyType);
}
catch (Exception)
{
throw;
}
finally
{
System.Console.WriteLine(sb.ToString());
}
*/
//System.Console.WriteLine("Receiving Message started");
using (Message message = messageQueue.ReceiveById(msg.Id))
{
//System.Console.WriteLine("Receiving Message Complete");
//sb = new StringBuilder();
string bodyText = string.Empty;
try
{
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
System.IO.StreamReader sr = new System.IO.StreamReader(message.BodyStream);
while (!sr.EndOfStream)
{
sw.WriteLine(sr.ReadLine());
}
sr.Close();
sw.Close();
bodyText = (string) FromXml(sb.ToString(), typeof(string));
int indx = bodyText.IndexOf(',');
string tokens = bodyText.Substring(indx + 1);
indx = tokens.IndexOf(',');
string command = tokens.Substring(0, indx);
tokens = tokens.Substring(indx + 1);
if (command.Equals(COMMAND_STARTED))
{
System.Console.WriteLine("STARTED " + tokens);
}
else if (command.Equals(COMMAND_UPDATE))
{
System.Console.WriteLine(tokens);
}
else if (command.Equals(COMMAND_ENDED_OK))
{
System.Console.WriteLine(tokens);
System.Console.WriteLine("WEBJOB: Success");
finalResults = new FinalResults(0, 0, "Success");
running = false;
}
else if (command.Equals(COMMAND_ENDED_WARNING))
{
System.Console.WriteLine(tokens);
System.Console.WriteLine("WEBJOB: Warning Issued");
finalResults = new FinalResults(1, 1, "Warning");
running = false;
}
else if (command.Equals(COMMAND_ENDED_FAIL))
{
System.Console.WriteLine(tokens);
System.Console.WriteLine("WEBJOB: Failure");
finalResults = new FinalResults(2, 16, "Failure");
running = false;
}
}
catch (Exception)
{
throw;
}
finally
{
//System.Console.WriteLine("Body: " + bodyText);
}
}
}
}
}
}
return finalResults;
}
MessageQueue messageQueue = null;
string webServiceURL = "";
Dictionary<string, string> parms = new Dictionary<string, string>();
string jobid = "NONE";
順便說一句。如果您使用的是2008 R2,則可以通過使用子隊列而無需太多修改即可使其工作。 http://technet.microsoft.com/en-us/library/cc730897(WS.10).aspx。就你而言,每個工作都是一個子隊列。 – Naraen 2011-03-23 22:49:37
子隊列看起來很有希望。目前通過一些例子來看看它是如何工作的以及它是如何工作的。 – ddjammin 2011-03-24 02:11:41
忘記標記爲答案。我們確實實現了subqueue,並且已經證明是可靠的,謝謝你的幫助。 – ddjammin 2011-10-11 04:07:13