amqsget.c 从mq中读取消息

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cmqc.h>
#include "amqsget.h"
#include "commonfunc.h"

int mqsGet(unsigned char *sRecvBuffer)
{

MQOD    
od =
{MQOD_DEFAULT};

MQMD    
md =
{MQMD_DEFAULT};

MQGMO   gmo =
{MQGMO_DEFAULT};

MQHCONN 
Hcon;

MQHOBJ  
Hobj;

MQLONG  
O_options;

MQLONG  
C_options;

MQLONG  
CompCode;

MQLONG  
OpenCode;

MQLONG  
Reason;

MQLONG  
CReason;

MQBYTE  
buffer[65536];

MQLONG  
buflen;

MQLONG  
messlen;

char    
QMName[50];

int iret =
0;
 char
smqRecvQueNm[50];

char
smqSendQueNm[50];

char
smqQueMngNm[50];

char
smqQueOpenOptn[50];  
 char
smqQueClsOptn[50];

memset(smqRecvQueNm, 0,
sizeof(smqRecvQueNm));
 memset(smqSendQueNm, 0,
sizeof(smqSendQueNm));
 memset(smqQueMngNm, 0,
sizeof(smqQueMngNm));
 memset(smqQueOpenOptn, 0,
sizeof(smqQueOpenOptn));
 memset(smqQueClsOptn, 0,
sizeof(smqQueClsOptn));
 memset(sRecvBuffer, 0, sizeof(buffer));
 memset(buffer, 0, sizeof(buffer));

iret = ReadINIFile("MQMANAGELIST", "mqMngNm",
smqQueMngNm, "main.ini" );
 if ( iret != 1 ) { 
  WriteLog("SEND",
"配置文件中没有找到节点[mqMngNm]\n");
  return 1;
 }

iret = ReadINIFile("MQMANAGELIST",
"mqQueOpenOptn", smqQueOpenOptn, "main.ini" );
 if ( iret != 1 ) { 
  WriteLog("SEND",
"配置文件中没有找到节点[mqQueOpenOptn]\n");
  return 1;
 }

iret = ReadINIFile("MQMANAGELIST",
"mqQueClsOptn", smqQueClsOptn, "main.ini" );
 if ( iret != 1 ) { 
  WriteLog("SEND",
"配置文件中没有找到节点[mqQueClsOptn]\n");
  return 1;
 }

iret = ReadINIFile("MQQUEUELIST", "mqRecvQueNm",
smqRecvQueNm, "main.ini" );
 if ( iret != 1 ) { 
  WriteLog("SEND",
"配置文件中没有找到节点[mqRecvQueNm]\n");
  return 1;
 }

//WriteLog("SEND", "smqRecvQueNm=[%s],
smqQueMngNm=[%s], smqQueOpenOptn=[%s], smqQueClsOptn=[%s]\n\n",
smqRecvQueNm, smqQueMngNm, smqQueOpenOptn, smqQueClsOptn);
 //WriteLog("SEND", "Sample AMQSGET0
start\n");

QMName[0] =
0;  
 strncpy(od.ObjectName, smqRecvQueNm,
MQ_Q_NAME_LENGTH);
 strncpy(QMName, smqQueMngNm,
MQ_Q_MGR_NAME_LENGTH);

MQCONN(QMName,

&Hcon,

&CompCode,

&CReason);

if (CompCode == MQCC_FAILED)
 {
  WritePrintLog( "MQCONN ended
with reason code %d\n", CReason);
  //exit( (int)CReason );
  return 1;
 }

if (0 != strlen(smqQueOpenOptn))
 {
  O_options = atoi(
smqQueOpenOptn );
  //WriteLog("SEND",
"open  options are %d\n", O_options);
 }
 else
 {
  O_options =
MQOO_INPUT_AS_Q_DEF

| MQOO_FAIL_IF_QUIESCING
   
  ;                       
   
 }

MQOPEN(Hcon,

&od,                      
  
  O_options,                
 
  &Hobj,                    
 
  &OpenCode,                
 
  &Reason);

