在公共信息模型(Common Information Model,CIM)中,客戶機應用程序可以訂閱 CIM 事件通知。通常,應用程序可以通過不同的連接埠用多個事件處理程序創建事件過濾器,但是這將消耗大量網路資源並增加維護複雜度。在本文中,查看如何用一個特定連接埠註冊多個 CIM 事件處理程序。另請查看如何用 SBLIM CIM 客戶機庫編寫代碼的一些提示。
讓我們首先來查看一些關鍵詞。分散式管理任務工作組(Distributed Management Task Force,DMTF)公共信息模型(Common Information Model,CIM)是描述企業環境和 Internet 環境中的計算實體和業務實體的概念信息模型。它將提供一致的使用面向對象技術的數據定義和結構。
CIM 事件 是一種有趣的現象,可以按照生命周期 或警告 分類。CIM 事件十分複雜並且涉及到範圍廣泛的主題和場景。在 CIM 架構中,客戶機應用程序可以訂閱 CIM 事件的通知。通常,應用程序可以通過多個連接埠用相關事件處理程序創建事件過濾器。
在本文中,我們將展示如何用一個特定的連接埠註冊 CIM 事件處理程序以避免增加複雜度。這將使您可以更輕鬆地用最少的網路資源實現和維護一個具體的、可操作的 CIM 事件模型。
CIM 事件模型概覽
CIM 事件模型將定義與事件相關的抽象。它將描述 CIM 表示層次結構及如何將表示用於模型事件。事件模型還將描述如何使用訂閱進行註冊,以便您可以接收表示。
表示可以有零個或多個事件觸發器,這些觸發器可以識別狀態更改。例如,如果由於卷創建而觸動觸發器,此事件將得到一個卷創建的生命周期表示。另舉一例,如果由於拔出磁碟而觸動觸發器,此事件將得到磁碟已拔出的警告表示。
基於 HTTP 的 CIM 操作規範(有關鏈接,請參閱 參考資料)將描述發送和接收 CIM 表示的 CIM-XML 標準。圖 1 中顯示了 CIM 事件模型的全部工作流程:
整個操作流程包括以下步驟:
從技術上講,當客戶註冊以接收 CIM 事件模型中的表示時,需要創建三類 CIM 實例:
CIM 表示類層次結構用於描述在正確定義了表示訂閱后可以檢測到的事件類型。CIM_Indication 實例表示事件的出現。通常,表示是生命周期非常短暫的對象,用於從表示生成器到零個或多個表示使用者之間傳遞信息。下面是 CIM 事件模型中定義的相關 CIM 表示類:
當您在 CIM 事件模型中註冊 CIM 事件處理程序時可以使用所有類,這取決於具體要求。我們將在下面的小節中給您提供一些代碼示例並且在最後提供實驗結果。
創建事件過濾器
事件過濾器將描述要交付的事件類型及交付條件。要創建事件過濾器,需要創建 CIM_IndicationFilter 類的實例並定義其屬性值。可以設置這些屬性以惟一識別過濾器、指定查詢字元串和指定解析查詢字元串的查詢語言。注意,屬性是不是強制屬性取決於目標 CIM 伺服器中的 CIM 提供程序的實現;不同的 CIM 提供程序之間的字元串屬性格式會略有不同。
清單 1 顯示了用 SBLIM CIM 客戶機庫創建事件過濾器的示例:
// Define the name of CIM class to be created String filterClassName = "CIM_IndicationFilter"; // Define the filter string for CIM_IndicationFilter instance String filterString = "SELECT * FROM CIM_InstCreation WHERE SourceInstance ISA CIM_StorageVolume"; // Get the CIM class definition from the CIMOM CIMClass filterClass = _client.getClass(new CIMObjectPath(filterClassName)); // Create an instance of the CIM_IndicationFilter class CIMInstance filterInstance = filterClass.newInstance(); // Set the name of the filter filterInstance.setProperty("Name", new CIMValue("PCL_Event", new CIMDataType(CIMDataType.STRING))); // Set the filter string to select CIM events filterInstance.setProperty("Query", new CIMValue(filterString, new CIMDataType(CIMDataType.STRING))); // Set the query language (WQL) to parse the query string filterInstance.setProperty("QueryLanguage", new CIMValue("WQL", new CIMDataType(CIMDataType.STRING))); // Set the source name space of the filter filterInstance.setProperty("SourceNamespace", new CIMValue(_namespace, new CIMDataType(CIMDataType.STRING))); // Create an instance of CIM_IndicationFilter on the target CIM server CIMObjectPath filterCOP = _client.createInstance(new CIMObjectPath(), filterInstance); |
用一個連接埠創建事件處理程序
通常,應用程序可以通過多個連接埠用事件處理程序創建事件偵聽程序,但是這在網路資源和維護方面花費較大。
通過使用兩個類 —SBLIM CIM Client Library 中定義的 CIMEventDispatcher 和 HttpServerConnection — 可以用一個特定的連接埠註冊 CIM 事件處理程序。圖 2 中顯示了整個過程。
下面是用同一個埠創建事件處理程序的兩個步驟:
清單 2 和 3 顯示了創建方法:
// Create a CIMIndicationListenertList instance CIMIndicationListenertList _indicationClient = new CIMIndicationListenertList(); /* * Add a inner listener to the list, because if there is only one listener in * CIMIndicationListenertList, it will get all event no matter the event is the * certain listener registered to listen or not, we need to add a InnerListener * which implements CIMListener and doing nothing */ _indicationClient.addListener(new InnerListener()); /* * Add IndicationListenerList to a dispatcher, CIMEvent will be dispatched to * target listener according to the hash code of one listener */ CIMEventDispatcher dispatcher = new CIMEventDispatcher(_indicationClient); // Create an new CIMIndicationHandler instance which wraps the dispatcher CIMIndicationHandler indicationHdlr = new CIMIndicationHandler(dispatcher); // Create a HttpServerConnection with the specified port // NOTE: This target port is shared by all the event handlers HttpServerConnection _serverConn = new HttpServerConnection( new HttpConnectionHandler(indicationHdlr), targetPort); _serverConn.setName("CIMListener - Http Server"); _serverConn.start(); // You can also add listener after HttpServerConnection has started |
// Create internal listener, InternalListener should implement CIMListener and // have its own function InternalListener internalListener = new InternalListener(); // Add internal listener to CIMIndicationListenertList instance _indicationClient.addListener(internalListener); // Create a listener destination instance CIMClass indicationDestinationClass = _client.getClass( new CIMObjectPath("CIM_ListenerDestinationCIMXML"), false); CIMInstance cimListener = indicationDestinationClass.newInstance(); // Set the properties of CIM instance, and which properties are mandatory are // highly depending on the implementation of CIM providers // Set the creation class name of the listener cimListener.updateProperty(new CIMProperty("CreationClassName", new CIMValue("CIM_ListenerDestinationCIMXML", CIMDataType.getPredefinedType(CIMDataType.STRING)))); // Set the system creation class name of the listener cimListener.updateProperty(new CIMProperty("SystemCreationClassName", new CIMValue(null, CIMDataType.getPredefinedType(CIMDataType.STRING)))); // Set the system name of the listener cimListener.updateProperty(new CIMProperty("SystemName", new CIMValue(null, CIMDataType.getPredefinedType(CIMDataType.STRING)))); // Set the name of the listener cimListener.updateProperty(new CIMProperty("Name", new CIMValue(listenerName, CIMDataType.getPredefinedType(CIMDataType.STRING)))); // Set destination value, it should be combined with IP address of client and // hash code of the listener object cimListener.updateProperty(new CIMProperty("Destination", new CIMValue(IPAddress + "/" + internalListener.hashCode(), CIMDataType.getPredefinedType(CIMDataType.STRING)))); // Register the listener destination on the target CIM server CIMObjectPath listenerCOP = _client.createInstance(new CIMObjectPath(), cimListener); |
將事件過濾器綁定到事件處理程序
事件過濾器將定義過濾器的條件。事件處理程序將描述在發生特定事件后採取的操作。
因此,綁定事件過濾器與事件處理程序意味著設置在特定事件發生時採取特定操作的情形。
CIM 類 CIM_IndicationSubscription 中有兩個屬性:
在綁定事件過濾器與事件處理程序時,您是在創建 CIM_IndicationSubscription 類的實例並設置該實例的 Filter 和 Handler 屬性。在創建該類的表示時,將交付事件過濾器所指定的事件表示。
在 CIM 事件模型下,事件過濾器與事件處理程序之間的關係可以表示為 m:n,這意味著一個事件過濾器可以綁定到多個事件處理程序上,或者一個事件處理程序可以綁定到多個事件過濾器。圖 3 和 4 演示了這兩種情況。
清單 4 顯示了如何用 SBLIM CIM 客戶機庫綁定事件處理程序與事件過濾器。
// Define class name of indication subscription String subscriptionClassName = "CIM_IndicationSubscription"; // Construct object path of CIM_IndicationSubscription CIMObjectPath cop = new CIMObjectPath(subscriptionClassName, _namespace); // Get the CIMClass object for subscription CIMClass subscriptionClass = _client.getClass(cop); // Create CIM_IndicationSubscription instance and set properties CIMInstance subscriptionInstance = subscriptionClass.newInstance(); // Set object path of event filter instance as the value of "Filter" property subscriptionInstance.setProperty("Filter", new CIMValue(filterInstance.getObjectPath(), new CIMDataType(CIMDataType.REFERENCE))); // Set object path of event handler instance as the value of "Handler" property subscriptionInstance.setProperty("Handler", new CIMValue(handlerInstance.getObjectPath(), new CIMDataType(CIMDataType.REFERENCE))); // Create CIM_IndicationSubscription instance on the target CIM server CIMObjectPath sop = _client.createInstance(new CIMObjectPath(), subscriptionInstance); |
Linux 中的測試結果
我們在 Linux® 中部署了實現並且執行了一些測試。當在目標 CIM 伺服器上註冊 CIM 事件處理程序時,我們能夠獲得每個建立的偵聽程序的連接信息。
圖 5 顯示了典型網路狀態;每個偵聽程序都有自己的通信埠。
圖 6 顯示了我們的實現使用的網路狀態。您可以看到所有偵聽程序都共享同一個通信埠。
圖 6. 我們實現的註冊 CIM 事件處理程序
(責任編輯:A6)
[火星人 ] 高效構建 CIM 事件模型已經有831次圍觀