java 如何使用java读取MQ消息而不从队列中删除消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45345115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:38:54  来源:igfitidea点击:

how to read MQ messages using java without deleting message from the queue

javaibm-mq

提问by Samyak Parakh

When I execute my below code, it will read data from MQ on the console and then delete all the data from the queue. I don't want my data to be deleted from the queue while reading from MQ. I want to make it parameterize and also wants to write the data to excel. Can anybody please help me out from this. Below is my code that I am using.

当我执行下面的代码时,它将从控制台上的 MQ 读取数据,然后从队列中删除所有数据。我不希望我的数据在从 MQ 读取时从队列中删除。我想让它参数化,也想将数据写入excel。任何人都可以帮我解决这个问题。下面是我正在使用的代码。

public class MQReadJava
{
    private MQQueueManager _queueManager = null;
    public int port = 1416;
    public String hostname = "xyz";
    public String channel = "SYSTEM.ABC.123";
    public String qManager = "ABC.BAW";
    public String inputQName = "MYQUEUE";

    public MQReadJava()
    {
        super(); 
    }

    private void init(String[] args) throws IllegalArgumentException
    {
        // Set up MQ environment

        MQEnvironment.hostname = hostname;
        MQEnvironment.channel = channel;
        MQEnvironment.port = port;
    }

    public static void main(String[] args)throws IllegalArgumentException
    {
        MQReadJava readQ = new MQReadJava();
        try
        {
            readQ.init(args);
            readQ.selectQMgr();
            readQ.read();
        }

        catch (IllegalArgumentException e)
        {
            System.exit(1);
        }
        catch (MQException e)
        {
            System.out.println(e);
            System.exit(1);
        }
    }

    private void selectQMgr() throws MQException
    {
        _queueManager = new MQQueueManager(qManager);
    }

    private void read() throws MQException
    {
        int openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED;

        //int   openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING;

        MQQueue queue = _queueManager.accessQueue( inputQName,
        openOptions,
        null, // default q manager
        null, // no dynamic q name
        null ); // no alternate user id

        System.out.println("MQRead is now connected.\n");
        int depth = queue.getCurrentDepth();
        System.out.println("Current depth: " + depth + "\n");

        if (depth == 0)
        {
            return;
        }

        MQGetMessageOptions getOptions = new MQGetMessageOptions();
        getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING + 
        MQC.MQGMO_CONVERT;

        while(true)
        {
            MQMessage message = new MQMessage();
            try
            {
                queue.get(message, getOptions);
                byte[] b = new byte[message.getMessageLength()];
                message.readFully(b);
                System.out.println(new String(b));
                message.clearMessage();
            }
            catch (IOException e)
            {
                System.out.println("IOException during GET: " + e.getMessage());
                break;
            }
            catch (MQException e)
            {
                if (e.completionCode == 2 && e.reasonCode == MQException.MQRC_NO_MSG_AVAILABLE) {
                    if (depth > 0)
                    {
                        System.out.println("All messages read.");
                    }
                }
                else
                {
                    System.out.println("GET Exception: " + e);
                }
                break;
            }
        }
        queue.close();
        _queueManager.disconnect();
    }
}

回答by Roger

Below is my code that I am using.

下面是我正在使用的代码。

:) You downloaded my MQRead program. If you don't want it to do a destructive MQGET then you need to update the code to do a browse (see JoshMc's comments). Why didn't you download my MQBrowse program? Finally, please start reading the MQ Knowledge Centeras it contains lots & lots of information for beginners to IBM MQ.

:) 您下载了我的 MQRead 程序。如果您不希望它执行破坏性的 MQGET,那么您需要更新代码以进行浏览(请参阅 JoshMc 的评论)。你为什么不下载我的 MQBrowse 程序?最后,请开始阅读MQ 知识中心,因为它包含大量适用于 IBM MQ 初学者的信息。



Update: You should not use MQEnvironment class as it is not thread safe. Put the connection information in a Hashtable. See here for an example: Java program to connect WMQ with User Id instead of channel

更新:您不应该使用 MQEnvironment 类,因为它不是线程安全的。将连接信息放在一个哈希表中。请参见此处的示例: Java program to connect WMQ with User Id 而不是 channel

回答by JoshMc

In order to not delete messages from the queue you need to browse the queue, this would be accomplished by updating your openOptions and getOptions as follows:

为了不从需要浏览队列的队列中删除消息,这将通过更新 openOptions 和 getOptions 来完成,如下所示:

int openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING +  MQC.MQOO_BROWSE;

and

getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING + MQC.MQGMO_CONVERT + MQC.MQGMO_BROWSE_NEXT;


Checkout this great list of sample IBM MQ Classes for Java applications on Capitalware's website: Sample IBM MQ Java Code

在 Capitalware 的网站上查看用于 Java 应用程序的示例 IBM MQ 类的重要列表:示例 IBM MQ Java 代码