if (Reason != MQRC_NONE)
 {
  WritePrintLog( "MQOPEN ended
with reason code %d\n", Reason);
 }

if (OpenCode == MQCC_FAILED)
 {
  WritePrintLog( "unable to open
queue for input\n");
  return 1;
 }

CompCode =
OpenCode;

gmo.Options =
MQGMO_WAIT

|
MQGMO_NO_SYNCPOINT  
   
  |
MQGMO_CONVERT;      
   
 gmo.WaitInterval =
15000;

//while (CompCode != MQCC_FAILED)
 if (CompCode != MQCC_FAILED)
 {
  buflen = sizeof(buffer) -
1;

memcpy(md.MsgId, MQMI_NONE,
sizeof(md.MsgId));
  memcpy(md.CorrelId, MQCI_NONE,
sizeof(md.CorrelId));

md.Encoding      
= MQENC_NATIVE;
  md.CodedCharSetId =
MQCCSI_Q_MGR;

MQGET(Hcon,

Hobj,

&md,

&gmo,

buflen,

buffer,

&messlen,

&CompCode,

&Reason);

if (Reason != MQRC_NONE)
  {
   if (Reason ==
MQRC_NO_MSG_AVAILABLE)
   {

WritePrintLog(
"no more messages\n");
   }
   else
   {
    WritePrintLog(
"MQGET ended with reason code %d\n", Reason);

if
(Reason == MQRC_TRUNCATED_MSG_FAILED)
    {

CompCode
= MQCC_FAILED;
    }

}
  }

if (CompCode !=
MQCC_FAILED)
  {
   buffer[messlen]
=
'\0';

WritePrintLog(
"message <%s>\n", buffer);
   strcpy(sRecvBuffer,
buffer);
  }

}

if (OpenCode != MQCC_FAILED)
 {
  if (0 !=
strlen(smqQueClsOptn))
  {
   C_options =
atoi( smqQueClsOptn );
   WritePrintLog(
"close options are %d\n", C_options);
  }
  else
  {
   C_options =
MQCO_NONE;

}

MQCLOSE(Hcon,

&Hobj,

C_options,

&CompCode,

&Reason);

if (Reason != MQRC_NONE)
  {
   WritePrintLog(
"MQCLOSE ended with reason code %d\n", Reason);
  }
 }

if (CReason != MQRC_ALREADY_CONNECTED )
 {
  MQDISC(&Hcon,

&CompCode,

&Reason);

if (Reason != MQRC_NONE)
  {
   WritePrintLog(
"MQDISC ended with reason code %d\n", Reason);
  }
 }

if(0 == strlen(buffer)) {
  //WritePrintLog("接收到的报文为空\n");

return 1;
 }
 else {
  WritePrintLog("buffer=[%s]\n",
buffer);
  strcpy(sRecvBuffer,
buffer);
 }

//WriteLog("SEND", "Sample AMQSGET0
end\n");
 return(0);
}

amqsput.c 向mq中发送消息

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cmqc.h>
#include "amqsput.h"
#include "commonfunc.h"
#include "codeConv.h"

int mqsPut(char * sOutBuffer)

{
 
 FILE *fp;

MQOD    
od =
{MQOD_DEFAULT};

MQMD    
md =
{MQMD_DEFAULT};

MQPMO   pmo =
{MQPMO_DEFAULT};

MQHCONN 
Hcon;

MQHOBJ  
Hobj;

MQLONG  
O_options;

MQLONG  
C_options;

MQLONG  
CompCode;

MQLONG  
OpenCode;

MQLONG  
Reason;

MQLONG  
CReason;

MQLONG  
messlen;

char    
buffer[65535];

char    
QMName[50];

WritePrintLog("Sample AMQSPUT0
start\n");

int iret = 0;
 char sQMName[50];
 char sQInName[50];
 char sQOutName[50];
 char smqQueOpenOptn[50];
 char smqQueClsOptn[50];
 char sSendDynamicQueNm[50];

memset(sQMName, 0, sizeof(sQMName));
 memset(sQInName, 0, sizeof(sQInName));
 memset(sQOutName, 0, sizeof(sQOutName));
 memset(smqQueOpenOptn, 0,
sizeof(smqQueOpenOptn));
 memset(smqQueClsOptn, 0,
sizeof(smqQueClsOptn));
 memset(sSendDynamicQueNm, 0,
sizeof(sSendDynamicQueNm));

iret = ReadINIFile("MQMANAGELIST", "mqMngNm",
sQMName, "main.ini" );
 if ( iret != 1 ) { 
  WritePrintLog(
"配置文件中没有找到节点[mqMngNm]\n");
  return 1;
 }

iret = ReadINIFile("MQQUEUELIST", "mqRecvQueNm",
sQInName, "main.ini" );
 if ( iret != 1 ) { 
  WritePrintLog(
"配置文件中没有找到节点[mqRecvQueNm]\n");
  return 1;
 }

iret = ReadINIFile("MQQUEUELIST", "mqSendQueNm",
sQOutName, "main.ini" );
 if ( iret != 1 ) { 
  WritePrintLog(
"配置文件中没有找到节点[mqSendQueNm]\n");
  return 1;
 }
 
 iret = ReadINIFile("MQQUEUELIST",
"mqSendDynamicQueNm", sSendDynamicQueNm, "main.ini" );
 if ( iret != 1 ) { 
  WritePrintLog(
"配置文件中没有找到节点[mqSendDynamicQueNm]\n");
  return 1;
 }

iret = ReadINIFile("MQMANAGELIST",
"mqQueOpenOptn", smqQueOpenOptn, "main.ini" );
 if ( iret != 1 ) { 
  WriteLog("SEND",
"配置文件中没有找到节点[mqQueOpenOptn]\n");
  return 1;
 }

iret = ReadINIFile("MQMANAGELIST",
"mqQueClsOptn", smqQueClsOptn, "main.ini" );
 if ( iret != 1 ) { 
  WriteLog("SEND",
"配置文件中没有找到节点[mqQueClsOptn]\n");
  return 1;
 }

WritePrintLog( "sQMName=[%s],
sQOutName=[%s],sSendDynamicQueNm=[%s],sQInName=[%s]\n\n", sQMName,
sQOutName,sSendDynamicQueNm, sQInName);

QMName[0] =
0;   
 strncpy(QMName, sQMName,
(size_t)MQ_Q_MGR_NAME_LENGTH);
 MQCONN(QMName,

&Hcon,

&CompCode,

&CReason);

if (CompCode == MQCC_FAILED)
 {
  WritePrintLog("MQCONN ended
with reason code %d\n", CReason);
  exit( (int)CReason );
 }

strncpy(od.ObjectName, sQOutName,
(size_t)MQ_Q_NAME_LENGTH);
 WritePrintLog("target queue is %s\n",
od.ObjectName);
 
 //if (argc > 5)
 {
 strncpy(od.ObjectQMgrName, QMName, (size_t)
MQ_Q_MGR_NAME_LENGTH);
 WritePrintLog("target queue manager is %s\n",
od.ObjectQMgrName);
 }

//if (argc > 6)
 {
 strncpy(od.DynamicQName, sSendDynamicQueNm,
(size_t) MQ_Q_NAME_LENGTH);
 WritePrintLog("dynamic queue name is %s\n",
od.DynamicQName);
 }

if (0 != strlen(smqQueOpenOptn))
 {
  O_options = atoi(
smqQueOpenOptn );
  WritePrintLog("open 
options are %d\n", O_options);
 }
 else
 {
  O_options =
MQOO_OUTPUT

|
MQOO_FAIL_IF_QUIESCING   
   ;                       
  
 }

MQOPEN(Hcon,

&od,

O_options,

&Hobj,

&OpenCode,

&Reason);

if (Reason != MQRC_NONE)
 {
  WritePrintLog("MQOPEN ended
with reason code %d\n", Reason);
 }

if (OpenCode == MQCC_FAILED)
 {
  WritePrintLog("unable to open
queue for output\n");
 }

CompCode =
OpenCode;

fp = stdin;
 memcpy(md.Format,

MQFMT_STRING,
(size_t)MQ_FORMAT_LENGTH);

pmo.Options = MQPMO_NO_SYNCPOINT
  | MQPMO_FAIL_IF_QUIESCING;

if (CompCode != MQCC_FAILED)
 {
  //if (fgets(buffer,
sizeof(buffer), fp) != NULL)
  //{
  // messlen =
(MQLONG)strlen(buffer);
  //if (buffer[messlen-1] ==
'\n') 
  //{
  //
buffer[messlen-1]  =
'\0';   
  // 
--messlen;

//}
  // }
  //else messlen =
0;

strcpy(buffer,
sOutBuffer);
  messlen = strlen(buffer);

char *szOut;
  char sTbuffer[65535];

memset(sTbuffer, 0,
sizeof(sTbuffer));
  szOut = u2g(buffer);
  strcpy(sTbuffer, szOut);
  free(szOut);

if (messlen >
0)
  {
  
   WritePrintLog("MQPUT===============buffer[%s][%d]\n",
sTbuffer, strlen(sTbuffer));
   
   
   
   
   memcpy(md.MsgId,

MQMI_NONE,
sizeof(md.MsgId) );

MQPUT(Hcon,

Hobj,

&md,

&pmo,

messlen,

buffer,

&CompCode,

&Reason);

if (Reason !=
MQRC_NONE)
   {
    WritePrintLog("MQPUT
ended with reason code %d\n", Reason);
   }
  }
  else

CompCode =
MQCC_FAILED;
 }

if (OpenCode != MQCC_FAILED)
 {
  if (0 !=
strlen(smqQueClsOptn))
  {
   C_options =
atoi( smqQueClsOptn );
   WritePrintLog("close
options are %d\n", C_options);
  }
  else
  {
   C_options =
MQCO_NONE;

}

MQCLOSE(Hcon,

&Hobj,

C_options,

&CompCode,

&Reason);

if (Reason != MQRC_NONE)
  {
   WritePrintLog("MQCLOSE
ended with reason code %d\n", Reason);
  }
 }

if (CReason != MQRC_ALREADY_CONNECTED)
 {
  MQDISC(&Hcon,

&CompCode,

&Reason);

if (Reason != MQRC_NONE)
  {
   WritePrintLog("MQDISC
ended with reason code %d\n", Reason);
  }
 }

WritePrintLog("Sample AMQSPUT0 end\n");
 return(0);
 }

C语言实现mq收发数据的函数的更多相关文章

  1. dsp28377控制DM9000收发数据——第三版程序,通过外部引脚触发来实现中断接受数据,优化掉帧现象

    //-------------------------------------------------------------------------------------------- - //D ...

  2. dsp28377控制DM9000收发数据

    首先感谢上一篇转载文章的作者给出的参考,下面是一些自己在调试过程中的一些步骤: 首先把代码贴上来: //------------------------------------------------ ...

  3. 【C语言入门教程】5.2 函数的作用域规则(auto, static)

    作用域规则是指代码或数据的有效使用范围.C语言将函数作为独立的代码块,函数之间不能相互访问其内部的代码或数据.函数间数据的传递只能通过接口实现.但是,变量的定义方法可改变函数的作用域规则,可将变量分为 ...

  4. iOS开发之音频口通信-通过方波来收发数据

    之前做过的项目有需要通过音频口通信用方波来收发数据,由于这方面的资料比较少,下面就介绍下其原理,希望能给大家帮助. 一. 音频通信简介大家应该都知道支付宝声波支付和拉卡拉吧,它们都是利用手机的音频口( ...

  5. 【AT91SAM3S】串口UART初始化及收发数据

    SAM3S中的UART串口是一个两线异步收发器.这个串口能用来通信或者跟踪.有两个DMA通道与UART串口关联,可通过使用DMA处理串口传输以节省CPU时间. SAM3S4C中有两个UART.与外设引 ...

  6. R语言中的横向数据合并merge及纵向数据合并rbind的使用

    R语言中的横向数据合并merge及纵向数据合并rbind的使用 我们经常会遇到两个数据框拥有相同的时间或观测值,但这些列却不尽相同.处理的办法就是使用merge(x, y ,by.x = ,by.y ...

  7. R语言系列:生成数据

    R语言系列:生成数据 (2014-05-04 17:41:57) 转载▼ 标签: r语言 教育 分类: 生物信息 生成规则数据1.使用“:“,如x=1:10,注意该方法既可以递增也可以递减,如y=10 ...

  8. 孤荷凌寒自学python第四十八天通用同一数据库中复制数据表函数最终完成

    孤荷凌寒自学python第四十八天通用同一数据库中复制数据表函数最终完成 (完整学习过程屏幕记录视频地址在文末) 今天继续建构自感觉用起来顺手些的自定义模块和类的代码. 今天经过反复折腾,最终基本上算 ...

  9. 孤荷凌寒自学python第四十七天通用跨数据库同一数据库中复制数据表函数

    孤荷凌寒自学python第四十七天通用跨数据库同一数据库中复制数据表函数 (完整学习过程屏幕记录视频地址在文末) 今天继续建构自感觉用起来顺手些的自定义模块和类的代码. 今天打算完成的是通用的(至少目 ...

随机推荐

  1. Knockout.Js官网学习(selectedOptions绑定、uniqueName 绑定)

    selectedOptions绑定 selectedOptions绑定用于控制multi-select列表已经被选择的元素,用在使用options绑定的<select>元素上. 当用户在m ...

  2. 架构设计:系统存储(24)——数据一致性与Paxos算法(中)

    (接上文<架构设计:系统存储(23)--数据一致性与Paxos算法(上)>) 2-1-1. Prapare准备阶段 首先须要介绍几个在Acceptor角色上须要被持久化保存的数据属性: P ...

  3. ubuntu 12.04启用休眠

    x86-64 与EM64区别 EM64T全称是Extended Memory 64 Technology(64位内存技术扩展技术.Intel声称“EM64T技术是Intel对IA32平台一系列技术革新 ...

  4. wkhtmlpdf安装以及中文乱码

    见此页http://www.cnblogs.com/day959/p/6652726.html

  5. CFX构建webservice实例,与Spring整合.

    项目结构图: 步骤一: 添加maven包依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&qu ...

  6. 检查死锁与Locked ownable synchronizers(转载)

    通过jstack可快速检查线程的死锁信息,用法如下: # 获取JVM ID(JAVA 进程ID),通过参数lv可以获取更详细的JAVA方法调用信息 jps -lv # 得到JVM ID后,执行jsta ...

  7. [转载]js正则表达式/replace替换变量方法

    原文地址:http://www.blogjava.net/pingpang/archive/2012/08/12/385342.html JavaScript正则实战(会根据最近写的不断更新) 1.j ...

  8. Nginx负载均衡权重,ip_hash

    nginx为后端web服务器(apache,nginx,tomcat,weblogic)等做反向代理 几台后端web服务器需要考虑文件共享,数据库共享,session共享问题.文件共享可以使用nfs, ...

  9. Android Error:Execution failed for task ':app:compileDebugJavaWithJavac' 解决方案

    今天使用 Android Studio 构建项目的时候出现了这个错误 compileDebugJavaWithJavac 通过搜索发现造成该问题的原因有很多需要结合具体的项目进行排查 通过 Andro ...

  10. Atitit 视图参数解决方案 oracle版和mysql版本 attilax总结.docx

    Atitit 视图参数解决方案 oracle版和mysql版本 attilax总结.docx 1.1. Package机制1 1.2. 全局变量机制1 1.3. 临时表模式,oracle mysql都 ...