IBM MQ 使用一例

MQ作为一种中间件还是不错的,可以由一个系统发送一些消息给MQ Server,然后另外一个系统来取得这些消息。比较方便的实现不同系统、不同语言间的通信。

MQ Server可以进行消息的排队、缓存等。

具体MQ Server的配置我就不介绍了,可以去IBM的网站下载一个试用版试试看。

下面是一个收发的简单的小例子,具体意思看里面的注释。

None.gif import  com.ibm.mq.MQC;
None.gif
import  com.ibm.mq.MQEnvironment;
None.gif
import  com.ibm.mq.MQException;
None.gif
import  com.ibm.mq.MQGetMessageOptions;
None.gif
import  com.ibm.mq.MQMessage;
None.gif
import  com.ibm.mq.MQPutMessageOptions;
None.gif
import  com.ibm.mq.MQQueue;
None.gif
import  com.ibm.mq.MQQueueManager;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif * 可以在MQ的资源管理器的某一个队列上放入测试消息、浏览消息等
InBlock.gif * 可以放入多条消息,按先进先出的方式取得
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  MQTest  dot.gif {
InBlock.gif    
InBlock.gif    
private String qManager;// QueueManager名
InBlock.gif

InBlock.gif    
private MQQueueManager qMgr;
InBlock.gif
InBlock.gif    
private MQQueue qQueue;
InBlock.gif    
InBlock.gif    String HOST_NAME;
InBlock.gif    
InBlock.gif    
int PORT=0;
InBlock.gif
InBlock.gif    String Q_NAME;
InBlock.gif    
InBlock.gif    String CHANNEL;
InBlock.gif    
InBlock.gif    
int CCSID;
InBlock.gif    
InBlock.gif    String Msg;
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void init()dot.gif{
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            HOST_NAME
="192.168.88.169";
InBlock.gif            PORT
=1414;
InBlock.gif            qManager
="QM_yfgaochun";
InBlock.gif            Q_NAME
="QM_NAME1";
InBlock.gif            CHANNEL
="S_yfgaochun";
InBlock.gif            CCSID
=1381//表示是简体中文, CCSID的值在AIX上一般设为1383,如果要支持GBK则设为1386,在WIN上设为1381。
InBlock.gif
            
InBlock.gif            Msg 
= "MQ测试发送Gao";
InBlock.gif            
//System.out.println(Msg);
InBlock.gif
            
InBlock.gif            MQEnvironment.hostname 
= HOST_NAME; //安裝MQ所在的ip address
InBlock.gif
            MQEnvironment.port=PORT; // TCP/IP port
InBlock.gif
            
InBlock.gif            MQEnvironment.channel
=CHANNEL;
InBlock.gif            MQEnvironment.CCSID
=CCSID;
InBlock.gif            
InBlock.gif            qMgr 
= new MQQueueManager(qManager);
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*            try {
InBlock.gif                XADataSource ds = null;
InBlock.gif                Connection con = (Connection)qMgr.getJDBCConnection(ds);
InBlock.gif            } catch (SQLException e) {
InBlock.gif                e.printStackTrace();
InBlock.gif            } catch (Exception e) {
InBlock.gif                e.printStackTrace();
ExpandedSubBlockEnd.gif            }
*/

InBlock.gif            
InBlock.gif            
//int qOptioin = MQC.MQOO_INQUIRE | MQC.MQOO_OUTPUT; 发送时使用
InBlock.gif            
//int qOptioin = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; 接收时使用
InBlock.gif
            int qOptioin = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_INQUIRE | MQC.MQOO_OUTPUT;
InBlock.gif
InBlock.gif            qQueue 
= qMgr.accessQueue(Q_NAME, qOptioin);
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (MQException e) dot.gif{
InBlock.gif            System.out
InBlock.gif                    .println(
"A WebSphere MQ error occurred : Completion code "
InBlock.gif                            
+ e.completionCode + " Reason Code is "
InBlock.gif                            
+ e.reasonCode);
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
void finalizer() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            qQueue.close();
InBlock.gif            qMgr.disconnect();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (MQException e) dot.gif{
InBlock.gif            System.out
InBlock.gif                    .println(
"A WebSphere MQ error occurred : Completion code "
InBlock.gif                            
+ e.completionCode + " Reason Code is "
InBlock.gif                            
+ e.reasonCode);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
InBlock.gif     * 取过一次,下次就没有了
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void GetMsg() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            MQMessage retrievedMessage 
= new MQMessage();
InBlock.gif
InBlock.gif            MQGetMessageOptions gmo 
= new MQGetMessageOptions();
InBlock.gif            gmo.options 
+= MQC.MQPMO_SYNCPOINT;
InBlock.gif
InBlock.gif            qQueue.get(retrievedMessage, gmo);
InBlock.gif
InBlock.gif            
int length = retrievedMessage.getDataLength();
InBlock.gif
InBlock.gif            
byte[] msg = new byte[length];
InBlock.gif
InBlock.gif            retrievedMessage.readFully(msg);
InBlock.gif
InBlock.gif            String sMsg 
= new String(msg);
InBlock.gif            System.out.println(sMsg);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch (RuntimeException e)dot.gif
InBlock.gif            e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch (MQException e) dot.gif{
InBlock.gif            
if (e.reasonCode != 2033//没有消息
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                e.printStackTrace();
InBlock.gif                System.out
InBlock.gif                        .println(
"A WebSphere MQ error occurred : Completion code "
InBlock.gif                                
+ e.completionCode
InBlock.gif                                
+ " Reason Code is "
InBlock.gif                                
+ e.reasonCode);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (java.io.IOException e) dot.gif{
InBlock.gif            System.out
InBlock.gif                    .println(
"An error occurred whilst to the message buffer "
InBlock.gif                            
+ e);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void SendMsg(byte[] qByte) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            MQMessage qMsg 
= new MQMessage();
InBlock.gif            qMsg.write(qByte);
InBlock.gif            MQPutMessageOptions pmo 
= new MQPutMessageOptions();
InBlock.gif            
InBlock.gif            qQueue.put(qMsg, pmo);
InBlock.gif
InBlock.gif            System.out.println(
"The message is sent!");
InBlock.gif            System.out.println(
"\tThe message is " + new String(qByte,"GBK"));
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (MQException e) dot.gif{
InBlock.gif            System.out
InBlock.gif                    .println(
"A WebSphere MQ error occurred : Completion code "
InBlock.gif                            
+ e.completionCode + " Reason Code is "
InBlock.gif                            
+ e.reasonCode);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (java.io.IOException e) dot.gif{
InBlock.gif            System.out
InBlock.gif                    .println(
"An error occurred whilst to the message buffer "
InBlock.gif                            
+ e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 
@param args
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        
// TODO Auto-generated method stub
InBlock.gif
        MQTest mqst = new MQTest();        
InBlock.gif        mqst.init();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            mqst.SendMsg(mqst.Msg.getBytes(
"GBK"));
InBlock.gif            
//mqst.GetMsg();
ExpandedSubBlockStart.gifContractedSubBlock.gif
        }
 catch (Exception e) dot.gif{
InBlock.gif            e.printStackTrace();
ExpandedSubBlockEnd.gif        }

InBlock.gif        mqst.finalizer();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值