IBM MQ 使用 Java 程序发送消息

IBM MQ Send Message Using Java Program

我正在尝试从 Websphere Application Server 上的 java 程序 运行 向 MQ 队列发送消息。 我在 Websphere Applciaiton 服务器中配置了 QConnection Factory & Q jndis。但是当 运行 程序出现错误时

Details: "com.ibm.ejs.jms.JMSConnectionFactoryHandle incompatible with javax.jms.QueueConnectionFactory".
        at com.ibm.bpm.rest.impl.service.ServiceRunner$TaskRunner.runService(ServiceRunner.java:1385)
        at com.ibm.bpm.rest.impl.service.StartActionHandler.handleActionGetModel(StartActionHandler.java:363)
        at com.ibm.bpm.rest.impl.playback.ServicePlaybackResourceImpl.createServicePlayback(ServicePlaybackResourceImpl.java:141)
        at com.ibm.bpm.rest.impl.playback.ServicePlaybackResource.createServicePlayback(ServicePlaybackResource.java:115)
        at sun.reflect.GeneratedMethodAccessor742.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)

这是我正在使用的程序。非常感谢任何帮助。

public void putMessageViaCF3(String messageContent, String connectionFactory, String sendQName)
            throws MQException, IOException, NamingException, JMSException {
        Queue myQueue;
        QueueConnectionFactory myQueueFactory;
        QueueConnection connection = null;
        QueueSession session = null;
        try {
            InitialContext jndi = new InitialContext();
            myQueueFactory = (QueueConnectionFactory) jndi.lookup("jms/SORC_QM_CF");
            myQueue = (Queue) jndi.lookup("jms/SORC_SEND_Q");

            connection=myQueueFactory.createQueueConnection();

            session = connection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);

            QueueSender sender = session.createSender(myQueue);
            connection.start();
            TextMessage textMessage = session.createTextMessage("Test Harish");
            textMessage.setStringProperty("messageType", "file");
            sender.send(textMessage);

            sender.close();
            
        } finally {

            if (session != null) {
                session.close();
            }

            if (connection != null) {
                connection.close();
            }
        }

    }

根据this resource from IBM,这种ClassCastException出现在:

...a queue connection factory is defined as a WebSphere MQ Connection Factory instead of a WebSphere MQ Queue Connection Factory.

要解决此问题,您应该:

  • 将连接工厂定义为 WebSphere MQ 队列连接工厂
  • 在应用程序代码中使用 javax.jms.ConnectionFactory 对象而不是 javax.jms.QueueConnectionFactory 对象。