COM + series - actual exercise MSMQ (message queue) (1)

zhaozj2021-02-12  148

COM series - actual exercise MSMQ (message queue) (1)

tornado

Keywords: COM , MSMQ.

?

Overview:

Talking about the message queue, you can't mention COM . In fact, COM is a combination of three technologies: DCOM, MTS and MSMQ.

Let's first understand what is the message queue (MSMQ)? MSMQ (Microsoft ?? Message ?? Queue, Microsoft Message Queue) is Windows 2000 (NT also has MSMQ, WIN95 / 98 / ME / XP does not include message queue services but supports the client's operation) The basis of communication in the operating system, is also for Create a distributed, loose access to the communication application. These applications can communicate through different kinds of networks, or with offline computer.

MSMQ is an asynchronous transmission mode that implements communication between multiple different applications, and the application of mutual communication can be distributed on the same machine, or may be distributed in any of the connected network spaces. Its implementation principle is that the sender of the message puts the information you want to send into a container (we call Message), then save it to a system public space message queue (Message ?? queue); Local or off-site messaging procedures proceed from the queue to its message.

The most important feature of MSMQ is that the message is saved when the connection is turned off, and the message is sent when the connection is restored.

An application is asynchronous data transfer between the client. The simplest application is that the user has completed the work of the day and passed the data in the form of a message at home. When the user landed on the next day, the office's computer will accept the transferred message and data Update.

To use MSMQ for software development, you need to install MSMQ. Open Control Panel, 'Add or Remove Programs, Add Delete Windows Components, and select Message Queue.

We use VB as a development tool, mainly considering the combination of VB and COM , the simple and easy to use of VB, so that users can easily accept COM .

This will enter the actual development phase after installation. Open Control Panel, Administrative Tools, Computer Management, and Message Queue. Establish a new message queue (special queue) and specify a name for it, assume that it is Testqueue, this name can be free. It can also be done by programming.

Windows NT and Windows2000, WinXP version of MSMQ are different. Windows NT uses MSMQ1.0 (MQOA10.TLB), Windows2000 uses MSMQ2.0 (MQOA20.TLB), WinXP uses MSMQ3.0 (MQOA.DLL)

Development environment: WinXP VB6

Message sending program:

Option expedition

'Quote Microsoft Message Queue 3.0 Object Library

Private subcmdsend_click ()

??? 'Creating the variable required by MSMQ

??? DIM Qinfo as msmqqueueInfo

??? DIM QOBJECT AS MSMQQueue

??? DIM QMESSAGE AS MSMQMESSAGE

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? 'instantiated queue information

??? set qinfo = new msmqqueueInfo

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? 'queue path

??? qinfo.pathname = "./private there/testqueue"

??? 'queue name

??? qinfo.label = "test queue"

??? 'Queue priority, support eight kinds. MQ_MIN_PPRIORITY is the lowest level, MQ_MAX_PRIORITY is the highest level

??? qinfo.basepriority = mq_min_priority ???

??? 'If the queue does not exist, then create

??? on Error Goto CheckQueue:

??? Qinfo.create False, False

??? goto queuecreated:

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

CheckQueue:

??? if not err.number = mq_error_queue_exists then

??????? msgbox "error:" hex (err.number) Err.Source Err.Description, vbcritical vbokonly, "Information Tips"

??? End IF

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Queuecreated:

??? 'turn on

??? set qobject = qinfo.open (MQ_SEND_ACCESS, MQ_DENY_NONE)

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? set qmessage = new msmqmessage

???????

??? 'Fill information

??? qMessage.Label = txtmsglabel.text 'message title

??? qMessage.body = txtmsgbody.text 'message content

??? qMessage.Priority = LstPriority.text 'priority (directly 0-7 numbers)

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? 'send messages

??? qMessage.send Qobject, MQ_NO_TRANSACTION

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? 'shut down

??? qobject.close

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? 'freed

??? set qinfo = Nothing

??? set qobject = Nothing

??? set qMessage = Nothing

End Sub

After running the program, enter the content, select priority, and send it. Open the message queue can be seen, as shown below:

?

Message Receive Procedure: Partial Note The same as above

Option expedition

DIM Qinfo As MsmqqueueInfo

DIM QOBJECT AS MSMQQueue

DIM QMESSAGE AS MSMQMESSAGE

?

Private subduDNext_click ()

??? set qinfo = new msmqqueueInfo

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? qinfo.pathname = "./private there/testqueue"

??? qinfo.label = "test queue"

??? 'This method will delete messages in the message queue

??? 'set qobject = qinfo.open (MQ_RECEIVE_ACCESS, MQ_DENY_NONE)

??? 'set qMessage = QObject.Receive (MQ_NO_TRANSACTION, FALSE, TRUE, 5)

??? 'This method does not delete messages in the message queue

??? set QObject = qinfo.open (MQ_PEEK_ACCESS, MQ_DENY_NONE)

??? set qMessage = Qobject.peek (MQ_NO_TRANSACTION, FALSE, TRUE, 5)

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? if qmessage is nothing then

??????? msgbox "There is no message in the message queue", Vbinformation Vbokonly, "Information Tips" ??????? Goto Nimentage:

??? ELSE

???????

??? lblmsglabel.caption = qMessage.label

??? lblmsgbody.caption = qMessage.body

??? lblpriorityLevel.caption = QMessage.Priority

??? lblarrivaltime.caption = qMessage.arrivedtime

??? End IF

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Nimentage:

??? qobject.close

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? set qinfo = Nothing

??? set qobject = Nothing

??? set qMessage = Nothing

??? EXIT SUB

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

CheckQueue:

??? msgbox "error:" Hex (Err.Number) Err.Source Err.Description, vbcritical vbokonly, "Information Tips"

End Sub

?

After running, you can see the message.

?

Message queues have other usages, for example, the most concerned database operation, promptly responding to the client's send message, and more.

What to say is that MSMQ should not be used in the following situations:

1. • Under the occasion of time requirements. Msmq is mainly delayed data.

2. ? High demand for message size. MSMQ increases the byte of the transfer message. Even if not encrypted, MSMQ will add information after the message is for ease of tracking.

3. • Although chat programs can be made, remote control procedures. But not recommended.

?

The best way to learn is written. I will continue to discuss with everyone in the following chapters.

?

转载请注明原文地址:https://www.9cbs.com/read-6733.html

New Post(0)