Commits

Dariusz Suchojad  committed 435a84f

Added an example of using MQ properties in C.

  • Participants
  • Parent commits c4595fa

Comments (0)

Files changed (2)

File message-properties/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 = ("message-properties.c",)
+
+for program in programs:
+    Program(program, LIBS=libs, LIBPATH=libpath)
+

File message-properties/message-properties.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;
+   MQHOBJ h_obj2;
+   MQLONG comp_code;
+   MQLONG reason;
+   MQHMSG msg_handle1 = MQHM_UNUSABLE_HMSG;
+   MQHMSG msg_handle2 = MQHM_UNUSABLE_HMSG;
+   MQLONG buflen = 100;
+   MQLONG messlen;
+   MQBYTE buffer[101];
+   MQBYTE value[64];
+   MQLONG actual_value_length;
+   MQLONG property_type = MQTYPE_STRING;
+   MQLONG pd_options = MQPD_NONE;
+
+   char qm_name[]   = "QM01";
+   char channel[]   = "SVRCONN.1";
+   char connection_name[] = "192.168.1.139(1434)";
+   
+   char queue_name[]   = "Q1";
+   char payload[] = "My payload";
+   
+   char property_name[] = "abc";
+   long property_name_length = 3;
+   
+   char property_value[] = "qwerty";
+   long property_value_length = 6;
+   
+   char selection_string[] = "abc='qwerty'";
+   long selection_string_length = 12;
+
+   long impo_options = MQIMPO_INQ_FIRST;
+   
+   MQIMPO impo = {MQIMPO_DEFAULT};
+   MQCNO cno = {MQCNO_DEFAULT};
+   MQCD client_conn = {MQCD_CLIENT_CONN_DEFAULT};
+   MQOD od1 = {MQOD_DEFAULT};
+   MQOD od2 = {MQOD_DEFAULT};
+   MQMD md = {MQMD_DEFAULT};
+   MQPMO pmo = {MQPMO_DEFAULT};
+   MQGMO gmo = {MQGMO_DEFAULT};
+   MQCMHO cmho = {MQCMHO_DEFAULT};
+   MQSMPO smpo = {MQSMPO_DEFAULT};
+   MQPD pd1 = {MQPD_DEFAULT};
+   MQPD pd2 = {MQPD_DEFAULT};
+   MQCHARV name = {MQCHARV_DEFAULT};
+
+   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);
+   
+   MQCRTMH(h_conn, &cmho, &msg_handle1, &comp_code, &reason);
+   printf("MQCRTMH ended with reason code %d\n", reason);
+   
+   name.VSPtr = property_name;
+   name.VSLength = property_name_length;
+   
+   MQSETMP(h_conn, msg_handle1, &smpo, &name, &pd1, property_type, property_value_length,
+            (MQBYTE *)property_value, &comp_code, &reason);
+   printf("MQSETMP ended with reason code %d\n", reason);
+   
+   pmo.Version = MQPMO_VERSION_3;
+   pmo.NewMsgHandle = msg_handle1;
+
+   od1.Version = MQOD_VERSION_4;
+   strncpy(od1.ObjectName, queue_name, MQ_Q_NAME_LENGTH);
+   
+   MQOPEN(h_conn, &od1, MQOO_OUTPUT | MQOO_FAIL_IF_QUIESCING, &h_obj, &comp_code, &reason);
+   printf("MQOPEN ended with reason code %d\n", reason);
+
+   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);
+   
+   od2.Version = MQOD_VERSION_4;
+   od2.SelectionString.VSPtr = selection_string;
+   od2.SelectionString.VSLength = selection_string_length;
+   strncpy(od2.ObjectName, queue_name, MQ_Q_NAME_LENGTH);
+
+   MQOPEN(h_conn, &od2, MQOO_INPUT_AS_Q_DEF | MQOO_FAIL_IF_QUIESCING, &h_obj2, &comp_code, &reason);
+   printf("MQOPEN ended with reason code %d\n", reason);
+   
+   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) {
+         printf("Message: <");
+         int x;
+         for(x=0; x<buflen; x++) {
+            printf("%c", buffer[x]);
+         }
+         printf(">\n");
+   }
+   
+   impo.Options = impo_options;
+   pd2.Options = pd_options;
+   
+   MQINQMP(h_conn, msg_handle1, &impo, &name, &pd2, &property_type, sizeof(value),
+    value, &actual_value_length, &comp_code, &reason);
+   printf("MQINQMP ended with reason code %d\n", reason);
+   if(comp_code == MQCC_OK) {
+         printf("Property value: <");
+         int x;
+         for(x=0; x<actual_value_length; x++) {
+            printf("%c", value[x]);
+         }
+         printf(">\n");
+   }
+   
+   name.VSPtr = "aaa";
+   name.VSLength = 3;
+   
+   MQINQMP(h_conn, msg_handle1, &impo, &name, &pd2, &property_type, sizeof(value),
+    value, &actual_value_length, &comp_code, &reason);
+   printf("MQINQMP ended with reason code %d\n", reason);
+
+   MQCLOSE(h_conn, &h_obj2, 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;
+}