使用 .Net 的 Oracle 高级排队 [英] Oracle Advanced Queueing with .Net

查看:62
本文介绍了使用 .Net 的 Oracle 高级排队的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何使用 PL/SSQL 和 ODP.NET 从 C# 实现 Oracle Advance Queue?我在 C# 或 VB.NET 中找不到包含具体示例的单个示例或资源.理想情况下,我想要一些关于如何使用简单类型(XMl/string)使消息入队和出队的示例.

Somebody knows how to implement Oracle Advance Queue from C# using PL/SSQL and ODP.NET? I can't find a single example or resource with concrete examples in C# or VB.NET. Ideally I would like some examples on how the enqueue and dequeue messages with simple types (XMl/string).

推荐答案

我无法在最佳实践方面为您提供帮助,但我可以在 UDT 队列方面为您提供帮助.在处理队列之前,您需要从数据库生成自定义类型到您的 C# 项目中.假设您安装了 Visual Studio 和 ODP.NET,您只需通过服务器资源管理器连接到数据库,找到您的 UDT,右键单击并选择生成自定义类..."这些类直接映射到您的 UDT 并使用存储出队信息.

I can't help you with the best practices, but I can help you with a UDT Queue. Before you deal with the queue, you need to generate custom types from the database into your C# project. Assuming you have Visual Studio and ODP.NET installed, you simply need to connect to the database through the Server Explorer, locate your UDTs, right click and choose "Generate Custom Class..." These classes map directly to your UDTs and are used to store the Dequeued information.

以下是用于将消息入队的代码示例:

Here is an example of the code you would use to enqueue a message:

private void main(string[] args)
{
    string _connstring = "Data Source=host/DB;User
    Id=USER;Password=PASSWORD1;";

        OracleConnection _connObj = new OracleConnection(_connstring);

        // Create a new queue object
        OracleAQQueue _queueObj = new OracleAQQueue("UDT_NAME", _connObj);

        _connObj.Open();

        OracleTransaction _txn = _connObj.BeginTransaction();

        // Set the payload type to your UDT
        _queueObj.MessageType = OracleAQMessageType.Udt;
        _queueObj.UdtTypeName = "UDT_NAME";

        // Create a new message object
        OracleAQMessage _msg = new OracleAQMessage();

        // Create an instance of JobClass and pass it in as the payload for the
        // message
        UDT_CUSTOM_CLASS _custClass = new UDT_CUSTOM_CLASS();
        // Load up all of the properties of custClass
        custClass.CustString = "Custom String";
        custClass.CustInt = 5;

        _msg.Payload = custClass;

        // Enqueue the message
        _queueObj.EnqueueOptions.Visibility = OracleAQVisibilityMode.OnCommit;
        _queueObj.Enqueue(_msg);

        _txn.Commit();
        _queueObj.Dispose();
        _connObj.Close();
        _connObj.Dispose();
        _connObj = null;
}

与出队类似的过程:

private void main(string[] args)
{
    string _connstring = "Data Source=host/DB;User
    Id=USER;Password=PASSWORD1;";

    OracleConnection _connObj = new OracleConnection(_connstring);

    // Create a new queue object
    OracleAQQueue _queueObj = new OracleAQQueue("UDT_NAME", _connObj);

    // Set the payload type to your UDT
    _queueObj.MessageType = OracleAQMessageType.Udt;
    _queueObj.UdtTypeName = "UDT_NAME";

    _connObj.Open();

    OracleTransaction _txn = _connObj.BeginTransaction();

    // Dequeue the message.
    _queueObj.DequeueOptions.Visibility = OracleAQVisibilityMode.OnCommit;
    _queueObj.DequeueOptions.Wait = 10;
    OracleAQMessage _deqMsg = _queueObj.Dequeue();

    UDT_CUSTOM_CLASS data = (UDT_CUSTOM_CLASS)_deqMsg.Payload;

    // At this point, you have the data and can do whatever you need to do with it

    _txn.Commit();
    _queueObj.Dispose();
    _connObj.Close();
    _connObj.Dispose();
    _connObj = null;

}

这是一个简单"的例子.我从 Ed Zehoo 的 Pro ODP.NET for Oracle Database 11g 中提取了大部分内容.这是一本优秀的书,我强烈推荐它来帮助您更好地了解 OPD.NET 的来龙去脉.您可以在此处购买电子书:http://apress.com/book/view/9781430228202.如果您输入优惠券代码 MACWORLDOC,您可以获得 21.00 美元的电子书.该优惠仅适用于受密码保护的 PDF 格式的电子书.我希望这会有所帮助!

That's a "simple" example. I pulled most of that out of Pro ODP.NET for Oracle Database 11g by Ed Zehoo. It's an excellent book and I strongly recommend it to help you gain a better understanding of the ins and outs of all things OPD.NET. You can buy the eBook here: http://apress.com/book/view/9781430228202. If you enter the coupon code MACWORLDOC, you can get the eBook for $21.00. That offer is only good for the eBook which comes in a password protected PDF format. I hope this helps!

这篇关于使用 .Net 的 Oracle 高级排队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