2015-04-29 189 views
2

這是我的java類代碼JMS無法連接到WebSphere MQ


package com.ibm.point_A_point; 

import javax.jms.Connection; 
import javax.jms.Destination; 
import javax.jms.JMSException; 
import javax.jms.Message; 
import javax.jms.MessageConsumer; 
import javax.jms.MessageProducer; 
import javax.jms.Session; 
import javax.jms.TextMessage; 

import com.ibm.msg.client.jms.JmsConnectionFactory; 
import com.ibm.msg.client.jms.JmsFactoryFactory; 
import com.ibm.msg.client.wmq.WMQConstants; 

/** 
* A minimal and simple application for Point-to-point messaging. 
* 
* Application makes use of fixed literals, any customisations will require re-compilation of this 
* source file. Application assumes that the named queue is empty prior to a run. 
* 
* Notes: 
* 
* API type: JMS API (v1.1, unified domain) 
* 
* Messaging domain: Point-to-point 
* 
* Provider type: WebSphere MQ 
* 
* Connection mode: Client connection 
* 
* JNDI in use: No 
* 
*/ 
public class SimplePTP { 

    // System exit status value (assume unset value to be 1) 
    private static int status = 1; 

    /** 
    * Main method 
    * 
    * @param args 
    */ 
    public static void main(String[] args) { 

    // Variables 
    Connection connection = null; 
    Session session = null; 
    Destination destination = null; 
    MessageProducer producer = null; 
    MessageConsumer consumer = null; 

    try { 
     // Create a connection factory 
     JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER); 
     JmsConnectionFactory cf = ff.createConnectionFactory(); 

     // Set the properties 
     cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, "localhost"); 
     cf.setIntProperty(WMQConstants.WMQ_PORT, 1414); 
     cf.setStringProperty(WMQConstants.WMQ_CHANNEL, "SYSTEM.DEF.SVRCONN"); 
     cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT); 
     cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "QM1"); 
     cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "SimplePTP (JMS)"); 

     // Create JMS objects 
     connection = cf.createConnection(); 
     session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
     destination = session.createQueue("queue:///Q1"); 
     producer = session.createProducer(destination); 
     consumer = session.createConsumer(destination); 

     long uniqueNumber = System.currentTimeMillis() % 1000; 
     TextMessage message = session.createTextMessage("SimplePTP: Your lucky number today is " 
                 + uniqueNumber); 

     // Start the connection 
     connection.start(); 

     // And, send the message 
     producer.send(message); 
     System.out.println("Sent message:\n" + message); 

     Message receivedMessage = consumer.receive(15000); // in ms or 15 seconds 
     System.out.println("\nReceived message:\n" + receivedMessage); 

     recordSuccess(); 
    } 
    catch (JMSException jmsex) { 
     recordFailure(jmsex); 
    } 
    finally { 
     if (producer != null) { 
     try { 
      producer.close(); 
     } 
     catch (JMSException jmsex) { 
      System.out.println("Producer could not be closed."); 
      recordFailure(jmsex); 
     } 
     } 
     if (consumer != null) { 
     try { 
      consumer.close(); 
     } 
     catch (JMSException jmsex) { 
      System.out.println("Consumer could not be closed."); 
      recordFailure(jmsex); 
     } 
     } 

     if (session != null) { 
     try { 
      session.close(); 
     } 
     catch (JMSException jmsex) { 
      System.out.println("Session could not be closed."); 
      recordFailure(jmsex); 
     } 
     } 

     if (connection != null) { 
     try { 
      connection.close(); 
     } 
     catch (JMSException jmsex) { 
      System.out.println("Connection could not be closed."); 
      recordFailure(jmsex); 
     } 
     } 
    } 
    System.exit(status); 
    return; 
    } // end main() 

    /** 
    * Process a JMSException and any associated inner exceptions. 
    * 
    * @param jmsex 
    */ 
    private static void processJMSException(JMSException jmsex) { 
    System.out.println(jmsex); 
    Throwable innerException = jmsex.getLinkedException(); 
    if (innerException != null) { 
     System.out.println("Inner exception(s):"); 
    } 
    while (innerException != null) { 
     System.out.println(innerException); 
     innerException = innerException.getCause(); 
    } 
    return; 
    } 

    /** 
    * Record this run as successful. 
    */ 
    private static void recordSuccess() { 
    System.out.println("SUCCESS"); 
    status = 0; 
    return; 
    } 

    /** 
    * Record this run as failure. 
    * 
    * @param ex 
    */ 
    private static void recordFailure(Exception ex) { 
    if (ex != null) { 
     if (ex instanceof JMSException) { 
     processJMSException((JMSException) ex); 
     } 
     else { 
     System.out.println(ex); 
     } 
    } 
    System.out.println("FAILURE"); 
    status = -1; 
    return; 
    } 

} 

我一直這個錯誤(異常) com.ibm.msg.client.jms.DetailedJMSSecurityException :JMSWMQ2013:通過連接模式'Client'和主機名'localhost(1414)'提供給隊列管理器'QM1'的身份驗證安全性無效。 檢查提供的用戶名和密碼在連接的等待隊列管理器中是否正確。 WebSphere MQ調用失敗,並顯示完成代碼'2'('MQCC_FAILED');模式'2035'('MQRC_NOT_AUTHORIZED')。

+0

您應該提供用戶名和密碼以連接到隊列。 – CKing

+0

但是如何獲取密碼和用戶名? –

回答

1

您需要用戶標識和密碼(取決於MQ的版本)才能連接到隊列管理器。該用戶標識通常存在於隊列管理器正在運行的計算機上。

您需要在代碼中傳遞用戶標識和密碼,如下所示。用您的用戶和密碼替換。

cf.setStringProperty(WMQConstants.USERID,"userid"); 
cf.setStringProperty(WMQConstants.PASSWORD, "password"); 

除此之外,用戶必須被賦予權力來連接到隊列管理器,把/獲取有關應用程序正在使用的隊列。

我建議您與您的MQ管理員聯繫以提供用戶標識/密碼信息以及必需的權限。

2
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, "SYSTEM.DEF.SVRCONN"); 

您不應該使用SYSTEM通道。讓您的MQAdmin爲您的應用程序創建一個。

connection = cf.createConnection(); 

將其更改爲:

connection = cf.createConnection("myUser","myPswd"); 

提出您MQADMIN如果認證是針對爲本地操作系統或LDAP,然後使用適當的用戶名和密碼。

相關問題