COM series - actual exercise MSMQ (Message Queue) (2)
tornado
Keywords: COM , MSMQ.
?
Overview:
How to pass the ADO record set in MSMQ may be a topic that many people concerned. This means that some complicated things can be delivered, such as binary field content, etc. The record set can be directly transmitted in the MSMQ message body. Because the ADO recordset assembly implements the IPRESistStream interface.
There is also a problem that how to synchronize the message sent by the sender. Although MSMQ is asynchronous transmission, synchronous transmission is still more important. MSMQ provides arrived events, similar to Winsock. To accomplish this feature
Let's use the example to explain the above two problems.
Development environment: WinXP VB6
Message sending program:
Option expedition
?
Private subdquit_click ()
Unload me
End Sub
?
'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
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??? DIM conn as new adodb.connection
??? DIM RS as new adoDb.recordset
??? conn.open "provider = SQLOLEDB.1; PERSIST security info = true; user ID = sa; password =; initial catalog = northwind; data source = localhost"
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??? rs.cursorlocation = aduseclient
?? rs.open "Select * from Employees", conn, adopenstatic, AdlockReadonly ???
??? 'Fill information
??? qMessage.Label = txtmsglabel.text 'message title
??? qmessage.body = rs' Send record sets as 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
After the END SUB runs the program, enter the content, select priority, and send it.
?
?
Message Receive Procedure: Partial Note The same as above
Option expedition
DIM Qinfo As New MSMQQueueInfo
DIM QOBJECT AS MSMQQueue
DIM WITHEVENTS QMSMQEvent As MSMQEvent
'Quote Microsoft Message Queue 3.0 Object Library
?
Private subdquit_click ()
Unload me
End Sub
?
Private sub flow_load ()
??? '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_receive_access, mq_deny_none)
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??? 'connection event
??? set qmsmqevent = new msmqevent
??? qobject.enablenotification QMSMQEvent, MQMSG_Current, 1000
End Sub
Private sub qmsmqevent_arrived (byval qurive as ign)
? 'Get the current message
? DIM QMESSAGE AS MSMQMESSAGE
• set qmessage = queue.peekcurrent? Msgbox "has new news" & qMessage.Label
?
? lblmsglabel.caption = qMessage.label
? DIM RS as new adodb.recordset
? Set = quessage.body
? Set datagrid1.datasource = rs
?
? 'Get the next one
qobject.enablenotification qmsmqevent, MQMSG_Next, 1000
End Sub
?
Private sub qmsmqevent_arrivederror (Byval Queue As Object, ByVal Error AS Long)
? Msgbox "Errors" & ErrorCode
End Sub
?
?
After running, you can see the message, as shown below:
?
The content of MSMQ is temporarily annihilated. I hope we can communicate.
Source code download: