关于MessageQ的编程资料网上比较少,bea提供的文档里有比较详细的说明。多读几次MessageQ的文档就能写出程序来。下面是几个MessageQ的操作函数:
#include
<
stdio.h
>
#include
<
stdlib.h
>
#include
<
string
.h
>
#include
"
p_entry.h
"
#include
"
p_return.h
"
#include
"
p_symbol.h
"
#include
<
unistd.h
>
#include
<
sys
/
types.h
>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*attach到消息队列里去,qid为消息队列的id*/
int
qattach(
short
qid)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
q_address q_addr;
int32 attach_mode;
int32 dmq_status;
int32 q_num_len;
int32 q_type;
char q_num_str[4];
int rc;
attach_mode = PSYM_ATTACH_BY_NUMBER;
q_type = PSYM_ATTACH_PQ;
memset(q_num_str, 0x00, 4);
(void)sprintf( q_num_str, "%d", qid );
q_num_len = (int32) strlen( q_num_str );
dmq_status = pams_attach_q( &attach_mode, &q_addr, &q_type,
q_num_str, &q_num_len,
(int32 *) 0,
(int32 *) 0,
(int32 *) 0,
(char *) 0,
(char *) 0);
if(dmq_status==PAMS__SUCCESS)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return(0);
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return ( dmq_status );
}
}
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*从消息队列里detach*/
int32 qdetach( )
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int32 dmq_status;
dmq_status = pams_exit( );
if(dmq_status==PAMS__SUCCESS)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return(0);
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return ( dmq_status );
}
}
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*从消息队列读取消息,msg_area为消息内容,msg_size为读取长度,qid为队列编号*/
int32 qread(
char
*
msg_area,
short
*
msg_size,
short
*
qid)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
char priority;
q_address msg_source;
short msg_class;
short msg_type;
short msg_area_len;
short msg_data_len;
int32 timeout;
int32 dmq_status;
char *qgroup, *getenv( );
int i;
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
priority = 0; /**//* get all messages */
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
msg_area_len = *msg_size; /**//* size of the message buffer */
timeout = 0;
dmq_status = pams_get_msgw(
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
msg_area, /**//* where msg is written */
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
&priority, /**//* get all messages */
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
&msg_source, /**//* q_address of sender */
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
&msg_class, /**//* class of received msg */
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
&msg_type, /**//* type of received msg */
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
&msg_area_len, /**//* size of message buffer */
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
msg_size, /**//* actual size of the msg */
&timeout,
(int32 *) 0,
(struct PSB *) 0,
(struct show_buffer *) 0,
(int32 *) 0,
(int32 *) 0,
(int32 *) 0,
(char *) 0 );
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ( dmq_status == PAMS__SUCCESS )
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return( 0 );
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return ( dmq_status );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*往消息队列里发送消息,msg_area为消息内容,msg_size为消息长度,qdest为消息队列编号*/
int32 qwrite(
char
*
msg_area,
short
msg_size,
short
qdest)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
char priority;
char delivery;
char uma;
short msg_class;
short msg_type;
int32 dmq_status;
int32 timeout;
struct PSB put_psb;
char *qgroup, *getenv( );
q_address qdest_addr;
int i;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
priority = 0; /**//* No priority */
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
msg_class = msg_type = 0; /**//* No type or class at the moment */
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
delivery = PDEL_MODE_NN_MEM; /**//* No Notification and nonrecoverable */
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
timeout = 100; /**//* Wait 10 seconds before giving up */
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
uma = PDEL_UMA_DISCL; /**//* If can't deliver it, DISCard and Log */
qgroup=getenv("DMQ_GROUP_ID");
qdest_addr.au.queue=(int16)qdest;
qdest_addr.au.group=(int16)atoi(qgroup);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
dmq_status = pams_put_msg(
msg_area,
&priority,
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
&qdest_addr, /**//* passed in */
&msg_class,
&msg_type,
&delivery,
&msg_size,
&timeout,
&put_psb,
&uma,
(q_address *) 0,
(int32 *) 0,
(char *) 0,
(char *) 0 );
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ( dmq_status == PAMS__SUCCESS )
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return( 0 );
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return ( dmq_status );
}
}
编译:
[root@localhost src]# cc -o ../bin/test -I $BEADIR/include test.c -L $BEADIR/lib -ldmq
在同一个消息队列中,你也可以根据程序的pid来发送和获得只属于本应用程序的消息。