Commits

Dariusz Suchojad  committed c4595fa

Adding an example of using MQCONNX, MQPUT, MQSUB and MQSUBRQ.

  • Participants

Comments (0)

Files changed (4)

File mqsubrq/README

+Code in this directory is meant to illustrate the WebSphere MQ MQSUBRQ call.
+All the secondary features have been kept to minimum, for instance,
+there's no parsing of command line options as that would only obscure
+the point.
+
+The whole thing consists of several source code files:
+
+- retained-put.c - publishes a message on a topic
+- retained-get.c - uses MQSUBRQ to request a retained message
+- SConstruct - used for compiling the programs
+- README - the file you're reading right now
+
+Prior to compiling the programs you need to modify both C source code files
+and adjust the values of:
+
+- topic_name, qm_name, channel, connection_name in retained-put.c
+- topic_string, qm_name, channel, connection_name in retained-get.c
+
+You also need to execute an MQSC script along the lines of what's below
+on the target queue manager:
+
+DEFINE TOPIC(TOPIC.1) TOPICSTR('/topic/1')
+
+Compiling the programs is a matter of running the "scons" command in
+the directory the C programs are in. Visit the SCons web site if you don't
+have it installed yet - http://www.scons.org/
+
+So, running
+
+$ scons
+
+will result in two files being created - "retained-put" and "retained-get".
+The files may be now executed, which will result in issuing an MQCONNX call
+to a remote queue manager, publishing a message and getting it
+as a retained subscription later on.
+
+Enjoy!
+
+Contact: dsuch at gefira.pl

File mqsubrq/SConstruct

+
+# Adjust the values below, libpath in particular, if there are
+# any errors building the code on your system.
+libs = ["mqm_r"]
+libpath = "/opt/mqm/lib64"
+
+programs = ("retained-get.c", "retained-put.c")
+
+for program in programs:
+    Program(program, LIBS=libs, LIBPATH=libpath)
+

File mqsubrq/retained-get.c

+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <ctype.h>
+ #include <string.h>
+
+ #include <cmqc.h>
+ #include <cmqxc.h>
+
+int main(int argc, char **argv) {
+
+   MQHCONN  h_conn;
+   MQHOBJ h_obj2;
+   MQLONG comp_code;
+   MQLONG reason;
+   MQLONG buflen = 100;
+   MQLONG messlen;
+   MQBYTE buffer[101];
+
+   char topic_string[]   = "/topic/1";
+
+   char qm_name[]   = "QM01";
+   char channel[]   = "SVRCONN.1";
+   char connection_name[] = "192.168.1.139(1434)";
+
+   MQCNO cno = {MQCNO_DEFAULT};
+   MQCD client_conn = {MQCD_CLIENT_CONN_DEFAULT};
+   MQMD md = {MQMD_DEFAULT};
+   MQSD sd = {MQSD_DEFAULT};
+   MQGMO gmo = {MQGMO_DEFAULT};
+   MQSRO sro = {MQSRO_DEFAULT};
+   MQHOBJ h_sub = MQHO_NONE;
+
+   MQCHAR res_topic_str[151];
+   memset(res_topic_str, 0 , sizeof(res_topic_str));
+
+   strncpy(client_conn.ChannelName, channel, MQ_CHANNEL_NAME_LENGTH);
+   strncpy(client_conn.ConnectionName, connection_name, MQ_CONN_NAME_LENGTH);
+
+   cno.ClientConnPtr = &client_conn;
+   cno.Version = MQCNO_VERSION_2;
+
+   printf("Channel: %s\n", client_conn.ChannelName);
+   printf("Connection name: %s.\n\n", client_conn.ConnectionName);
+
+   MQCONNX(qm_name, &cno,  &h_conn, &comp_code, &reason);
+   printf("MQCONNX ended with reason code %d\n", reason);
+
+   sd.Options =  MQSO_CREATE | MQSO_NON_DURABLE | MQSO_FAIL_IF_QUIESCING | MQSO_MANAGED | MQSO_PUBLICATIONS_ON_REQUEST;
+   sd.ObjectString.VSPtr = &topic_string;
+   sd.ObjectString.VSLength = (MQLONG)strlen(topic_string);
+   printf("sd.Options %d\n", sd.Options);
+
+   MQSUB(h_conn, &sd, &h_obj2, &h_sub, &comp_code, &reason);
+   printf("MQSUB ended with reason code %d\n", reason);
+
+   MQSUBRQ(h_conn, h_sub, MQSR_ACTION_PUBLICATION, &sro, &comp_code, &reason);
+   printf("MQSUBRQ ended with reason code %d\n", reason);
+   printf("sro.NumPubs: %i \n", sro.NumPubs);
+
+   gmo.Options =   MQGMO_WAIT | MQGMO_NO_SYNCPOINT | MQGMO_CONVERT | MQGMO_FAIL_IF_QUIESCING | MQGMO_ACCEPT_TRUNCATED_MSG;
+   gmo.WaitInterval = 150;
+
+   MQGET(h_conn, h_obj2, &md, &gmo, buflen, buffer, &messlen, &comp_code, &reason);
+   printf("MQGET ended with reason code %d\n", reason);
+   if(comp_code == MQCC_OK) {
+         buffer[messlen] = '\0';
+         printf("message <%s>\n", buffer);
+   }
+
+   MQDISC(&h_conn, &comp_code, &reason);
+   printf("MQDISC ended with reason code %d\n", reason);
+
+   return 0;
+}

