MSMQ VB Operation CodeSteps (Simplified Edition)

zhaozj2021-02-16  55

MSMQ VB Operation Code / Steps

1. Install MSMQ, get Microsoft Message Queue 2.0 Objtec libary Add type library references in VB 2, declare the variables that need to be used DIM MSGQUEUE AS MSMQQQueue '- Queen DIM QueryObj AS MSMQQuery' - Query DIM QueueInfo As MsmqQueUeInfo '- Queue Information DIM MSG AS MSMQMESSAGE3, created (private queue, local) set queueinfo = new msmqqueueInfo queueinfo.pathname = "./private $/" QueueInfo.Label = "inqueue" queueinfo.create "- Creation queue

4, sends a message (assuming the message queue has been created) Set queueInfo = New MSMQQueueInfo queueInfo.PathName = "./Private$/in" 'queueInfo.Label = "InQueue" Set msgqueue = queueInfo.Open (MQACCESS.MQ_SEND_ACCESS, MQSHARE.MQ_DENY_NONE ) IF (msgqueue.isopen ()) THEN SET MSG = new msmqmessage msg.label = "hi" msg.body = "this is a msg test" - can be any variable, just support IDispatch interface and sustainable interface MSG .Send msgqueue msgqueue.close debug.print "send!" End if5, active acceptance message (Note this is a blocking operation) set queueinfo = new msmqqueueInfo queueinfo.pueueinfo queueinfo.pathname = "./private there/in" 'queueinfo.Label = " InQueue "Set msgqueue = queueInfo.Open (MQACCESS.MQ_RECEIVE_ACCESS, MQSHARE.MQ_DENY_NONE) If (msgqueue.IsOpen ()) Then Set msg = msgqueue.Receive Debug.Print" recvied! "& msg.Body End If6, can also be used MSMQ Event to make a message announcement

DIM WITHEVENTS EVT AS MSMQEvent

Dim queueInfo As MSMQQueueInfoDim q As MSMQQueueDim msg As MSMQMessagePrivate Sub Form_Load () Set queueInfo = New MSMQQueueInfo queueInfo.PathName = "./Private$/in" 'queueInfo.Label = "InQueue" Set q = queueInfo.Open (MQACCESS.MQ_RECEIVE_ACCESS, MQSHARE.MQ_DENY_NONE) Set evt = new msmqEvent Q.enAblenotification Evtend Sub

Private sub evt_arrived (BYVAL Q As Object, BYVAL CURSOR As Long) 'Dim B AS Boolean Dim Theq AS MSMQQueue Set Theq = Q set msg = Theq.Receive () Debug.print Msg.Label "Body:" Msg.Body Theq.enablenotification Evtend Sub Note the DIM WITHEVENTS EVT AS MSMQEVENT statement

7. Significance can be cross-process messaging, and even the VB program interact with the .NET program, if configured as enterprise MSMQ, can constitute a distributed message processing system, which is very advantageous for large applications.

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

New Post(0)