weixin_38986584 2019-10-23 15:01 采纳率: 66.7%
浏览 438

如何向多个队列同时发送消息用的是ibm mq

package test;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

public class MessageByMQ {

/**
 * 队列管理器的名称
 */
private String qManagerName="QM_CTL";

/**
 * 队列管理器
 */
private   MQQueueManager qMgr;
/**
 * 队列名称
 */
private  String queueName= "Q.CTL.REQ";
\\private String q = "Q.SVC.TYPE.REQ";
\\private String s = "Q.SVC.TYPE.REQ2";
/**
 * 队列
 */
private MQQueue qQueue;

/**
 * mq服务器所在的主机名称
 */
private String hostname="127.0.0.1";
/**
 * 服务器连接通道名称
 */
private String channelName="SVRCONN_CTL";

/**
 * 监听器监听的端口
 */
private  int  port=1431;
/**
 * 传输的编码类型
 */
private  int CCSID = 1381; 
@Before
public  void  init(){
    try {
        MQEnvironment.hostname = this.hostname; // 安裝MQ所在的ip address
        MQEnvironment.port = this.port; // TCP/IP port
        MQEnvironment.channel = this.channelName;
        MQEnvironment.CCSID = CCSID;
        qMgr = new MQQueueManager(this.qManagerName);
        int qOptioin = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_INQUIRE
                | MQC.MQOO_OUTPUT;

        qQueue = qMgr.accessQueue(queueName, qOptioin);
      \\  qQueue = qMgr.accessQueue(s, qOptioin);
        \\qQueue = qMgr.accessQueue(q, qOptioin);    


    } catch (MQException e) {
        e.printStackTrace();
    }
}





/**
 * 发送信息
 */

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

}




/**
 * 从消息队列取数据
 */
public void GetMsg() {
    try {
        MQMessage retrievedMessage = new MQMessage();

        MQGetMessageOptions gmo = new MQGetMessageOptions();
        gmo.options += MQC.MQPMO_SYNCPOINT;
        qQueue.get(retrievedMessage, gmo);
        int length = retrievedMessage.getDataLength();

        byte[] msg = new byte[length];

        retrievedMessage.readFully(msg);

        String sMsg = new String(msg,"GBK");
        System.out.println(sMsg);

    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (MQException e) {
        e.printStackTrace();
        if (e.reasonCode != 2033) // 没有消息
        {
            e.printStackTrace();
            System.out
                    .println("A WebSphere MQ error occurred : Completion code "
                            + e.completionCode
                            + " Reason Code is "
                            + e.reasonCode);
        }
    } catch (java.io.IOException e) {
        System.out
                .println("An error occurred whilst to the message buffer "
                        + e);
    }
}





/**
 * 单元测试方法
 */

@Test
public  void  testMQ(){
    MessageByMQ mqst = new MessageByMQ();
    mqst.init();
    try {
        mqst.SendMsg("<Message><Head><ControlerName>wmb</ControlerName><FunctionName>ALL</FunctionName><ReturnCode></ReturnCode><ReturnInfo></ReturnInfo></Head><Body><TokenPoolID>ALL</TokenPoolID><ServiceID>ALL</ServiceID><SystemID>ALL</SystemID> </Body></Message>".getBytes("GBK"));
        mqst.GetMsg();
    } catch (Exception e) {
        e.printStackTrace();
    }
}


/**
 * 释放资源
 */
@After
public  void  release(){
    try {
        qQueue.close();
        qMgr.disconnect();
    } catch (MQException e) {
        System.out
                .println("A WebSphere MQ error occurred : Completion code "
                        + e.completionCode + " Reason Code is "
                        + e.reasonCode);
    }   
}

}

  • 写回答

1条回答 默认 最新

  • 明初啥都能学会 2024-04-16 20:01
    关注

    要向多个队列同时发送消息,您可以在 SendMsg 方法中添加额外的参数来指定要发送消息的队列名称。然后,您可以修改 init 方法,使其连接多个队列,而不仅仅是一个队列。

    以下是如何修改您的代码来实现这一点:

    1. 修改 init 方法:连接多个队列,并在 SendMsg 方法中根据需要选择要发送消息的队列。
    @Before
    public  void  init(){
        try {
            MQEnvironment.hostname = this.hostname;
            MQEnvironment.port = this.port;
            MQEnvironment.channel = this.channelName;
            MQEnvironment.CCSID = CCSID;
            qMgr = new MQQueueManager(this.qManagerName);
            
            // 连接第一个队列
            int qOption = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_INQUIRE | MQC.MQOO_OUTPUT;
            qQueue1 = qMgr.accessQueue(queueName1, qOption);
            
            // 连接第二个队列
            qQueue2 = qMgr.accessQueue(queueName2, qOption);
            
            // 连接更多的队列...
            
        } catch (MQException e) {
            e.printStackTrace();
        }
    }
    
    
    1. 修改 SendMsg 方法:添加一个参数来指定要发送消息的队列,并在方法中根据指定的队列发送消息。
    public void SendMsg(byte[] qByte, MQQueue qQueue) {
        try {
            MQMessage qMsg = new MQMessage();
            qMsg.write(qByte);
            MQPutMessageOptions pmo = new MQPutMessageOptions();
            qQueue.put(qMsg, pmo);
            System.out.println("The message is sent!");
            System.out.println("\tThe message is " + new String(qByte, "GBK"));
        } catch (MQException e) {
            e.printStackTrace();
            System.out.println("A WebSphere MQ error occurred : Completion code "
                    + e.completionCode + " Reason Code is " + e.reasonCode);
        } catch (java.io.IOException e) {
            e.printStackTrace();
            System.out.println("An error occurred whilst to the message buffer "
                    + e);
        }
    }
    
    1. 修改测试方法:在测试方法中调用 SendMsg 方法,并指定要发送消息的队列。
    @Test
    public void testMQ(){
        MessageByMQ mqst = new MessageByMQ();
        mqst.init();
        try {
            // 向第一个队列发送消息
            mqst.SendMsg("<Message><Head><ControlerName>wmb</ControlerName><FunctionName>ALL</FunctionName><ReturnCode></ReturnCode><ReturnInfo></ReturnInfo></Head><Body><TokenPoolID>ALL</TokenPoolID><ServiceID>ALL</ServiceID><SystemID>ALL</SystemID> </Body></Message>".getBytes("GBK"), mqst.qQueue1);
            
            // 向第二个队列发送消息
            mqst.SendMsg("<Message><Head><ControlerName>wmb</ControlerName><FunctionName>ALL</FunctionName><ReturnCode></ReturnCode><ReturnInfo></ReturnInfo></Head><Body><TokenPoolID>ALL</TokenPoolID><ServiceID>ALL</ServiceID><SystemID>ALL</SystemID> </Body></Message>".getBytes("GBK"), mqst.qQueue2);
            
            // 发送到更多的队列...
            
            mqst.GetMsg();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    通过这种方式,您可以在同一个测试方法中向多个队列发送消息。请确保根据您的需求适当地连接和发送到所需的队列。

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器