The system/q component is an important part of the tuxedo system. It provides a queue mechanism that allows messages to be stored in persistent media (such as disks) according to certain queuing rules) or non-persistent media (such as memory), and then forwarded to other processing processes. This storage and forwarding mechanism ensures that messages transmitted between two communication entities are not lost or retransmitted, thus ensuring transaction integrity.
System/Q queue Communication
System/Q uses two servers provided by the tuxedo System: The Message Queue Server tmqeue and the message forwarding server tmqforward. Tmqeue is used to manage the queue of a message. tmqforward is used to retrieve the message from the queue and forward it to the server for processing. Then, the processing result is returned to the response queue.
Figure 1
System/Q queue communication can be divided into two types: client-to-client and client-to-server, the difference is that the former does not need to forward messages, while the latter needs to forward messages to the server request service for processing. Therefore, the forwarding server tmqforward is not required in the configuration file.
Communication between the client and the client
This end-to-end communication mode is a simple application of the queue system. Figure 1 shows the end-to-end asynchronous communication mode. The left client writes a message to the queue through the tpenqueue () function, and then performs the following work. The right client receives and processes the message through the tpdequeue () function, then, write the message to the queue. The client on the left extracts the message from the queue. At this point, a communication cycle is completed.
If synchronous communication is used, after the client on the left writes messages to the queue, the message is blocked. The process continues to be executed only after the Response Message from the client on the right is obtained.
Client-to-Server Communication
This communication mode is a common application of system/Q. The following is the communication process between the client and the server:
Figure 2
In Figure 2, the left side is the client, the right side is the server, and the middle side is the message queue system. The server on the right provides two services: service1 and service2. In the message queue system, tms_qm is the transaction management server of system/Q. It defines a Queue Server group and a queue space, the queue space defines four message queues. service1 and service2 correspond to the queues of two services with the same name respectively (this is a customary naming rule. If a client requests the service1 service on the server, put the request message in the service1 Queue ). Client_rply1 is the response queue of service1 service to the request, and failureq is the response queue for server operation failure. Process description in the figure: ① The client initiates a request to service1; ② The tmqueue queues the request message; ③ The tmqueue reports to the Client whether the queue is successful; ④ the tmqforward retrieves the request message; ⑤ send the request message to service1; ⑥ service1 responds to the request; 7tmqforward queues the response message; The reply client requests retrieval of the response message; and tmqueue retrieves the response message; whether the ⑩ tmqueue message is obtained successfully.
Queue Management
Before using the system/q component, you must create a queue space and queue. The Operations and Management of the queue by TUXEDO are basically completed through qmadmin. In NT, qmadmin is a DOS application. Before running, you must set the environment variable qmconfig, for example:
Set qmconfig = c: \ qsample \ que
Qmadmin uses this variable to create a queue device que in the C: \ qsample directory. When qmadmin is run, an operation environment appears. Create the queue device, queue space, and Message Queue according to the following command:
> Crdl c: \ qsample \ que 0 400
... ...
> Qspacecreate-N 100b
Queue space name: qspace
IPC key for queue space: 62839
Size of queue space in disk pages: 100
Number of queues in queue space: 6
...
> Qopen qspace
> Qcreate
Queue name: String
Queue Order (priority, time, expiration, FIFO, LIFO): FIFO
The crdl command creates a queue device named que with a size of 400 disk blocks. Then, the qspacecreate command is used to create a queue Space named qspace with a size of 100 disk blocks; finally, a queue named string is created in qspace using the qcreate command, which uses the first-in-first-out (FIFO) queuing rule. To complete the example described in this article, you must create two other Queues: rplyq and errque.
System/Q application instance
Qsample is an example of tuxedo used to demonstrate system/Q. The client puts a message containing a string into the string queue and tmqforward forwards the message to the string service, it is required to convert the strings in the message body to uppercase. After the processing is completed, tmqforward puts the response message into the response queue rplyq, and the client extracts the Response Message from rplyq to print the output.
Qsample Service Program
Service program server. the content of C is as follows. Row 3 defines a service named string. Row 3 converts uppercase letters, and row 3 returns the conversion result to the tmqforward server through the tpreturn function, the conversion is successful.
1. # include "atmi. H"
2. String (MSG)
3. tpsvcinfo MSG;
4 .{
5. Char STR;
6. Int I;
7. Str = (char) MSG-> data;
8. For (I = 0; I <MSG-> Len; I ++ ){
9. Str [I] = toupper (STR [I]);
10 .}
11. tpreturn (tpsuccess, 0, MSG-> data, 0l, 0 );
Set Environment Variables
In addition to setting qmconfig, you also need to set the following environment variables according to the actual situation: tuxdir (installation directory of TUXEDO), appdir, and tuxconfig.
Create a Message Queue
Qsample uses a queue space qspace and three queue strings, rplyq, and errque. The best way to create them on the NT platform is to use the nmake command, that is, "C: \ qsample \ nmake/F qsapme. nt que". You can also create them manually.
Create transaction log
When managing queue transactions, tms_qm needs to use the log device. The nmake command is also used to create the device:
C: \ qsample \ nmake/F qsapme. nt tlog
It executes a set of pre-defined tuxedo commands in qsample. nt.
Customer program of qsample
The client. C process of the customer program is to join the tuxedo system → queue the message → exit the system. The simpapp directly calls the toupper service through the tpcall () function, while the qsample puts the message into the queue string, and the tmqforward server calls the string service through tpcall. Below are some source codes, with the error capture and processing omitted:
# Include "uunix. H"
# Include "atmi. H"
Main (){
Char reqstr;
/String to be sent/
Long Len;
/The length of the returned string/
Tpqctl qctl;
/System/Q control structure/
Tpinit (null );
/Add to the tuxedo system/
Reqstr = tpalloc ("string", null, 100 ));
/System/Q control structure/
(Void) strcpy (reqstr, "This is a Q example ");
/Enter the string to be converted/
(Void) printf ("before: % s \ n", reqstr );
Qctl. Flags = tpqreplyq;/specifies the response queue/
(Void) strcpy (qctl. replyqueue, "rplyq");/Specify the response queue name as rplyq/
/Use a message to enter the string queue of the queue space qspace. The reqstr is the message body/
Tpenqueue ("qspace", "string", (tpqctl) & qctl, (char) reqstr, 0, 0 );
Qctl. Flags = tpqwait;
/Set the Wait response tag/
Tpdequeue ("qspace", "rplyq", (tpqctl) & qctl, (char) & reqstr, & Len, tpnotime );
(Void) printf ("after: % s \ n", reqstr );
Tpfree (reqstr );
/Release the memory and exit/
(Void) tpterm ();
/Exit the tuxedo system/
}
Modify configuration file
The configuration file UBB. Sample defines three servers: Server, tmqueue, tmqforward, and group1 and que1. Server provides the string service. tmqueue and tmqforward belong to the que1 group. The openinfo parameter in the que1 group defines the queue Resource Manager TUXEDO/QM, queue device, and queue space. The clopt parameter in the last row defines to forward messages to the string service.
Groups
Group1
Lmid = Site1 kgno = 1
Tmsname = TMS tmscount = 2
Que1
Lmid = Site1 kgno = 2
Tmsname = tms_qm tmscount = 2
Openinfo = "TUXEDO/QM: C: \ qsample \ que; qspace"
Servers
Default: clopt = "-"
Server srvgrp = group1 srvid = 1
Tmqueue
Srvgrp = que1 srvid = 1
Grace = 0 restart = y Conv = n maxgen = 10
Clopt = "-S qspace: tmqueue --"
Tmqforward
Srvgrp = que1 srvid = 5 Grace = 0 restart = y Conv = n maxgen = 10
Compile and run
Make sure that the include and Lib variables in the make file qsample. NT contain the header files and library files of the tuxedo system, and then compile the files using the following command:
C: \ qsample \ nmake/F qsample. nt all
After successful execution, use the tmloadcfcommand to configure the file, use the tmbootcommand to start the application, and run the client program client.exe to view the processing result of the string service.