2011-12-06 74 views
2

我需要編寫一個監聽WebSphere MQ服務器的Java客戶機。消息放入服務器的隊列中。Java客戶端正在監聽WebSphere MQ服務器?

我開發了這個代碼,但我不確定它是否正確。如果正確,那我該如何測試它?

這是一個獨立的Java項目,沒有應用程序服務器支持。我應該將哪些jar放入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> 

回答

1

如果可以選擇,我會建議你介紹Spring框架來處理JMS通信。這樣你只需要編寫你的業務邏輯,並且可以將錯誤處理留給Spring。

Spring JMS

下載最新Spring JARS,並期待在您的應用程序配置DefaultMessageListenerContainer。然後,您將編寫自己的POJO(普通的舊Java對象),並在每次有新消息到達時調用onMessage()事件。

我發現this tutorial,你會發現下手

3

有用沒有用示例代碼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和其他設置。請記住使用適合您的客戶端版本的Infocenter,而不是服務器版本。您可以混合搭配客戶端和服務器版本,但只能獲得服務器支持的功能。例如,使用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文件。請注意,WMQ客戶端版本中更改CLASSPATH的內容,請參閱Infocenter。後來的版本更簡單,只需要CLASSPATH中的幾個jar文件。

如果您想要download the WMQ trial進行測試,並且沒有Windows工作站的管理員權限,則可以在RedHat或SUSE虛擬機上輕鬆安裝。按照Andy Piper blog post中的描述,使用一些按摩功能,您還可以在Ubuntu上輕鬆安裝。

0

有一個小的客戶端應用程序,我在JavaFx2中爲Windows和Mac OSX開發。它可以在源代碼中找到(https://sourceforge.net/projects/mqconsole)。

您可以看到一個偵聽新郵件隊列的示例。

該程序列出隊列,每個隊列中的消息,查看消息的詳細信息並將消息發送到隊列並偵聽響應。

您可以檢出代碼,使用它並對其進行改進。

相關問題