编程语言
首页 > 编程语言> > Java客户端监听WebSphere MQ Server?

Java客户端监听WebSphere MQ Server?

作者:互联网

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

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

这是一个独立的Java项目,没有应用程序服务器支持.我应该把哪些罐放入classpath?

我有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虚拟机上轻松安装.通过一些按摩,您也可以按照Andy Piper blog post中的描述在Ubuntu上轻松安装.

标签:java,jms,ibm-mq
来源: https://codeday.me/bug/20190902/1792720.html