File mqsubrq/retained-put.c

+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <ctype.h>
+ #include <string.h>
+
+ #include <cmqc.h>
+ #include <cmqxc.h>
+
+int main(int argc, char **argv) {
+
+   MQHCONN  h_conn;
+   MQHOBJ h_obj;
+   MQLONG comp_code;
+   MQLONG reason;
+
+   char topic_name[]   = "TOPIC.1";
+   char payload[] = "My payload";
+
+   char qm_name[]   = "QM01";
+   char channel[]   = "SVRCONN.1";
+   char connection_name[] = "192.168.1.139(1434)";
+
+   MQCNO cno = {MQCNO_DEFAULT};
+   MQCD client_conn = {MQCD_CLIENT_CONN_DEFAULT};
+   MQOD td = {MQOD_DEFAULT};
+   MQMD md = {MQMD_DEFAULT};
+   MQPMO pmo = {MQPMO_DEFAULT};
+
+   MQCHAR res_topic_str[151];
+   memset(res_topic_str, 0 , sizeof(res_topic_str));
+
+   strncpy(client_conn.ChannelName, channel, MQ_CHANNEL_NAME_LENGTH);
+   strncpy(client_conn.ConnectionName, connection_name, MQ_CONN_NAME_LENGTH);
+
+   cno.ClientConnPtr = &client_conn;
+   cno.Version = MQCNO_VERSION_2;
+
+   printf("Channel: %s\n", client_conn.ChannelName);
+   printf("Connection name: %s.\n\n", client_conn.ConnectionName);
+
+   MQCONNX(qm_name, &cno,  &h_conn, &comp_code, &reason);
+   printf("MQCONNX ended with reason code %d\n", reason);
+
+   td.ObjectType = MQOT_TOPIC;
+   td.Version = MQOD_VERSION_4;
+   strncpy(td.ObjectName, topic_name, MQ_TOPIC_NAME_LENGTH);
+   td.ResObjectString.VSPtr = res_topic_str;
+   td.ResObjectString.VSBufSize = sizeof(res_topic_str)-1;
+
+   MQOPEN(h_conn, &td, MQOO_OUTPUT | MQOO_FAIL_IF_QUIESCING, &h_obj, &comp_code, &reason);
+   printf("MQOPEN ended with reason code %d\n", reason);
+
+   pmo.Options = MQPMO_FAIL_IF_QUIESCING | MQPMO_RETAIN;
+   md.Persistence = MQPER_PERSISTENT;
+
+   MQPUT(h_conn, h_obj, &md, &pmo, (MQLONG)strlen(payload)+1, payload, &comp_code, &reason);
+   printf("MQPUT ended with reason code %d\n", reason);
+
+   MQCLOSE(h_conn, &h_obj, MQCO_NONE, &comp_code, &reason);
+   printf("MQCLOSE ended with reason code %d\n", reason);
+
+   MQDISC(&h_conn, &comp_code, &reason);
+   printf("MQDISC ended with reason code %d\n", reason);
+
+   return 0;
+}