4 JMXを使用したWebLogic Server MBeanへのアクセス
この章の内容は以下のとおりです。
リモート・クライアントのクラスパスの設定
JMXクライアントが独自のJVM (つまり、WebLogic ServerインスタンスでないJVM)で実行される場合は、クライアントのクラスパスに次のJARファイルを含めます。
WL_HOME\server\lib\weblogic.jar
ここで、WL_HOME
は、WebLogic Serverのインストール先ディレクトリです。
リモート・アクセスのためにwlthint3client.jar
ライブラリが提供されます。このライブラリを使用すると、T3またはT3Sプロトコルによる接続でWebLogic ServerインスタンスまたはドメインのMBeanにアクセスすることができます。T3プロトコルは、WebLogic Serverと相互運用するために最適化された高性能プロトコルです。できるだけT3プロトコルを使用することをお薦めします。
wlthint3client.jar
を使用するには、クライアントのクラスパスにwlthint3client.jar
を追加します。外部サーバーにホストされているアプリケーションは、wlthint3client.jar
を使用すると、WebLogic Serverインスタンスのリモート・クライアントとして機能できます。JMS、サーブレット、EJB、起動クラスなどのリモート・サービスへのアクセスを可能にするには、必要なアプリケーション・コードをwlthint3client.jar
とともにアプリケーション・サーバーにデプロイします。「WebLogicシンT3クライアントの理解」を参照してください。
MBeanサーバーへのリモート接続の作成
各WebLogic Serverドメインには3種類のMBeanサーバーが含まれており、それぞれのMBeanサーバーが別々のMBean階層へのアクセスを提供します。「MBeanサーバー」を参照してください。
WebLogic MBeanサーバーに接続するには、次の手順に従います。
例:ドメイン・ランタイムMBeanサーバーへの接続
例4-1のコードに関する以下の点に留意してください。
-
クラスは、グローバル変数
connection
およびconnector
を使用して、MBeanサーバーへの接続を表します。connection
変数およびconnector
変数に値を割り当てるinitConnection()
メソッドは、クラス内で再利用できる1つの接続を確立するためにクラス・インスタンスごとに一度だけ呼び出される必要があります。 -
initConnection()
メソッドは、クラスがインスタンス化されるときに渡される引数として、(サーバーのリスニング・アドレスとリスニング・ポートとともに)ユーザー名とパスワードを取ります。暗号化されていないユーザー資格証明がコード内に含まれないようにするため、この方法をお薦めします。これらの引数を保持するString
オブジェクトは破棄され、JVMのガベージ・コレクション・ルーチンによってメモリーから削除されます。 -
クライアントは、
jmx.remote.x.request.waiting.timeout
環境パラメータを10000に設定するので、MBeanサーバー・メソッド呼出しはすべて、呼出し後10000ミリ秒以内にメソッドが返されなければ、タイムアウトします。 -
クラスが作業を終了したら、
JMXConnector.close()
メソッドを呼び出してMBeanサーバーとの接続を閉じる(Java SE 8 API仕様のJMXConnector
(http://docs.oracle.com/javase/8/docs/api/javax/management/remote/JMXConnector.html
)を参照してください。)
例4-1 ドメイン・ランタイムMBeanサーバーへの接続
public class MyConnection { private static MBeanServerConnection connection; private static JMXConnector connector; private static final ObjectName service; /* * Initialize connection to the Domain Runtime MBean Server. */ public static void initConnection(String hostname, String portString, String username, String password) throws IOException, MalformedURLException { String protocol = "t3"; Integer portInteger = Integer.valueOf(portString); int port = portInteger.intValue(); String jndiroot = "/jndi/"; String mserver = "weblogic.management.mbeanservers.domainruntime"; JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver); Hashtable h = new Hashtable(); h.put(Context.SECURITY_PRINCIPAL, username); h.put(Context.SECURITY_CREDENTIALS, password); h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote"); h.put("jmx.remote.x.request.waiting.timeout", new Long(10000)); connector = JMXConnectorFactory.connect(serviceURL, h); connection = connector.getMBeanServerConnection(); } public static void main(String[] args) throws Exception { String hostname = args[0]; String portString = args[1]; String username = args[2]; String password = args[3]; MyConnection c= new MyConnection(); initConnection(hostname, portString, username, password); ... connector.close(); } }
ベスト・プラクティス: MBeanサーバーの選択
WebLogic Serverドメインは3種類のMBeanサーバーを保持しており、それぞれのMBeanサーバーが特定の機能を実行します。以下のように、実行しようとしているタスクをサポートしているMBeanサーバーを介してMBeanにアクセスします。
-
ドメインの構成を変更するには、編集MBeanサーバーを使用します。
-
保留中の構成MBeanの階層に対する変更をモニターするには、編集MBeanサーバーを使用します。
-
アクティブな構成MBeanのみをモニターする(ランタイムMBeanはモニターしない)ためには、ランタイムMBeanサーバーを使用します。
ランタイムMBeanサーバーを使用したモニターのほうが、ドメイン・ランタイムMBeanサーバーを使用したモニターより、使用するメモリーとネットワーク・トラフィックの量が小さくなります。(WebLogic Serverは、クライアントが接続を要求するまでドメイン・ランタイムMBeanサーバーを初期化しません。)
ほとんどの場合、ドメイン内のすべてのサーバー・インスタンスは同じ構成データのセットを保持しているため、管理サーバーまたは管理対象サーバーどちらのランタイムMBeanサーバーをモニターするかは重要ではありません。ただし、サーバーを再起動しないと消費できない変更を行うと、サーバーは変更を受け入れなくなり、そのサーバーの構成データが古くなるおそれがあります。この場合、このサーバーのランタイムMBeanサーバーをモニターすることは、その特定のサーバー・インスタンスの構成のみをモニターすることになります。WebLogic Serverドメインの変更、および変更のアクティブ化のプロセスを理解するには、Oracle WebLogic Serverドメイン構成の理解の構成の変更の管理を参照してください。
-
クライアントが複数のサーバーのランタイムMBeanをモニターする場合や、クライアントが別のJVMで実行されている場合は、ドメイン内の各サーバー・インスタンス上のそれぞれのランタイムMBeanサーバーに個別に接続するのではなく、管理サーバー上のドメイン・ランタイムMBeanサーバーに接続することをお薦めします。
ドメイン・ランタイムMbeanサーバーのMbeanにJMXリスナーとフィルタを登録する場合は、JMXフィルタを、それがモニターするMBeanと同じJVMで実行します。たとえば、ある管理対象サーバー上のMBeanにフィルタを登録する場合は、フィルタをその管理対象サーバーで実行し、フィルタ条件を満たすメッセージのみをリスナーに転送します。
一般的に、ドメイン・ランタイムMBeanサーバーを使用するコードのほうが、各サーバー・インスタンス上のランタイムMBeanサーバーを使用するコードより管理がしやすく安全性も高くなっています。その理由は以下のとおりです。
-
コードで必要なのは、管理サーバー上のドメイン・ランタイムMBeanサーバーに接続するための1つのURLを作成することだけです。以後、コードですべてのサーバー・インスタンスの値をルックアップしたり、必要に応じて結果をフィルタ処理したりできます。
-
コードでランタイムMBeanサーバーを使用して複数のサーバー・インスタンス上のMBeanの値を読み込む場合には、各サーバー・インスタンス用のURLを作成する必要があり、URLごとに一意のリスニング・アドレスとリスニング・ポートの組合せが必要になります。
-
管理サーバーのセキュアな管理ポートを介してWebLogic Serverドメイン内のすべての管理トラフィックをルーティングでき、ファイアウォールを使用して管理対象サーバーの管理ポートへのファイアウォール外部からの接続を回避できます。
ドメイン・ランタイムMBeanサーバーを使用してすべてのJMXリクエストを転送することによるトレード・オフとして、ネットワーク・レイテンシとメモリー使用量の増加のためにパフォーマンスが若干低下します。MBean値を読み込むために各管理対象サーバーのランタイムMBeanサーバーに直接接続すると、管理対象サーバーから値を取得するためにドメイン・ランタイムMBeanサーバーが作成するネットワーク・ホップが削除されます。ただし、大半のネットワーク・トポロジとパフォーマンス要件では、ドメイン・ランタイムMBeanサーバーによって有効化される簡略化されたコード・メンテナンスおよび強化セキュリティが推奨されます。
ノート:
JMX通知がMBeansに追加されると、ドメイン・ランタイムMBeanサーバーは大量のメモリーを消費する可能性があります。JMX通知が使用されると、次の2つの場合には、ドメイン内のすべての管理対象サーバーで実行されるすべてのランタイムMBeanサーバーに登録されたすべてのJMXオブジェクト名のコピーを管理サーバーが保持することになります。
-
WebLogic Serverレベルで、管理対象サーバーの停止の際の未登録MBean通知をシミュレートする場合。
-
JDK JMXクライアント通知レイヤーにおいて。
次の条件がどちらも当てはまる場合、この問題が発生する可能性は高くなります。
-
JMXランタイムMBeanの数を大幅に増やすFusion Middleware製品がドメインに含まれている。これには、ドメイン内で実行されているWebLogic ServerランタイムMBeanサーバー・インスタンス(つまり、管理サーバーとすべての管理対象サーバー)に登録されているMBeanを含む任意の製品が含まれます。(これらの製品には、Coherence、SOA Suite、OSBなどがあります。)
このような特定のスケーリング問題を排除するには、
managed-server-notifications-enabled
属性を無効にします。この構成属性は、管理対象サーバー・ランタイムMBeanサーバーに含まれるMBeanで通知を定義する機能を無効にします(これらのMBeanのObjectNameにはLocation=key
が含まれています)。管理対象サーバー通知が無効になると、WebLogic ServerおよびJDKコンポーネントに含まれるMBeanの2つのObjectNameセットが保持されなくなります。通知リスナーは、MBeanServerDelegateや、ローカルのドメイン・ランタイムMBeanサーバーに含まれるMBeanでも定義できます。ただし、通知リスナーをローカルでないMBeanに追加することはできません。
managed-server-notifications-enabled
属性は、WLSTを使用して、次のように設定できます。edit() startEdit() cd("JMX/domain-name") cmo.setManagedServerNotificationsEnabled(false) activate()
-
ランタイムMBeanサーバーへのローカル接続の作成
ローカル・クライアントは、JMXServiceURL
オブジェクトを作成するのではなく、JNDIツリーを介してWebLogic ServerインスタンスのランタイムMBeanサーバーにアクセスできます。
ノート:
「ドメイン実行時MBeanサーバーへのローカル接続の作成」の説明に従って、ローカル・クライアントは、JNDIツリーを介してWebLogic Serverのドメイン実行時MBeanサーバーにもアクセスできます。
JNDIからアクセスされると、ランタイムMBeanサーバーはそのjavax.management.MBeanServer
インタフェースを返します。このインタフェースには、MBeanServerConnection
インタフェースのすべてのメソッドと、ローカル・プロセスがカスタムMBeanの登録に使用できるregisterMBean()
などの追加のメソッドがあります。(Java SE 8 API仕様のMBeanServer
(http://docs.oracle.com/javase/8/docs/api/javax/management/MBeanServer.html
)を参照してください。)
JMXクライアントのクラスが、EJBまたはWebアプリケーションなどのJava EEモジュールにある場合、ランタイムMBeanServerのJNDI名は次のようになります。
java:comp/weblogic/jmx/runtime
たとえば:
InitialContext ctx = new InitialContext(); server = (MBeanServer)ctx.lookup("java:comp/weblogic/jmx/runtime");
JMXクライアントのクラスがJava EEモジュールの一部ではない場合、ランタイムMBeanサーバーのJNDI名は次のようになります。
java:comp/jmx/runtime
ノート:
java:comp/env namespace
でJNDIバインドを自動的に作成できません。したがってWebLogic Server 12.2.1を起動すると、既存のjava:comp/env/jmx
バインドは次の新しいバインドに置き換えられます。
-
java:comp/env/jmx/runtime
はjava:comp/weblogic/jmx/runtime
に置き換えられます -
java:comp/env/jmx/domainRuntime
はjava:comp/weblogic/jmx/domainRuntime
に置き換えられます -
java:comp/env/jmx/edit
はjava:comp/weblogic/jmx/edit
に置き換えられます
java:comp/env/jmx/runtime
バインド、java:comp/env/jmx/domainRuntime
バインドおよびjava:comp/env/jmx/edit
バインドは存続します。ただし、JNDIインタフェースを使用してそれらを表示しようとしても表示されません。これらの非推奨バインドにアクセスするアプリケーションによってそのバインドを見つけるには、そこにバインドされているオブジェクトのルックアップを実行します。
ドメイン・ランタイムMBeanサーバーへのローカル接続の作成
ローカル・クライアントは、JMXServiceURL
オブジェクトを作成するのではなく、JNDIツリーを介してWebLogic Serverインスタンスのドメイン・ランタイムMBeanサーバーにアクセスできます。
JNDIからアクセスされると、ドメイン・ランタイムMBeanサーバーはそのjavax.management.MBeanServer
インタフェースを返します。このインタフェースには、MBeanServerConnection
インタフェースのすべてのメソッドと、ローカル・プロセスがカスタムMBeanの登録に使用できるregisterMBean()
などの追加のメソッドの他にgetMBeanCount()
、instatiate()
、getClassLoader()
などのメソッドもあります。(Java SE 8 API仕様のMBeanServer
(http://docs.oracle.com/javase/8/docs/api/javax/management/MBeanServer.html
)を参照してください。)
ノート:
ベスト・プラクティスとして、Oracleでは、ドメイン・ランタイムMBeanサーバーをドメイン全体の操作を実行するMBeanにのみ使用することをお薦めします。MBeanの処理とネットワーク・アクティビティにより管理サーバーの処理速度が低下し、管理操作の開始が回避されていないことを確認してください。
JMXクライアントのクラスが、EJBまたはWebアプリケーションなどのJava EEモジュールにある場合、ドメイン・ランタイムMBeanServerのJNDI名は次のようになります。
java:comp/weblogic/jmx/domainRuntime
たとえば:
InitialContext ctx = new InitialContext(); server = (MBeanServer)ctx.lookup("java:comp/weblogic/jmx/domainRuntime");
JMXクライアントのクラスがJava EEモジュールの一部ではない場合、ドメイン・ランタイムMBeanサーバーのJNDI名は次のようになります。
java:comp/jmx/domainRuntime
ドメイン・ランタイムMBeanサーバーは、管理サーバー上にのみ存在します。呼出しctx.lookup()
は、ローカルMBeanServerに参照を返すため、ルックアップ・メソッドは管理サーバー上で実行する場合にのみ呼び出すことができます。管理対象サーバー上で実行するときに呼び出された場合、NameNotFound
例外がスローされます。
MBean階層内の移動
WebLogic Serverは、MBeanを階層データ・モデルにまとめます。(WebLogic Server MBeanのデータ・モデルを参照してください。)このモデルでは、すべての親MBeanにその子のオブジェクト名を含んだ属性が含まれています。標準のJMX APIで子のオブジェクト名を使用して、子MBeanの属性の値を取得または設定したり、そのメソッドを呼び出したりします。
WebLogic Server MBean階層を移動するには、次の手順に従います。
MBean階層でのMBeanの場所を特定するには、Oracle WebLogic Server MBeanリファレンスでそのMBeanの説明を参照してください。Oracle WebLogic Server MBeanリファレンスでは、各MBeanについて、現在のMBeanのファクトリ・メソッドを保持する親MBeanが記載されています。ファクトリ・メソッドがパブリックでないMBeanについては、Oracle WebLogic Server MBeanリファレンスに、現在のMBeanにアクセスできる他のMBeanが記載されています。
例:サーバーの名前と状態の出力
例4-2のサンプル・コードでは、ドメイン・ランタイムMBeanサーバーに接続し、DomainRuntimeServiceMBean
を使用してドメイン内の各ServerRuntimeMBean
のオブジェクト名を取得します。その後、各サーバーのServerRuntimeMBean
のName
属性およびState
属性の値を取得し、出力します。
例4-2のコードに関する以下の点に留意してください。
-
クラスは、
connection
グローバル変数とconnector
グローバル変数に加えて、WebLogic ServerサービスMBeanのオブジェクト名をグローバル変数に割り当てます。クラス内のメソッドはこのオブジェクト名を頻繁に使用します。このオブジェクト名は一度定義したら、変更する必要はありません。 -
printServerRuntimes()
メソッドは、ドメイン内のすべてのServerRuntimeMBean
インスタンスの配列を保持するDomainRuntimeServiceMBean
のServerRuntimes
属性の値を取得します。(Oracle WebLogic Server MBeanリファレンスのDomainRuntimeServiceMBeanを参照してください。)
例4-2 例:サーバーの名前と状態の出力
import java.io.IOException; import java.net.MalformedURLException; import java.util.Hashtable; import javax.management.MBeanServerConnection; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import javax.naming.Context; public class PrintServerState { private static MBeanServerConnection connection; private static JMXConnector connector; private static final ObjectName service; // Initializing the object name for DomainRuntimeServiceMBean // so it can be used throughout the class. static { try { service = new ObjectName( "com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean"); }catch (MalformedObjectNameException e) { throw new AssertionError(e.getMessage()); } } /* * Initialize connection to the Domain Runtime MBean Server */ public static void initConnection(String hostname, String portString, String username, String password) throws IOException, MalformedURLException { String protocol = "t3"; Integer portInteger = Integer.valueOf(portString); int port = portInteger.intValue(); String jndiroot = "/jndi/"; String mserver = "weblogic.management.mbeanservers.domainruntime"; JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver); Hashtable h = new Hashtable(); h.put(Context.SECURITY_PRINCIPAL, username); h.put(Context.SECURITY_CREDENTIALS, password); h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote"); connector = JMXConnectorFactory.connect(serviceURL, h); connection = connector.getMBeanServerConnection(); } /* * Print an array of ServerRuntimeMBeans. * This MBean is the root of the runtime MBean hierarchy, and * each server in the domain hosts its own instance. */ public static ObjectName[] getServerRuntimes() throws Exception { return (ObjectName[]) connection.getAttribute(service, "ServerRuntimes"); } /* * Iterate through ServerRuntimeMBeans and get the name and state */ public void printNameAndState() throws Exception { ObjectName[] serverRT = getServerRuntimes(); System.out.println("got server runtimes"); int length = (int) serverRT.length; for (int i = 0; i < length; i++) { String name = (String) connection.getAttribute(serverRT[i], "Name"); String state = (String) connection.getAttribute(serverRT[i], "State"); System.out.println("Server name: " + name + ". Server state: " + state); } } public static void main(String[] args) throws Exception { String hostname = args[0]; String portString = args[1]; String username = args[2]; String password = args[3]; PrintServerState s = new PrintServerState(); initConnection(hostname, portString, username, password); s.printNameAndState(); connector.close(); } }
例:サーブレットのモニター
Webアプリケーションの各サーブレットは、サーブレットのランタイムの状態についての情報を保持するServletRuntimeMBean
のインスタンスを提供します。(Oracle WebLogic Server MBeanリファレンスのServletRuntimeMBeanを参照してください。)
WebLogic Serverのデータ・モデルでは、ServletRuntimeMBean
へのパスは次のようになります。
例4-3 サーブレットのモニター
import java.io.IOException; import java.net.MalformedURLException; import java.util.Hashtable; import javax.management.MBeanServerConnection; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import javax.naming.Context; public class MonitorServlets { private static MBeanServerConnection connection; private static JMXConnector connector; private static final ObjectName service; // Initializing the object name for DomainRuntimeServiceMBean // so it can be used throughout the class. static { try { service = new ObjectName( "com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean"); }catch (MalformedObjectNameException e) { throw new AssertionError(e.getMessage()); } } /* * Initialize connection to the Domain Runtime MBean Server */ public static void initConnection(String hostname, String portString, String username, String password) throws IOException, MalformedURLException { String protocol = "t3"; Integer portInteger = Integer.valueOf(portString); int port = portInteger.intValue(); String jndiroot = "/jndi/"; String mserver = "weblogic.management.mbeanservers.domainruntime"; JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver); Hashtable h = new Hashtable(); h.put(Context.SECURITY_PRINCIPAL, username); h.put(Context.SECURITY_CREDENTIALS, password); h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote"); connector = JMXConnectorFactory.connect(serviceURL, h); connection = connector.getMBeanServerConnection(); } /* * Get an array of ServerRuntimeMBeans */ public static ObjectName[] getServerRuntimes() throws Exception { return (ObjectName[]) connection.getAttribute(service, "ServerRuntimes"); } /* * Get an array of WebAppComponentRuntimeMBeans */ public void getServletData() throws Exception { ObjectName[] serverRT = getServerRuntimes(); int length = (int) serverRT.length; for (int i = 0; i < length; i++) { ObjectName[] appRT = (ObjectName[]) connection.getAttribute(serverRT[i], "ApplicationRuntimes"); int appLength = (int) appRT.length; for (int x = 0; x < appLength; x++) { System.out.println("Application name: " + (String)connection.getAttribute(appRT[x], "Name")); ObjectName[] compRT = (ObjectName[]) connection.getAttribute(appRT[x], "ComponentRuntimes"); int compLength = (int) compRT.length; for (int y = 0; y < compLength; y++) { System.out.println(" Component name: " + (String)connection.getAttribute(compRT[y], "Name")); String componentType = (String) connection.getAttribute(compRT[y], "Type"); System.out.println(componentType.toString()); if (componentType.toString().equals("WebAppComponentRuntime")){ ObjectName[] servletRTs = (ObjectName[]) connection.getAttribute(compRT[y], "Servlets"); int servletLength = (int) servletRTs.length; for (int z = 0; z < servletLength; z++) { System.out.println(" Servlet name: " + (String)connection.getAttribute(servletRTs[z], "Name")); System.out.println(" Servlet context path: " + (String)connection.getAttribute(servletRTs[z], "ContextPath")); System.out.println(" Invocation Total Count : " + (Object)connection.getAttribute(servletRTs[z], "InvocationTotalCount")); } } } } } } public static void main(String[] args) throws Exception { String hostname = args[0]; String portString = args[1]; String username = args[2]; String password = args[3]; MonitorServlets s = new MonitorServlets(); initConnection(hostname, portString, username, password); s.getServletData(); connector.close(); } }
例4-3のコードでは、上記の節で説明したように階層を移動し、ServletRuntimeMBean
属性の値を取得します。