Click here to Skip to main content
15,894,405 members
Articles / Programming Languages / Visual Basic

Oracle Advance Queue or Advanced Queuing from .NET (C#/VB/MC++)

Rate me:
Please Sign up or sign in to vote.
4.88/5 (26 votes)
22 Nov 2005CPOL23 min read 177.9K   3.7K   71  
How to use Oracle advance queue from a .NET enviroment.
//
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
//  PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER 
//  REMAINS UNCHANGED.
//
//  Email:  gustavo_franco@hotmail.com
//
//  Copyright (C) 2005 Franco, Gustavo 
//
using System;
using System.Threading;
using System.Windows.Forms;

using AQLib;
using DBTypes;

namespace OracleAQ
{
	public class PreConsumer : AQConsumer	
	{
		#region Variables Declaration
		private int	mPerformanceCounter = 0;
		
		//All the following data can be filled before the payload is called,
		//When payload arrive this info can be use to help payload to be procesed.
		private string mInternalInformation = "";
		#endregion

		#region Constructors
		public PreConsumer()
		{
		}
		#endregion

		#region Properties
		public string InternalInformation
		{
			get{return mInternalInformation;}
			set{mInternalInformation = value;}
		}
		#endregion

		#region Methods
		public override void ProcessPayload(object payload)
		{
			try
			{
				if (Form1.MainInstance != null && !Form1.MainInstance.IsDisposing)
					Form1.MainInstance.AddItemToMessagesList(
						"Got a payload with type: [" + payload.GetType().FullName + 
						"] HashCode: [" + payload.GetHashCode() + 
						"] Thread Id: [" + Thread.CurrentThread.GetHashCode() +
						"] Counter: [" + (mPerformanceCounter++) + "] and extra info is [" +
						mInternalInformation +
						"]");

				// Lets add a little delay between (0 and 2 secons) to allow dispatcher see that this consumer 
				// is busy and send next message to different consumer.
				Thread.Sleep(new Random().Next(2000));

				// THIS IS HOW IN REAL LIFE WILL BE, YOU WILL CAST THE INCOMING PAYLOAD TO THE EXPECTED TYPE
				// AFTER THAT YOU CAN START PROCESS IT.
				// Tm_Message_Payload_Ty tm_payload = (Tm_Message_Payload_Ty) payload;
			}
			catch(AQException ex)
			{
				if (Form1.MainInstance != null)
					Form1.MainInstance.ShowAQExceptionMessage(ex);
			}
			catch(Exception ex)
			{
				//This will happend when user close the main form without Stop Producer first.
				//OnClosing event call producer stop, but it is too late because now Form1 has to wait
				//for all consumer to finish, means the message queue is stopped, and so this consumer cannot 
				//insert a message in the list box then it is a dead lock.
				//On real life this will never happend if you finalize the producer properly, also in real life 
				//usually the Main Form is not used to interactive with consumers.
				if (ex.GetType() == typeof(System.Threading.ThreadAbortException))
					return;

				//There are several ways to finish it properly, but if I get ObjectDisposedException is beacuse
				//User closed the form when consumers still were running.
				//Just ignore this error... (see above)
				if (ex.GetType() == typeof(System.ObjectDisposedException))
					return;

				if (Form1.MainInstance != null)
					Form1.MainInstance.ShowExceptionMessage(ex);
			}
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Microsoft
United States United States
I started with programming about 19 years ago as a teenager, from my old Commodore moving to PC/Server environment Windows/UNIX SQLServer/Oracle doing gwBasic, QBasic, Turbo Pascal, Assembler, Turbo C, BC, Summer87, Clipper, Fox, SQL, C/C++, Pro*C, VB3/5/6, Java, and today loving C#.

Currently working as SDE on Failover Clustering team for Microsoft.

Passion for most programming languages and my kids Aidan&Nadia.

Comments and Discussions