在 IBM MQ in java 程序中创建主题

Create a topic in IBM MQ in java program

我想在我的 Java 应用程序中创建一个名称为 "manipulation" 的主题,但出现此错误:

Exception in thread "main" com.ibm.mq.MQException: MQJE001: Completion Code '2', Reason '2085'

我的代码是:

MQTopic subscriber = new MQTopic(queueManager, null, "manipulation",CMQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, CMQC.MQSO_CREATE);

如这里所述:

Accessing queues, topics, and processes in IBM MQ classes for Java

MQ 异常 2085 表示 MQRC_UNKNOWN_OBJECT_NAME

您提供的队列对象名称无效。

示例代码:

try {       
     MQTopicConnectionFactory cf = new MQTopicConnectionFactory();        
     // Config       
     cf.setHostName("<Destination-IP>");       
     cf.setPort(1414);
     cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);           
     cf.setQueueManager("<Queue-Manager-Name>");          
     cf.setChannel("SYSTEM.DEF.SVRCONN");        
     MQTopicConnection connection = (MQTopicConnection) cf.createTopicConnection();       
     MQTopicSession session = (MQTopicSession) connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);       
     MQTopic topic = (MQTopic) session.createTopic("topic://test");              
     MQTopicPublisher publisher =  (MQTopicPublisher) session.createPublisher(topic);       
     MQTopicSubscriber subscriber = (MQTopicSubscriber) session.createSubscriber(topic);              
     JMSTextMessage message = (JMSTextMessage) session.createTextMessage("Hello World");             
     // Start the connection      
     connection.start();        
     publisher.publish(message);       
     log.info("Sent message:" + message);        
     JMSMessage receivedMessage = (JMSMessage) subscriber.receive(10000);        
     log.info("Received message:" + receivedMessage);        
     publisher.close();       
     subscriber.close();       
     session.close();       
     connection.close();        
     log.info("SUCCESS");     
     } catch (JMSException jex) {       
       log.error(jex.getMessage());            
     } catch (Exception ex) {       
       log.error(ex.getMessage());       
      System.out.println("\nFAILURE\n");     
  }   
}

您也可以在命令提示符下执行相同的操作

crtmqm QM_A //Create Queue-Manager
strmqm QM_A //Start Queue-Manager
runmqsc QM_A //Open Websphere MQ CLI
DEFINE LISTENER(A.LISTENER) TRPTYPE(TCP) CONTROL(QMGR) PORT(1414) //Define Listener
START LISTENER(A.LISTENER) //Start Listener
DEFINE CHANNEL(A.CHANNEL) CHLTYPE(SVRCONN) //Define Channel
START CHANNEL(A.CHANNEL) // Start Channel
DEFINE QLOCAL(A.QUEUE) MAXDEPTH(50000) //Define LocalQueue
DEFINE TOPIC(A.TOPIC) TOPICSTR('A') //Define Topic
DEFINE SUB(A.SUBQUEUE) DEST(A.QUEUE) TOPICOBJ(A.TOPIC) REPLACE; //Link Subscription Queue with Topic

访问http://bencane.com/2013/04/22/websphere-mq-cheat-sheet-for-system-administrators/

您用于创建主题订阅的代码需要稍作修改才能使其正常工作:

MQTopic subscriber = new MQTopic(queueManager, null, "manipulation",CMQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, CMQC.MQSO_CREATE);

在上面的代码片段中,您指定了第三个参数,即主题对象名称。当您指定主题对象名称时,该对象必须存在才能使用。在您的情况下,您已经管理性地创建了一个名称为 manipulation 的主题对象,然后使用上面的构造函数创建了一个订阅。由于主题 manipulation 不存在,因此抛出 MQRC 2085 异常。

你有两个选择:

1) 管理性地创建主题对象 "manipulation".

2) 如果您不想创建主题对象,请在不使用主题对象名称的情况下即时创建订阅,如下所示

MQTopic subscriber = queueManager.accessTopic("manipulation", "", CMQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, CMQC.MQSO_MANAGED | CMQC.MQSO_CREATE | CMQC.MQOO_FAIL_IF_QUIESCING);