2

我正在尝试从 Java 建立与 MQ 的连接。我正在使用来自 IBM 的以下示例代码,但它可能已过时。我该如何解决这个问题/我错过了什么?

import com.ibm.mq.*;            // Include the WebSphere MQ classes for Java package
import com.ibm.mq.jmqi.*;
import com.ibm.mq.constants.MQConstants;

public class MQSample
{
    private String qManager = "your_Q_manager";  // define name of queue
    // manager to connect to.
    private MQQueueManager qMgr;                 // define a queue manager
    // object
    public static void main(String args[]) {
        new MQSample();
    }

    public MQSample() {
        try {

            // Create a connection to the queue manager

            qMgr = new MQQueueManager(qManager);

            // Set up the options on the queue we wish to open...
            // Note. All WebSphere MQ Options are prefixed with MQC in Java.

            int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF |
                    MQConstants.MQOO_OUTPUT ;

            // Now specify the queue that we wish to open,
            // and the open options...

            MQQueue system_default_local_queue =
                    qMgr.accessQueue("SYSTEM.DEFAULT.LOCAL.QUEUE",
                            openOptions);

            // Define a simple WebSphere MQ message, and write some text in UTF format..

            MQMessage hello_world = new MQMessage();
            hello_world.writeUTF("Hello World!");

            // specify the message options...

            MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the // defaults,
            // same as MQPMO_DEFAULT

            // put the message on the queue

            system_default_local_queue.put(hello_world,pmo);

            // get the message back again...
            // First define a WebSphere MQ message buffer to receive the message into..

            MQMessage retrievedMessage = new MQMessage();
            retrievedMessage.messageId = hello_world.messageId;

            // Set the get message options...

            MQGetMessageOptions gmo = new MQGetMessageOptions(); // accept the defaults
            // same as  MQGMO_DEFAULT
            // get the message off the queue...

            system_default_local_queue.get(retrievedMessage, gmo);

            // And prove we have the message by displaying the UTF message text

            String msgText = retrievedMessage.readUTF();
            System.out.println("The message is: " + msgText);
            // Close the queue...
            system_default_local_queue.close();
            // Disconnect from the queue manager

            qMgr.disconnect();
        }
        // If an error has occurred in the above, try to identify what went wrong
        // Was it a WebSphere MQ error?
        catch (MQException ex)
        {
            System.out.println("A WebSphere MQ error occurred : Completion code " +
                    ex.completionCode + " Reason code " + ex.reasonCode);
        }
        // Was it a Java buffer space error?
        catch (java.io.IOException ex)
        {
            System.out.println("An error occurred whilst writing to the message buffer: " + ex);
        }
    }
} // end of sample

我得到的错误:

"C:\Program Files\Java\jdk1.7.0_09\bin\java" -Didea.launcher.port=7536 "-Didea.launcher.bin.path=M:\IntelliJ IDEA 12.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_09\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\access-bridge.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext\zipfs.jar;M:\untitled\out\production\untitled;D:\MavenRepository\com\ibm\mq\mq\7.0.1.1\mq-7.0.1.1.jar;D:\MavenRepository\com\ibm\mq\jmqi\7.0.1.1\jmqi-7.0.1.1.jar;M:\Monitor_NEW\Engine\lib\ojdbc14_g-10.2.0.jar;D:\MavenRepository\com\ibm\mq\mqjms\7.0.1.1\mqjms-7.0.1.1.jar;M:\IntelliJ IDEA 12.1.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain MQSample
Exception in thread "main" java.lang.NoClassDefFoundError: javax/resource/ResourceException
    at com.ibm.mq.MQQueueManager.<clinit>(MQQueueManager.java:153)
    at MQSample.<init>(MQSample.java:28)
    at MQSample.main(MQSample.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException: javax.resource.ResourceException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 8 more
4

2 回答 2

6

将 connector-1.0.jar 依赖项添加到您的项目中。

于 2013-12-23T13:44:57.137 回答
0

根据IBM的说法,NoClassDefFoundError 异常可能是由于缺少 jar:“连接器无法从 IBM WebSphere MQ Java 客户端库中找到文件 jms.jar。确保 start_connector.bat 中的变量 MQSERIES_JAVA_LIB 指向 IBM WebSphere MQ Java 客户端库文件夹。”

于 2013-07-16T05:56:17.787 回答