程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> WebSphere >> 用社交網絡連接WebSphere MQ:列隊管理器和MQ應用程序的Twitter通知

用社交網絡連接WebSphere MQ:列隊管理器和MQ應用程序的Twitter通知

編輯:WebSphere

如今,社交網絡無所不在 —— 為了與朋友聯系,或是為了讓自己與時俱進,抑或是為了讓別人獲知共同關心話題的最新進展。社交網絡在企業中也很有用。本文將向您展示如何快速而輕松地在您的 WebSphere MQ 應用程序中使用社交網絡軟件(比如 Twitter)向廣大的系統管理員或最終用戶,甚至是向其他應用程序或中間件發送狀態及問題信息。本文中的示例使用的是面向 WebSphere Application Server Community Edition 運行時的 JEE 技術(簡單的消息驅動的 bean)的 WebSphere MQ 和 Twitter API。

簡介

社交網絡已經取得了爆炸式的發展,下面所列就是一些社交網絡的站點:Facebook、LinkedIn 和 Twitter。各企業現在也開始著手建立內部的社交網絡,而 IBM 的 ® Lotus® Connections 產品可以讓我們很方便地建立企業規模的社交網絡。

本文將向您展示如何在一個企業消息產品,比如 WebSphere® MQ 內使用社交網絡軟件。本文中的三個示例使用的是 Twitter,但也可以使用其他的具有 API 的社交網絡站點。這三個 MQ-Twitter 的例子是:

一個簡單的隊列,文本消息將從這裡被檢索並直接發布給 Twitter

隊列管理器事件,例如創建或刪除隊列

一個使用了 WebSphere MQ File Transfer Edition(後面簡稱為 WebSphere MQ FTE)的發布/訂閱示例

本文的示例是用部署到 WebSphere Application Server Community Edition 的消息驅動 bean (MDB) 開發的。另一種方式是使用一個具有消息偵聽器的獨立 Java™ 應用程序。下面將要介紹的這些代碼清單均截取自一個 zip 文件,這個 zip 文件包括了運行這個示例所需的所有源文件。

Twitter API

很多 Java 庫都提供一個到 Twitter API 的接口。本文中的示例使用 Apache Commons HTTP 庫與 Twitter API 通信。Twitter API 是一個很好的規范 —— 要了解更多信息,請參見 Twitter API wiki。

清單 1 內顯示的這個 Java 方法可用來 tweet 一個給定消息。要獲得完整的 Java 類,請下載並參考上述的 zip 文件。

清單 1. TwitterPlugin.java: sendNotification() 方法

public void sendNotification(String message) {

   if(message.length() > 140) {
    System.err.println("Message will be truncated from: "
      + message + " to: " + message.substring(0, 140));
   }

   PostMethod post = null;
   try {
    HttpClient client = new HttpClient();
    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(new AuthScope("twitter.com", 80, "realm"),
      new UsernamePasswordCredentials(getUsername(), getPassword()));
    post = new PostMethod("http://twitter.com/statuses/update.xml");
    post.setQueryString(URIUtil.encodeQuery("status="+message));
    post.setDoAuthentication( true );

   // execute the GET 
   int status = client.executeMethod( post );
   // print the status and response
   System.out.println("Status: " + status);

    } catch (URIException e ) {
      System.err.println(e.getMessage());
    } catch (HttpException e) {
      System.err.println(e.getMessage());
    } catch (IOException e) {
      System.err.println(e.getMessage());
    } finally {
   // release any connection resources used by the method 
      if(post != null) {
       post.releaseConnection();
      }
   }
}

簡單的文本消息 MDB

使用簡單的文本消息,可以快速地將 MQ 消息轉換為 Tweet。本例使用了一個標准的 MQ 隊列。要想驅動這個示例,可以使用任何能將一個文本消息放到隊列上的應用程序,比如隨 WebSphere MQ 所帶的 amqsput 示例。以下是 MDB 代碼:

清單 2. SimpleMQEJB.java: onMessage() 方法

public void onMessage(Message message) {

   if(message instanceof TextMessage) {
    try {
   /* Put your twitter username and password here */
    TwitterPlugin tp = new TwitterPlugin("YOUR_TWITTER_USERNAME",
      "YOUR_TWITTER_PASSWORD");
    String output = ((TextMessage) message).getText();
    tp.sendNotification(output);
    } catch(JMSException e) {
      e.printStackTrace();
    }
   } else {
    System.err.println("This application requires" +
      " a message in TextMessage format");
   }
}

上述代碼很簡單:它獲取 MQ Message 的有效負載,將它截短到 140 個字符,然後用 TwitterPlugin.java 代碼張貼它。圖 1 顯示了此示例在我的 mq_tweet twitter 帳戶上的輸出:

圖 1. 簡單的文本消息作為一個 tweet

MQ 事件消息 MDB

WebSphere MQ 通過 MQ Events Messaging 提供了一個隊列管理器內的錯誤、警告和其他重大動作的相關信息。在配置了以上功能後,WebSphere MQ 會將一個消息放到一個特定隊列上,然後應用程序可以從這個隊列中使用這個消息並根據這個消息的內容執行動作。

