【问题标题】:How to read a message from MQ using Java如何使用 Java 从 MQ 读取消息
【发布时间】:2014-11-07 16:06:43
【问题描述】:

我是 Java 新手,需要一些帮助。 我希望从 MQ 队列中读取消息,然后将消息输出到 txt 文件,最后从队列中删除原始消息。 我想出了以下内容,但它不会编译。 任何帮助将不胜感激! 谢谢!

public class test
{
public static void main(String[] args);
{
MQQueueManager QMgr=new MQQueueManager(qManager);                   
int openOptions=MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_INPUT_SHARED | MQC.MQOO_BROWSE;
MQQueue queue=QMgr.accessQueue(queueName, openOptions);         
MQMessage theMessage=new MQMessage();
MQGetMessageOptions gmo=new MQGetMessageOptions();  

gmo.options=MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_FIRST;
gmo.matchOptions=MQC.MQMO_NONE;
gmo.waitInterval=5000;

boolean thereAreMessages=true;

while(thereAreMessages){             **(Gives an "Illegal start of type" error)**
try{
    queue.get(theMessage,gmo);
    String msgText = theMessage.readString(theMessage.getMessageLength());
PrintStream out = new PrintStream(new         FileOutputStream("C:\\Users\\js04279\\Desktop\\test.txt")):
System.setOut(out);

System.out.println("REMOVING.............................");
getMessageOptions.options =MQC.MQGMO_MSG_UNDER_CURSOR;
}

catch(MQException e){
    if(e.reasonCode == e.MQRC_NO_MSG_AVAILABLE) 
        System.out.println("No more message available or retrieved");
        thereAreMessages=false;
    } 

catch (IOException e){
    System.out.println("ERROR: "+e.getMessage());
    }
}

I got the following error messages:
"test.java:17: illegal start of type"      - this was at the while statement
"test.java:35: <identifier> expected"

【问题讨论】:

  • 您是否收到错误消息?
  • 是的,我收到一条错误消息:While 语句的“test.java:17: 类型非法开始”和另一个错误“test.java:35: expected”跨度>
  • 存在一些语法错误。除非您喜欢手动编译 java 源代码并陷入解释编译器错误的困境,否则我建议您使用一个 IDE,它可以为您完成所有这些工作以及更多工作。 Eclipse is hugely popular, highly extendable, and free.
  • 你能告诉我jar文件名,我可以在哪里下载。

标签: java compilation queue mq


【解决方案1】:

Public static void main(String args[]);后去掉分号

正确的说法是

public static void main(String[] args) {
   //TODO your code
}

【讨论】:

    【解决方案2】:

    如果这是您在课堂上的全部内容,那么我认为您缺少 2 个右括号。 1 表示主要方法,1 表示类。

    我看到你的最后一个右括号是用于 while 循环的。

    Java 会将其视为类的右大括号,并将其解释为您在类级别编写语句。这是不允许的,因此被视为非法类型的开始。

    【讨论】:

      【解决方案3】:

      请使用 Eclipse 或 Net Beans 等 IDE 以避免编译时错误。使用 IDE 将在许多其他方面为您提供帮助。

      您的代码对我来说似乎很好。确实没有必要从队列中删除消息。一旦从队列中读取消息,它应该会自动删除。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-28
        • 1970-01-01
        • 1970-01-01
        • 2016-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多