1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Workflow.ComponentModel;
using System.Runtime.Serialization;
using System.Messaging;
namespace System.Workflow.Runtime
{
[Obsolete("The System.Workflow.* types are deprecated. Instead, please use the new types from System.Activities.*")]
public class WorkflowQueuingService
{
Object syncRoot = new Object();
IWorkflowCoreRuntime rootWorkflowExecutor;
List<IComparable> dirtyQueues;
EventQueueState pendingQueueState = new EventQueueState();
Dictionary<IComparable, EventQueueState> persistedQueueStates;
// event handler used by atomic execution context's Q service for message delivery
List<WorkflowQueuingService> messageArrivalEventHandlers;
// set for inner queuing service
WorkflowQueuingService rootQueuingService;
// Runtime information visible to host, stored on the root activity
[SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes",
Justification = "Design has been approved. This is a false positive. DependencyProperty is an immutable type.")]
public readonly static DependencyProperty PendingMessagesProperty = DependencyProperty.RegisterAttached("PendingMessages", typeof(Queue), typeof(WorkflowQueuingService), new PropertyMetadata(DependencyPropertyOptions.NonSerialized));
// Persisted state properties
internal static DependencyProperty RootPersistedQueueStatesProperty = DependencyProperty.RegisterAttached("RootPersistedQueueStates", typeof(Dictionary<IComparable, EventQueueState>), typeof(WorkflowQueuingService));
internal static DependencyProperty LocalPersistedQueueStatesProperty = DependencyProperty.RegisterAttached("LocalPersistedQueueStates", typeof(Dictionary<IComparable, EventQueueState>), typeof(WorkflowQueuingService));
private const string pendingNotification = "*PendingNotifications";
// Snapshots created during pre-persist and dumped during post-persist
// If persistence fails, changes made to queuing service during pre-persist must be undone
// in post-persist.
// Created for ref. 20575.
private Dictionary<IComparable, EventQueueState> persistedQueueStatesSnapshot = null;
private EventQueueState pendingQueueStateSnapshot = null;
// root Q service constructor
internal WorkflowQueuingService(IWorkflowCoreRuntime rootWorkflowExecutor)
{
this.rootWorkflowExecutor = rootWorkflowExecutor;
this.rootWorkflowExecutor.RootActivity.SetValue(WorkflowQueuingService.PendingMessagesProperty, this.pendingQueueState.Messages);
this.persistedQueueStates = (Dictionary<IComparable, EventQueueState>)this.rootWorkflowExecutor.RootActivity.GetValue(WorkflowQueuingService.RootPersistedQueueStatesProperty);
if (this.persistedQueueStates == null)
{
this.persistedQueueStates = new Dictionary<IComparable, EventQueueState>();
this.rootWorkflowExecutor.RootActivity.SetValue(WorkflowQueuingService.RootPersistedQueueStatesProperty, this.persistedQueueStates);
}
if (!this.Exists(pendingNotification))
this.CreateWorkflowQueue(pendingNotification, false);
}
// inner Q service constructor
internal WorkflowQueuingService(WorkflowQueuingService copyFromQueuingService)
{
this.rootQueuingService = copyFromQueuingService;
this.rootWorkflowExecutor = copyFromQueuingService.rootWorkflowExecutor;
this.rootWorkflowExecutor.RootActivity.SetValue(WorkflowQueuingService.PendingMessagesProperty, this.pendingQueueState.Messages);
this.persistedQueueStates = new Dictionary<IComparable, EventQueueState>();
this.rootWorkflowExecutor.RootActivity.SetValue(WorkflowQueuingService.LocalPersistedQueueStatesProperty, this.persistedQueueStates);
SubscribeForRootMessageDelivery();
}
public WorkflowQueue CreateWorkflowQueue(IComparable queueName, bool transactional)
{
if (queueName == null)
throw new ArgumentNullException("queueName");
lock (SyncRoot)
{
// if not transactional create one at the root
// so it is visible outside this transaction
if (this.rootQueuingService != null && !transactional)
{
return this.rootQueuingService.CreateWorkflowQueue(queueName, false);
}
NewQueue(queueName, true, transactional);
return new WorkflowQueue(this, queueName);
}
}
public void DeleteWorkflowQueue(IComparable queueName)
{
if (queueName == null)
throw new ArgumentNullException("queueName");
lock (SyncRoot)
{
// when we are deleting the queue from activity
// message delivery should not happen.
if (this.rootQueuingService != null && !IsTransactionalQueue(queueName))
{
this.rootQueuingService.DeleteWorkflowQueue(queueName);
return;
}
EventQueueState queueState = GetEventQueueState(queueName);
Queue queue = queueState.Messages;
Queue pendingQueue = this.pendingQueueState.Messages;
while (queue.Count != 0)
{
pendingQueue.Enqueue(queue.Dequeue());
}
WorkflowTrace.Runtime.TraceInformation("Queuing Service: Deleting Queue with ID {0} for {1}", queueName.GetHashCode(), queueName);
this.persistedQueueStates.Remove(queueName);
}
}
public bool Exists(IComparable queueName)
{
if (queueName == null)
throw new ArgumentNullException("queueName");
lock (SyncRoot)
{
if (this.rootQueuingService != null && !IsTransactionalQueue(queueName))
{
return this.rootQueuingService.Exists(queueName);
}
return this.persistedQueueStates.ContainsKey(queueName);
}
}
public WorkflowQueue GetWorkflowQueue(IComparable queueName)
{
if (queueName == null)
throw new ArgumentNullException("queueName");
lock (SyncRoot)
{
if (this.rootQueuingService != null && !IsTransactionalQueue(queueName))
{
return this.rootQueuingService.GetWorkflowQueue(queueName);
}
GetEventQueueState(queueName);
return new WorkflowQueue(this, queueName);
}
}
#region internal functions
internal Object SyncRoot
{
get { return syncRoot; }
}
internal void EnqueueEvent(IComparable queueName, Object item)
{
if (queueName == null)
throw new ArgumentNullException("queueName");
lock (SyncRoot)
{
if (this.rootQueuingService != null && !IsTransactionalQueue(queueName))
{
this.rootQueuingService.EnqueueEvent(queueName, item);
return;
}
EventQueueState qState = GetQueue(queueName);
if (!qState.Enabled)
{
throw new QueueException(String.Format(CultureInfo.CurrentCulture, ExecutionStringManager.QueueNotEnabled, queueName), MessageQueueErrorCode.QueueNotAvailable);
}
// note enqueue allowed irrespective of dirty flag since it is delivered through
qState.Messages.Enqueue(item);
WorkflowTrace.Runtime.TraceInformation("Queuing Service: Enqueue item Queue ID {0} for {1}", queueName.GetHashCode(), queueName);
// notify message arrived subscribers
for (int i = 0; messageArrivalEventHandlers != null && i < messageArrivalEventHandlers.Count; ++i)
{
this.messageArrivalEventHandlers[i].OnItemEnqueued(queueName, item);
}
NotifyExternalSubscribers(queueName, qState, item);
}
}
internal bool SafeEnqueueEvent(IComparable queueName, Object item)
{
if (queueName == null)
throw new ArgumentNullException("queueName");
lock (SyncRoot)
{
if (this.rootQueuingService != null && !IsTransactionalQueue(queueName))
{
return this.rootQueuingService.SafeEnqueueEvent(queueName, item);
}
EventQueueState qState = GetQueue(queueName);
if (!qState.Enabled)
{
throw new QueueException(String.Format(CultureInfo.CurrentCulture, ExecutionStringManager.QueueNotEnabled, queueName), MessageQueueErrorCode.QueueNotAvailable);
}
// note enqueue allowed irrespective of dirty flag since it is delivered through
qState.Messages.Enqueue(item);
WorkflowTrace.Runtime.TraceInformation("Queuing Service: Enqueue item Queue ID {0} for {1}", queueName.GetHashCode(), queueName);
// notify message arrived subscribers
for (int i = 0; messageArrivalEventHandlers != null && i < messageArrivalEventHandlers.Count; ++i)
{
this.messageArrivalEventHandlers[i].OnItemSafeEnqueued(queueName, item);
}
NotifySynchronousSubscribers(queueName, qState, item);
return QueueAsynchronousEvent(queueName, qState);
}
}
internal object Peek(IComparable queueName)
{
if (queueName == null)
throw new ArgumentNullException("queueName");
lock (SyncRoot)
{
if (this.rootQueuingService != null && !IsTransactionalQueue(queueName))
{
return this.rootQueuingService.Peek(queueName);
}
EventQueueState queueState = GetEventQueueState(queueName);
if (queueState.Messages.Count != 0)
return queueState.Messages.Peek();
object[] args = new object[] { System.Messaging.MessageQueueErrorCode.MessageNotFound, queueName };
string message = string.Format(CultureInfo.CurrentCulture, ExecutionStringManager.EventQueueException, args);
throw new QueueException(message, MessageQueueErrorCode.MessageNotFound);
}
}
internal Object DequeueEvent(IComparable queueName)
{
if (queueName == null)
throw new ArgumentNullException("queueName");
lock (SyncRoot)
{
if (this.rootQueuingService != null && !IsTransactionalQueue(queueName))
{
return this.rootQueuingService.DequeueEvent(queueName);
}
EventQueueState queueState = GetEventQueueState(queueName);
if (queueState.Messages.Count != 0)
return queueState.Messages.Dequeue();
object[] args = new object[] { System.Messaging.MessageQueueErrorCode.MessageNotFound, queueName };
string message = string.Format(CultureInfo.CurrentCulture, ExecutionStringManager.EventQueueException, args);
throw new QueueException(message, MessageQueueErrorCode.MessageNotFound);
}
}
internal EventQueueState GetQueueState(IComparable eventType)
{
lock (SyncRoot)
{
return GetQueue(eventType);
}
}
Activity caller;
internal Activity CallingActivity
{
get
{
if (this.rootQueuingService != null)
return this.rootQueuingService.CallingActivity;
return this.caller;
}
set
{
if (this.rootQueuingService != null)
this.rootQueuingService.CallingActivity = value;
this.caller = value;
}
}
private bool QueueAsynchronousEvent(IComparable queueName, EventQueueState qState)
{
if (qState.AsynchronousListeners.Count != 0 || IsNestedListenersExist(queueName))
{
Queue q = GetQueue(pendingNotification).Messages;
q.Enqueue(new KeyValuePair<IComparable, EventQueueState>(queueName, qState));
WorkflowTrace.Runtime.TraceInformation("Queuing Service: Queued delayed message notification for '{0}'", queueName.ToString());
return q.Count == 1;
}
return false;
}
bool IsNestedListenersExist(IComparable queueName)
{
for (int i = 0; messageArrivalEventHandlers != null && i < messageArrivalEventHandlers.Count; ++i)
{
WorkflowQueuingService qService = messageArrivalEventHandlers[i];
EventQueueState queueState = null;
if (qService.persistedQueueStates.TryGetValue(queueName, out queueState) &&
queueState.AsynchronousListeners.Count != 0)
return true;
}
return false;
}
internal void ProcessesQueuedAsynchronousEvents()
{
Queue q = GetQueue(pendingNotification).Messages;
while (q.Count > 0)
{
KeyValuePair<IComparable, EventQueueState> pair = (KeyValuePair<IComparable, EventQueueState>)q.Dequeue();
// notify message arrived subscribers
WorkflowTrace.Runtime.TraceInformation("Queuing Service: Processing delayed message notification '{0}'", pair.Key.ToString());
for (int i = 0; messageArrivalEventHandlers != null && i < messageArrivalEventHandlers.Count; ++i)
{
WorkflowQueuingService service = this.messageArrivalEventHandlers[i];
if (service.persistedQueueStates.ContainsKey(pair.Key))
{
EventQueueState qState = service.GetQueue(pair.Key);
if (qState.Enabled)
{
service.NotifyAsynchronousSubscribers(pair.Key, qState, 1);
}
}
}
NotifyAsynchronousSubscribers(pair.Key, pair.Value, 1);
}
}
internal void NotifyAsynchronousSubscribers(IComparable queueName, EventQueueState qState, int numberOfNotification)
{
for (int i = 0; i < numberOfNotification; ++i)
{
QueueEventArgs args = new QueueEventArgs(queueName);
lock (SyncRoot)
{
foreach (ActivityExecutorDelegateInfo<QueueEventArgs> subscriber in qState.AsynchronousListeners)
{
Activity contextActivity = rootWorkflowExecutor.GetContextActivityForId(subscriber.ContextId);
Debug.Assert(contextActivity != null);
subscriber.InvokeDelegate(contextActivity, args, false);
WorkflowTrace.Runtime.TraceInformation("Queuing Service: Notifying async subscriber on queue:'{0}' activity:{1}", queueName.ToString(), subscriber.ActivityQualifiedName);
}
}
}
}
/// <summary>
/// At termination/completion point, need to move messages from all queues to the pending queue
/// </summary>
internal void MoveAllMessagesToPendingQueue()
{
lock (SyncRoot)
{
Queue pendingQueue = this.pendingQueueState.Messages;
foreach (EventQueueState queueState in this.persistedQueueStates.Values)
{
Queue queue = queueState.Messages;
while (queue.Count != 0)
{
pendingQueue.Enqueue(queue.Dequeue());
}
}
}
}
#endregion
#region private root q service helpers
private EventQueueState GetEventQueueState(IComparable queueName)
{
EventQueueState queueState = GetQueue(queueName);
if (queueState.Dirty)
{
string message =
string.Format(CultureInfo.CurrentCulture, ExecutionStringManager.QueueBusyException, new object[] { queueName });
throw new QueueException(message, MessageQueueErrorCode.QueueNotAvailable);
}
return queueState;
}
private void NewQueue(IComparable queueID, bool enabled, bool transactional)
{
WorkflowTrace.Runtime.TraceInformation("Queuing Service: Creating new Queue with ID {0} for {1}", queueID.GetHashCode(), queueID);
if (this.persistedQueueStates.ContainsKey(queueID))
{
object[] args =
new object[] { System.Messaging.MessageQueueErrorCode.QueueExists, queueID };
string message =
string.Format(CultureInfo.CurrentCulture, ExecutionStringManager.EventQueueException, args);
throw new QueueException(message, MessageQueueErrorCode.QueueExists);
}
EventQueueState queueState = new EventQueueState();
queueState.Enabled = enabled;
queueState.queueName = queueID;
queueState.Transactional = transactional;
this.persistedQueueStates.Add(queueID, queueState);
}
internal EventQueueState GetQueue(IComparable queueID)
{
EventQueueState queue;
if (this.persistedQueueStates.TryGetValue(queueID, out queue))
{
queue.queueName = queueID;
return queue;
}
object[] args =
new object[] { System.Messaging.MessageQueueErrorCode.QueueNotFound, queueID };
string message =
string.Format(CultureInfo.CurrentCulture, ExecutionStringManager.EventQueueException, args);
throw new QueueException(message, MessageQueueErrorCode.QueueNotFound);
}
internal IEnumerable<IComparable> QueueNames
{
get
{
List<IComparable> list = new List<IComparable>(this.persistedQueueStates.Keys);
foreach (IComparable name in list)
{
if (name is String && (String)name == pendingNotification)
{
list.Remove(name);
break;
}
}
return list;
}
}
private void ApplyChangesFrom(EventQueueState srcPendingQueueState, Dictionary<IComparable, EventQueueState> srcPersistedQueueStates)
{
lock (SyncRoot)
{
Dictionary<IComparable, EventQueueState> modifiedItems = new Dictionary<IComparable, EventQueueState>();
foreach (KeyValuePair<IComparable, EventQueueState> mergeItem in srcPersistedQueueStates)
{
Debug.Assert(mergeItem.Value.Transactional, "Queue inside a transactional context is not transactional!");
if (mergeItem.Value.Transactional)
{
if (this.persistedQueueStates.ContainsKey(mergeItem.Key))
{
EventQueueState oldvalue = this.persistedQueueStates[mergeItem.Key];
if (!oldvalue.Dirty)
{
// we could get here when there
// are conflicting create Qs
string message =
string.Format(CultureInfo.CurrentCulture, ExecutionStringManager.QueueBusyException, new object[] { mergeItem.Key });
throw new QueueException(message, MessageQueueErrorCode.QueueNotAvailable);
}
}
modifiedItems.Add(mergeItem.Key, mergeItem.Value);
}
}
// no conflicts detected now make the updates visible
foreach (KeyValuePair<IComparable, EventQueueState> modifiedItem in modifiedItems)
{
// shared queue in the root, swap out to new value
// or add new item
this.persistedQueueStates[modifiedItem.Key] = modifiedItem.Value;
}
this.pendingQueueState.CopyFrom(srcPendingQueueState);
}
}
// message arrival async notification
private void NotifyExternalSubscribers(IComparable queueName, EventQueueState qState, Object eventInstance)
{
NotifySynchronousSubscribers(queueName, qState, eventInstance);
NotifyAsynchronousSubscribers(queueName, qState, 1);
}
private void NotifySynchronousSubscribers(IComparable queueName, EventQueueState qState, Object eventInstance)
{
QueueEventArgs args = new QueueEventArgs(queueName);
for (int i = 0; i < qState.SynchronousListeners.Count; ++i)
{
if (qState.SynchronousListeners[i].HandlerDelegate != null)
qState.SynchronousListeners[i].HandlerDelegate(new WorkflowQueue(this, queueName), args);
else
qState.SynchronousListeners[i].EventListener.OnEvent(new WorkflowQueue(this, queueName), args);
}
}
// returns a valid state only if transactional and entry exists
private EventQueueState MarkQueueDirtyIfTransactional(IComparable queueName)
{
lock (SyncRoot)
{
Debug.Assert(this.rootQueuingService == null, "MarkQueueDirty should be done at root");
if (!this.persistedQueueStates.ContainsKey(queueName))
return null;
EventQueueState queueState = GetQueue(queueName);
if (!queueState.Transactional)
return null;
if (queueState.Dirty)
return queueState; // already marked
queueState.Dirty = true;
if (this.dirtyQueues == null)
this.dirtyQueues = new List<IComparable>();
// add to the list of dirty queues
this.dirtyQueues.Add(queueName);
return queueState;
}
}
private void AddMessageArrivedEventHandler(WorkflowQueuingService handler)
{
lock (SyncRoot)
{
if (this.messageArrivalEventHandlers == null)
this.messageArrivalEventHandlers = new List<WorkflowQueuingService>();
this.messageArrivalEventHandlers.Add(handler);
}
}
private void RemoveMessageArrivedEventHandler(WorkflowQueuingService handler)
{
lock (SyncRoot)
{
if (this.messageArrivalEventHandlers != null)
this.messageArrivalEventHandlers.Remove(handler);
if (this.dirtyQueues != null)
{
foreach (IComparable queueName in this.dirtyQueues)
{
EventQueueState qState = GetQueue(queueName);
qState.Dirty = false;
}
}
}
}
#endregion
#region inner QueuingService functions
private bool IsTransactionalQueue(IComparable queueName)
{
// check inner service for existense
if (!this.persistedQueueStates.ContainsKey(queueName))
{
EventQueueState queueState = this.rootQueuingService.MarkQueueDirtyIfTransactional(queueName);
if (queueState != null)
{
// if transactional proceed to the inner queue service
// for this operation after adding the state
EventQueueState snapshotState = new EventQueueState();
snapshotState.CopyFrom(queueState);
this.persistedQueueStates.Add(queueName, snapshotState);
return true;
}
return false;
}
return true; // if entry exits, it must be transactional
}
private void SubscribeForRootMessageDelivery()
{
if (this.rootQueuingService == null)
return;
this.rootQueuingService.AddMessageArrivedEventHandler(this);
}
private void UnSubscribeFromRootMessageDelivery()
{
if (this.rootQueuingService == null)
return;
this.rootQueuingService.RemoveMessageArrivedEventHandler(this);
}
// listen on its internal(parent) queuing service
// messages and pull messages. There is one parent queuing service visible to the external
// host environment. A queueing service snapshot exists per atomic scope and external messages
// for existing queues need to be pushed through
private void OnItemEnqueued(IComparable queueName, object item)
{
if (this.persistedQueueStates.ContainsKey(queueName))
{
// make the message visible to inner queueing service
EventQueueState qState = GetQueue(queueName);
if (!qState.Enabled)
{
object[] msgArgs = new object[] { System.Messaging.MessageQueueErrorCode.QueueNotFound, queueName };
string message = string.Format(CultureInfo.CurrentCulture, ExecutionStringManager.EventQueueException, msgArgs);
throw new QueueException(message, MessageQueueErrorCode.QueueNotAvailable);
}
qState.Messages.Enqueue(item);
NotifyExternalSubscribers(queueName, qState, item);
}
}
private void OnItemSafeEnqueued(IComparable queueName, object item)
{
if (this.persistedQueueStates.ContainsKey(queueName))
{
// make the message visible to inner queueing service
EventQueueState qState = GetQueue(queueName);
if (!qState.Enabled)
{
object[] msgArgs = new object[] { System.Messaging.MessageQueueErrorCode.QueueNotFound, queueName };
string message = string.Format(CultureInfo.CurrentCulture, ExecutionStringManager.EventQueueException, msgArgs);
throw new QueueException(message, MessageQueueErrorCode.QueueNotAvailable);
}
qState.Messages.Enqueue(item);
NotifySynchronousSubscribers(queueName, qState, item);
}
}
internal void Complete(bool commitSucceeded)
{
if (commitSucceeded)
{
this.rootQueuingService.ApplyChangesFrom(this.pendingQueueState, this.persistedQueueStates);
}
UnSubscribeFromRootMessageDelivery();
}
#endregion
#region Pre-persist and Post-persist helpers for queuing service states
// Created for ref. 20575
internal void PostPersist(bool isPersistSuccessful)
{
// If persist is unsuccessful, we'll undo the changes done
// because of the call to .Complete() in PrePresist
if (!isPersistSuccessful)
{
Debug.Assert(rootWorkflowExecutor.CurrentAtomicActivity != null);
Debug.Assert(pendingQueueStateSnapshot != null);
Debug.Assert(persistedQueueStatesSnapshot != null);
TransactionalProperties transactionalProperties = rootWorkflowExecutor.CurrentAtomicActivity.GetValue(WorkflowExecutor.TransactionalPropertiesProperty) as TransactionalProperties;
Debug.Assert(transactionalProperties != null);
// Restore queuing states and set root activity's dependency properties to the new values.
pendingQueueState = pendingQueueStateSnapshot;
persistedQueueStates = persistedQueueStatesSnapshot;
rootWorkflowExecutor.RootActivity.SetValue(WorkflowQueuingService.RootPersistedQueueStatesProperty, persistedQueueStatesSnapshot);
rootWorkflowExecutor.RootActivity.SetValue(WorkflowQueuingService.PendingMessagesProperty, pendingQueueStateSnapshot.Messages);
// Also call Subscribe...() because the .Complete() call called Unsubscribe
transactionalProperties.LocalQueuingService.SubscribeForRootMessageDelivery();
}
// The backups are no longer necessary.
// The next call to PrePresistQueuingServiceState() will do a re-backup.
persistedQueueStatesSnapshot = null;
pendingQueueStateSnapshot = null;
}
// Created for ref. 20575
internal void PrePersist()
{
if (rootWorkflowExecutor.CurrentAtomicActivity != null)
{
// Create transactionalProperties from currentAtomicActivity
TransactionalProperties transactionalProperties = this.rootWorkflowExecutor.CurrentAtomicActivity.GetValue(WorkflowExecutor.TransactionalPropertiesProperty) as TransactionalProperties;
// Create backup snapshot of root queuing service's persistedQueuesStates
// qService.persistedQueueStates is changed when LocalQueuingService.Complete is called later.
persistedQueueStatesSnapshot = new Dictionary<IComparable, EventQueueState>();
foreach (KeyValuePair<IComparable, EventQueueState> kv in persistedQueueStates)
{
EventQueueState individualPersistedQueueStateValue = new EventQueueState();
individualPersistedQueueStateValue.CopyFrom(kv.Value);
persistedQueueStatesSnapshot.Add(kv.Key, individualPersistedQueueStateValue);
}
// Create backup snapshot of root queuing service's pendingQueueState
// qService.pendingQueueState is changed when LocalQueuingService.Complete is called later.
pendingQueueStateSnapshot = new EventQueueState();
pendingQueueStateSnapshot.CopyFrom(pendingQueueState);
// Reconcile differences between root and local queuing services.
transactionalProperties.LocalQueuingService.Complete(true);
}
}
#endregion Pre-persist and post-persist helpers for queuing service states
}
}
|