清單 3 顯示了另一個 MDB,但這次它被配置為從 SYSTEM.ADMIN.CONFIG.EVENT 隊列中讀取消息。這個 MDB 獲取消息並用簡單的邏輯決定將什麼消息發送給 Twitter:

清單 3. MQEventMDB.java: onMessage() 方法

public void onMessage(Message msg) {

   if(msg == null)return;
   if (msg instanceof TextMessage) {
    TextMessage txtMsg = (TextMessage) msg;
   try {
       System.out.println("Received TextMessage: " + txtMsg.getText());
      } catch (JMSException e) {
       e.printStackTrace();
      }
    }

   if (msg instanceof BytesMessage) {
    JMSBytesMessage bytesMsg = (JMSBytesMessage) msg;
    int bodySize;
    try {
      bodySize = (int) bytesMsg.getBodyLength();
      byte[] data = new byte[bodySize];
    bytesMsg.readBytes(data);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    DataInput dataInput = new DataInputStream(bais);
    PCFMessage response = new PCFMessage(dataInput);
    int reason = response.getReason();
    int type = response.getIntParameterValue(MQConstants.MQIACF_OBJECT_TYPE);
    if(type == MQConstants.MQOT_Q) {
      publishQueueEvent(response, null);
    } else {
      System.out.println("Object Type received: " + reason);
    }
    } catch (Exception e) {
      e.printStackTrace();
    }
   }
}

由於 MQ Event 消息是以 PCF 格式發布的,所以必須要將 JMSBytesMessage 對象轉換成 PCFMessage 對象。可以按照以下的步驟來完成這個轉換:讀取入向消息的字節並創建一個 DataInput 對象,然後將它傳遞給 PCFMessage 的構造函數。圖 2 中所示的是當創建一個隊列時所生成的一個輸出示例:

圖 2. 隊列創建事件消息作為一個 tweet

WebSphere MQ FTE Transfer Message MDB

WebSphere MQ FTE 是 WebSphere MQ 的一個新版本,它可以設法實現安全可靠的文件傳輸,並能發布關於傳輸審計日志的消息。有關這些審計消息的 XML 模式的信息,請參見 WebSphere MQ FTE 信息中心的 Message formats。聯合使用 MDB 及一些 JAXB 代碼,可以對文件傳輸的開始和結束進行 Twitter,甚至可以包括在一次傳輸的元數據中定義的一些散列標簽。

清單 4 是特定於 WebSphere MQ FTE 的 MDB。這裡,沒有連接到一個隊列,而是連接到一個 FTE 主題。如果元數據的名稱是以關鍵詞 Twitter 開頭的,那麼與這個文件傳輸相關聯的這個元數據將被追加到狀態消息。例如,Twitter.tag=SimpleTweet 會導致 #SimpleTweet 被追加到這個被發送的消息:

清單 4. MQEventMDB.java: onMessage() 方法

public void onMessage(Message message) {

   if(message instanceof TextMessage) {
    try {
      TwitterPlugin tp = new TwitterPlugin("YOUR_USERNAME", "YOUR_PASSWORD");
    String output = ((TextMessage) message).getText();
    String notification = null;
    if(output.contains("TransferLog.xsd")) {

      if(output.contains("started")) {
       Transaction transaction = generateTransaction(output);
       String transferName = getJobName(transaction);
       String twitterTags = getTwitterTags(transaction);
       notification = "Transfer started: " + transferName;
       if(notification.length() "<" (140 - twitterTags.length())) {
         notification = notification + " #ftetweet" + " " + twitterTags;
       }
      } else if(output.contains("completed")) {
       Transaction transaction = generateTransaction(output);
       String transferName = getJobName(transaction);
       String twitterTags = getTwitterTags(transaction);
       String transferFailed = "";
       if(transaction.getStatus().getResultCode() != 0) {
         transferFailed = " Transfer Failed (RC=" +
          transaction.getStatus().getResultCode()+")";
       }

       notification = "Transfer complete: " + transferName + transferFailed;
       if(notification.length() "<" (140 - twitterTags.length())) {
         notification = notification + " #ftetweet" +
         " " + twitterTags;
       }
      }
    }
      if(notification != null) {
       tp.sendNotification(notification);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
   } else {
      System.err.println("This application requires a message in TextMessage format");
   }
}

下面的圖 3 和 圖 4 顯示了一個文件傳輸開始和結束時所生成的 tweet:

圖 3. WebSphere MQ FTE 文件傳輸開始

圖 4. WebSphere MQ FTE 文件傳輸結束

結束語

本文向您展示了連接 Twitter 與 WebSphere MQ 隊列管理器或是 WebSphere MQ 應用程序是多麼容易實現以及它對於將狀態消息廣播給感興趣的群體是多麼地有效。在企業中可以部署一個名為 StatusNet(以前被稱為 Laconica)的 Twitter-風格的開源實現,該實現使用了與 Twitter 完全相同的 API,可以讓企業能夠快速地建立並部署一個社交網絡基礎設施。您可以很容易地用 Status.net 代替 Twitter 來開始發布有關您企業工作的信息,而同時無需擔心有任何的保密和敏感信息被發布到公共領域。

代碼:http://www.ibm.com/developerworks/cn/websphere/library/techarticles/1003_cumbers/1003_cumbers.html

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved