0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

目的

RestAPIでLibertyの状態を確認するのに、毎回同じ事を調べてしまいます。簡単にできるアプリを作成します。

プロジェクト作成

前の記事で詳細を書いたので、ここでは自分がよく使う手順を簡潔にまとめていきます。

mvn archetype:generate

libertyでfilter, io.openliberty.tools:liberty-archetype-webapp (-)を選択

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 2356: liberty
Choose archetype:
1: remote -> io.openliberty.tools:liberty-archetype-ear (An archetype that generates a Java EE 8 multi-module project that includes an EJB module, a web application module and an EAR module)
2: remote -> io.openliberty.tools:liberty-archetype-webapp (-)
3: remote -> io.openliberty.tools:liberty-plugin-archetype (-)
4: remote -> net.wasdev.maven.tools.archetypes:ejb-jee5-liberty (EJB 3.0 project targeting WebSphere Liberty)
5: remote -> net.wasdev.maven.tools.archetypes:ejb-jee6-liberty (EJB 3.1 project targeting WebSphere Liberty)
6: remote -> net.wasdev.maven.tools.archetypes:ejb-jee7-liberty (EJB 3.2 project targeting WebSphere Liberty)
7: remote -> net.wasdev.maven.tools.archetypes:osgi-liberty (OSGi project targeting WebSphere Liberty)
8: remote -> net.wasdev.maven.tools.archetypes:osgi-web25-liberty (OSGi Web 2.5 project targeting WebSphere Liberty)
9: remote -> net.wasdev.maven.tools.archetypes:osgi-web30-liberty (OSGi Web 3.0 project targeting WebSphere Liberty)
10: remote -> net.wasdev.maven.tools.archetypes:osgi-web31-liberty (OSGi Web 3.1 project targeting WebSphere Liberty)
11: remote -> net.wasdev.maven.tools.archetypes:webapp-jee5-liberty (Web 2.5 project targeting WebSphere Liberty)
12: remote -> net.wasdev.maven.tools.archetypes:webapp-jee6-liberty (Web 3.0 project targeting WebSphere Liberty)
13: remote -> net.wasdev.maven.tools.archetypes:webapp-jee7-liberty (Web 3.1 project targeting WebSphere Liberty)
14: remote -> net.wasdev.wlp.maven:liberty-archetype-ear (An archetype that generates a Java EE 7 multi-module project that includes an EJB module, a web application module and an EAR module)
15: remote -> net.wasdev.wlp.maven:liberty-archetype-webapp (-)
16: remote -> net.wasdev.wlp.maven:liberty-plugin-archetype (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 

pom.xml の更新 (openlibertyは導入済みのものを使うこだわり)

  • io.openliberty.tools のものを入れ替える
  • installDirectoryを変更する
  • serverNameを変更する
pom.xml
<plugin>
    <groupId>io.openliberty.tools</groupId>
    <artifactId>liberty-maven-plugin</artifactId>
    <version>3.11.4</version>
    <extensions>true</extensions>
    <configuration>
        <installDirectory>/home/keniooi/wlp</installDirectory>
        <serverName>mbeans-svr</serverName>
        <looseApplication>true</looseApplication>
        <deployPackages>all</deployPackages>
    </configuration>
</plugin>

Libertyサーバーの構成

server.xml の更新(よく使うfeatureをまとめて追加)をします。

  • datasource
  • jms
  • restConnector
  • adminCenter
server.xml
<server description="Sample Servlet server">
    <featureManager>
        <feature>javaee-7.0</feature>
        <feature>restConnector-2.0</feature>
        <feature>adminCenter-1.0</feature>
    </featureManager>
    
    <httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint" host="*" />
    <ssl id="defaultSSLConfig" verifyHostname="false" />
    
    <basicRegistry id="basic">
        <user name="wsadmin" password="passw0rd" />
        <user name="reader" password="passw0rd" />
    </basicRegistry>
    <administrator-role>
        <user>wsadmin</user>
    </administrator-role>
    <reader-role>
        <user>reader</user>
    </reader-role>

    <fileset dir="/output/resources/db2/" id="Db2Files" includes="*.jar"/>
    <library filesetRef="Db2Files" id="Db2Lib"/>
    <jdbcDriver id="Db2Driver" libraryRef="Db2Lib"/>
    <!-- jndiName derby is from original application which is developed with derby. -->
    <dataSource id="PdprofDataSource" jdbcDriverRef="Db2Driver" jndiName="jdbc/derbyEmbedded" type="javax.sql.XADataSource" queryTimeout="112s" syncQueryTimeoutWithTransactionTimeout="false">
        <connectionManager maxPoolSize="5" minPoolSize="2"/>
        <properties.db2.jcc databaseName="PDPROF" 
                      serverName="localhost" portNumber="50000"
                      user="db2inst1" password="passw0rd"/>
    </dataSource>

    <!-- curl -O https://repo1.maven.org/maven2/com/ibm/mq/wmq.jmsra/9.4.5.1/wmq.jmsra-9.4.5.1.rar -->
    <resourceAdapter id="mqJms" location="/output/resources/mq/wmq.jmsra.rar">
        <classloader apiTypeVisibility="spec, ibm-api, api, third-party"/>       
    </resourceAdapter>

    <jmsActivationSpec authDataRef="appAuth" id="jms/PdprofMdb">
		<properties.mqJms channel="DEV.APP.SVRCONN" destinationRef="Q3" hostName="localhost" port="1414" queueManager="QM1" transportType="CLIENT"/>
	</jmsActivationSpec>
	
	<jmsQueueConnectionFactory connectionManagerRef="ConMgr6" containerAuthDataRef="appAuth" jndiName="QCF">
		<properties.mqJms channel="DEV.APP.SVRCONN" hostName="localhost" port="1414" queueManager="QM1" transportType="CLIENT"/>
	</jmsQueueConnectionFactory>
	<connectionManager id="ConMgr6" maxPoolSize="5"/>
	<authData id="appAuth" password="passw0rd" user="app"/>

	<jmsQueue jndiName="Q1">
		<properties.mqJms baseQueueManagerName="QM1" baseQueueName="DEV.QUEUE.1"/>
	</jmsQueue>

	<jmsQueue id="Q2" jndiName="Q2">
		<properties.mqJms baseQueueManagerName="QM1" baseQueueName="DEV.QUEUE.2"/>
	</jmsQueue>

	<jmsQueue id="Q3" jndiName="Q3">
		<properties.mqJms baseQueueManagerName="QM1" baseQueueName="DEV.QUEUE.3"/>
	</jmsQueue>
    
    <webApplication id="mbeans" location="mbeans.war" name="mbeans">
        <application-bnd>
    		<security-role name="admin-role">
    			<user name="wsadmin"/>
    		</security-role>
    	</application-bnd>
    </webApplication>

    <enterpriseApplication id="datasource" location="datasource.ear" name="datasource">
    	<application-bnd>
    		<security-role name="admin-role">
    			<user name="wsadmin"/>
    		</security-role>
    	</application-bnd>
    </enterpriseApplication>

    <webApplication id="jms" location="jms.war" name="jms"/>

</server>

アプリケーションを更新

実装したい内容にあわせて更新していくのみのため省略

MBeansアプリケーション

作成したアプリケーションを紹介します。JMX管理からAPIテストまでを1画面でできる3カラムJMX/APIクライアントです。URLのコピーペースト、cURLクライアントの引数の調整など煩雑な作業を減らすことができます。

画面構成

image.png

左カラム:API HELP

APIの仕様やエンドポイントのHELP情報を常時表示。実装を確認しながら作業を進められます。

中央カラム:MBeans & URL探索

サーバー内のMBeansリストや利用可能なURLリストを効率的に取得・可視化します。

右カラム:POSTクライアント

中央で見つけたURLをコピーして貼り付ける事で、その場ですぐにPOSTリクエストを送信・検証できます。cURLで実行する場合の同等引数も確認できます。

右上のボタン

左カラム、右カラムの表示、非表示を選択できます。

アクセスURL

以下のURLです。localhost:9443 は環境にあわせて変更します。

環境の構築

git repository をコピーします。

レポジトリコピー
git clone https://github.com/pdprof/mbeans.git
cd mbeans/mbeans-docker

セットアップスクリプトの実行

セットアップ
export ACCESS_HOST=172.17.0.1
# export ACCESS_HOST=`hostname` # hostnameコマンドで返す値でアクセスできる場合
./setup-docker.sh

Db2 の起動

./db2-start.sh

MQ の起動

./mq-start.sh

Libertyの起動

./mbeans-start.sh

アプリケーションにアクセスして動作確認

ユーザー: wsadmin
パスワード: passw0rd

使用例

1 中央カラムで接続プールのMBeanを検索します。文字入力で対象を限定
image.png

2 GETボタンを押しMBeanで使用できるURLを取得
image.png

3 operations/showPoolContents のURLを選択
image.png

4 COPYボタンを押しURLをコピー
image.png

5 右カラムのURLにペースト
image.png

6 中央カラムで引数を確認 (signatureで引数の数、型を確認)

showPoolContents.json
{
    "name" : "showPoolContents",
    "description" : "Displays the current contents of the Connection Manager in a human readable format.",
    "descriptor" : {
      "names" : [ ],
      "values" : [ ]
    },
    "impact" : "0",
    "returnType" : "java.lang.String",
    "signature" : [ ],
    "URL" : "/IBMJMXConnectorREST/mbeans/WebSphere%3AjndiName%3Djdbc%2FderbyEmbedded%2Cname%3DdataSource%5BPdprofDataSource%5D%2FconnectionManager%5Bdefault-0%5D%2Ctype%3Dcom.ibm.ws.jca.cm.mbean.ConnectionManagerMBean/operations/showPoolContents"
}

7 左カラムで引数にあわせたPOSTするJSONデータを確認 0 params のため {} のみ
image.png

7.1 引数に何を指定するかはAPI文書を確認(今回はConnectionManagerMBean)

8 右カラムでPOSTデータを入力
image.png

9 cURLで実行するときのためにシェルの環境変数をコピー(オプション)

環境にあわせて値は変更
export ADMIN=wsadmin:passw0rd
export SERVER_URL=https://localhost:9443

10 METHODをPOSTに変えて、SENDボタンをクリック
image.png

11 Responseを確認(maxPoolSizeがとれてるので成功)
image.png

12 出力されたcURLコマンドラインをコピー(オプション)

curl -u $ADMIN -k -X "POST" \
  -H "Content-Type: application/json" \
  -d '{}'  \
  $SERVER_URL/IBMJMXConnectorREST/mbeans/WebSphere%3AjndiName%3Djdbc%2FderbyEmbedded%2Cname%3DdataSource%5BPdprofDataSource%5D%2FconnectionManager%5Bdefault-0%5D%2Ctype%3Dcom.ibm.ws.jca.cm.mbean.ConnectionManagerMBean/operations/showPoolContents

13 cURLコマンド実行(\$ADMIN, \$SERVER_URLの調整が必要です)

$ curl -u $ADMIN -k -X "POST" \
  -H "Content-Type: application/json" \
  -d '{}'  \
  $SERVER_URL/IBMJMXConnectorREST/mbeans/WebSphere%3AjndiName%3Djdbc%2FderbyEmbedded%2Cname%3DdataSource%5BPdprofDataSource%5D%2FconnectionManager%5Bdefault-0%5D%2Ctype%3Dcom.ibm.ws.jca.cm.mbean.ConnectionManagerMBean/operations/showPoolContents | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   319  100   317  100     2   6604     41 --:--:-- --:--:-- --:--:--  6510
{
  "value": "PoolManager@1a2da4f2\nname=WebSphere:type=com.ibm.ws.jca.cm.mbean.ConnectionManagerMBean,jndiName=jdbc/derbyEmbedded,name=dataSource[PdprofDataSource]/connectionManager[default-0]\njndiName=jdbc/derbyEmbedded\nmaxPoolSize=5\nsize=0\nwaiting=0\nunshared=0\nshared=0\navailable=0\n",
  "type": "java.lang.String"
}

トラブルシューティング

接続がうまくいかない場合は、/config/server.xml で指定したホスト名(IPアドレス)が問題ないか、SSLの接続エラーが出ていないかなどを確認します。

docker logs -f mbeans # ログ確認
docker restart mbeans # 再起動
docker inspect mbeans # container 確認
docker exec -it mbeans bash # Container内状況確認

まとめ

煩雑な処理になりがちなMBeanのREST APIをブラウザ上で簡単に試せるようになりました。GUIだけでは共有には向かないですが、cURLコマンドとしてテキストに残せるようになりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?