Java 客户端监听 WebSphere MQ 服务器?

标签 java jms ibm-mq

我需要编写一个监听 WebSphere MQ Server 的 Java 客户端。消息被放入服务器的队列中。

我开发了这段代码,但不确定它是否正确。如果正确,那我该如何测试呢?

这是一个独立的 Java 项目,不支持应用程序服务器。我应该将哪些 jar 放入类路径?

我有 MQ 设置,我应该把代码放在哪里?标准 JMS 可以跳过这些设置吗?令人困惑....

import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Main {
    Context jndiContext = null;
    QueueConnectionFactory queueConnectionFactory = null;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    Queue controlQueue = null;
    QueueReceiver queueReceiver = null;

    private String queueSubject = "";

    private void start() {
        try {
            queueConnection.start();

            queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = queueSession.createQueue(queueSubject);
            MessageConsumer consumer = queueSession.createConsumer(destination);
            consumer.setMessageListener(new MyListener());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void close() {
        try {
            queueSession.close();
            queueConnection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() {
        try {
            jndiContext = new InitialContext();
            queueConnectionFactory = (QueueConnectionFactory) this.jndiLookup("QueueConnectionFactory");
            queueConnection = queueConnectionFactory.createQueueConnection();
            queueConnection.start();
        } catch (Exception e) {
            System.err.println("Could not create JNDI API " + "context: " + e.toString());
            System.exit(1);
        }
    }

    private class MyListener implements MessageListener {
        @Override
        public void onMessage(Message message) {
            System.out.println("get message:" + message);
        }
    }

    private Object jndiLookup(String name) throws NamingException {
        Object obj = null;

        if (jndiContext == null) {
            try {
                jndiContext = new InitialContext();
            } catch (NamingException e) {
                System.err.println("Could not create JNDI API " + "context: " + e.toString());
                throw e;
            }
        }
        try {
            obj = jndiContext.lookup(name);
        } catch (NamingException e) {
            System.err.println("JNDI API lookup failed: " + e.toString());
            throw e;
        }
        return obj;
    }

    public Main() {

    }

    public static void main(String[] args) {
        new Main();

    }
}

MQ队列设置

     <queue-manager>
         <name>AAA</name>
         <port>1423</port>
         <hostname>ddd</hostname>
        <clientChannel>EEE.CLIENTS.00</clientChannel>
        <securityClass>PKIJCExit</securityClass>
         <transportType>1</transportType>
        <targetClientMatching>1</targetClientMatching>
 </queue-manager>
 <queues>
     <queue-details id="queue-1">
        <name>GGGG.NY.00</name>
        <transacted>false</transacted>
        <acknowledgeMode>1</acknowledgeMode>
        <targetClient>1</targetClient>
     </queue-details>
</queues>

最佳答案

有一篇带有示例代码的文章 Running a standalone Java application on WebSphere MQ V6.0 它将引导您解决大部分问题,包括如何使用免费的 WMQ 试用安装进行测试。与 v7 或 v7.1 的主要区别(如评论中所述)是,如果您想使用主题,则无需启动代理组件。除此之外,本文应该适用于当前的 WMQ 客户端和/或服务器。

另外,请引用 WebSphere MQ Using Java 手册(v7.0 客户端)或 Using WebSphere MQ Classes for Java 手册(v7.1 客户端)为您的客户端适当的 CLASSPATH 和其他设置。请记住使用适合您的客户端版本而不是服务器版本的信息中心。您可以混合搭配客户端和服务器版本,但您只能获得服务器支持的功能。例如,将 WMQ v7.1 客户端与 WMQ v7.0 服务器一起使用是完全有效的。

最后,免费客户端下载提供了许多示例程序,它们完全符合您的描述。一些使用 JNDI 来查找 WMQ 资源,另一些使用 Java 方法并且可以使用标准 Java 属性文件填充。带有 -nojndi 选项的那些展示了如何在运行时在代码中初始化 WMQ 对象。这些都在

[WMQ Install path]\tools\wmqjava\samples

...在最新的 Windows 客户端安装中 (SupportPac MQC71)。您还可以使用 v7.0 客户端 ( SupportPac MQC7 )。我建议使用示例开始而不是从头开始。为什么要重新发明轮子,对吗?

除了许多示例程序之外,供应商安装还包括所有必需的 jar 文件。请注意,CLASSPATH 中的内容因 WMQ 客户端版本而异,因此请参阅信息中心。以后的版本要简单得多,只需要 CLASSPATH 中的几个 jar 文件。

如果你想download the WMQ trial用于测试并且在您的 Windows 工作站上没有管理员权限,您可以轻松地将它安装在 RedHat 或 SUSE 虚拟机上。通过一些按摩,您还可以轻松地在 Ubuntu 上安装,如 Andy Piper blog post 中所述。 .

关于Java 客户端监听 WebSphere MQ 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8403959/

相关文章:

java - WebSphere MQ 配置问题

java - BorderLayout,要求用户输入 HGap 和 VGap

java - MQ消息中的RFH头是什么及其用途

java - 使用Struts 2上传图像

java - 奇怪的字符(?)添加到我的主题文本的末尾

java - 如何从队列中接收并将其发布到主题中?

java - 多实例 MQ 中的切换故障转移和自动重新连接

apache - 在 Apache Flume 中获取 JMS header

java - 匹配以井号 (#) 符号开头的单词

java - JPA 空参数