かつのりの日記2

わんくまでは珍しいJavaを中心とした日記です

目次

Blog 利用状況

書庫

日記カテゴリ

いろいろリンク

MBeanServerでプロパティを変更

Webアプリを運用するときに、ストップレスで設定を変更したい事があると思いますが、そのためにDBを使うのは面倒ですし、パフォーマンスの影響もあります。オンメモリでキャッシュするという運用もありますが、どのみち設定用の管理画面が必要になります。これを作るのは面倒になりますよね。

JavaのJDK5からはJMX(Java Management eXtension)が標準で装備されています。これはシステム管理を目的としているAPIで、MBean(Management Bean)と呼ばれる管理情報を作成する為のAPIと、そのMBeanを管理するAPIで構成されています。

JMXはApache GeronimoやJBossのサーバ管理機能で使われています。身近なところではTomcatの管理にも使われています。Java本体でもJVMの管理の為に提供されていますが、このMBeanはjava.lang.managementパッケージで提供されています。

前置きが長くなりましたが、MBeanが登録されたMBeanServerを操作するために、SunのJDKではJConsoleと呼ばれるツールが付属しています。Webアプリの設定情報もMBeanで管理するようにすれば、JConsoleで設定変更する事ができるのです。これなら設定を管理する画面を作る必要がありません。

そこでJavaのプロパティを示すPropertiesクラスがありますが、それのMBean化と、利用例が以下のコードになります。

(前のブログでも書いたのですが、放置されているのでこちらにコードを持ってきます。。。)

まずMBean化されたPropertiesです。Propertiesを継承します。

import java.util.Properties;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanOperationInfo;
import javax.management.ReflectionException;

public class PropertiesMBean extends Properties implements DynamicMBean {

	public PropertiesMBean() {
		super();
	}

	public PropertiesMBean(Properties properties) {
		super(properties);
	}

	public Object getAttribute(String attribute)
			throws AttributeNotFoundException, MBeanException,
			ReflectionException {
		return this.get(attribute);
	}

	public AttributeList getAttributes(String[] attributes) {
		AttributeList list = new AttributeList();
		for (int i = 0; i < attributes.length; i++) {
			Object value = null;
			try {
				value = getAttribute(attributes[i]);
			} catch (Exception e) {
			}
			list.add(new Attribute(attributes[i], value));
		}

		return list;
	}

	public MBeanInfo getMBeanInfo() {
		MBeanNotificationInfo[] notificationInfos = new MBeanNotificationInfo[0];
		MBeanOperationInfo[] operationInfos = new MBeanOperationInfo[0];
		MBeanConstructorInfo[] constructorInfos = new MBeanConstructorInfo[0];
		MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[this
				.size()];

		int i = 0;
		for (Object key : this.keySet()) {
			attributeInfos[i++] = new MBeanAttributeInfo(key.toString(),
					"java.lang.String", key.toString(), true, true, false);
		}

		return new MBeanInfo("PropertiesMBean", "Properties", attributeInfos,
				constructorInfos, operationInfos, notificationInfos);
	}

	public Object invoke(String actionName, Object[] params, String[] signature)
			throws MBeanException, ReflectionException {
		return null;
	}

	public void setAttribute(Attribute attribute)
			throws AttributeNotFoundException, InvalidAttributeValueException,
			MBeanException, ReflectionException {
		this.put(attribute.getName(), attribute.getValue());
	}

	public AttributeList setAttributes(AttributeList attributes) {
		AttributeList list = new AttributeList(attributes);

		for (int i = 0; i < attributes.size(); i++) {
			Attribute attribute = (Attribute) attributes.get(i);
			try {
				setAttribute(attribute);
			} catch (Exception e) {
				list.remove(attribute);
			}
		}

		return list;
	}
}

そして利用例です。

import java.lang.management.ManagementFactory;
import java.util.Properties;

import javax.management.MBeanServer;
import javax.management.ObjectName;

public class Test {

	public static void main(String[] args) throws Exception {
		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
		Properties properties = new PropertiesMBean();
		properties.setProperty("foo", "bar");
		ObjectName name = new ObjectName("test:type=properties");
		server.registerMBean(properties, name);
		while(true){
			System.out.println(properties.get("foo"));
			Thread.sleep(50);
		}
	}
}

 

上記サンプルではリモートAPIを利用していないので、JDK5の場合は起動オプションに、-Dcom.sun.management.jmxremoteをつける必要があります。それではTestクラスを起動してからJConsoleを起動してみます。JConsoleはJDKのインストールディレクトリの以下のbinの中に入っています。

Testクラスを起動した状態です。

image

JConsoleを起動します。この画面はJDK6のJconsoleです。

image 

こんな画面が立ち上がりますので、Testを選択してください。タブがあるのでMBeanを選択してツリーを展開すると、以下のようになります。

image

プロパティの状態が表示されていますね。これを変更すると、以下のように変わります。

image

image

設定がリモート経由でリニアに変更されているのが確認できると思います。これを応用すると、Webアプリでも利用する事が可能です。今回はリモートAPIを使っていないのでローカルホストからしか接続できませんが、リモートAPIを使う事によってネットワーク経由で操作することも可能です。

投稿日時 : 2007年8月31日 0:45

Feedback

# re: MBeanServerでプロパティを変更 2007/08/31 10:39 凪瀬

これは使えそうですね。
システム設計の段階で考慮に入れる価値は十分にありそうです。
VMのプロパティにcom.sun.~って設定するというのがちょっと嫌ですが。
Sunの独自拡張扱いなんですかね?Sun純正じゃないVMとかだとどうなるのかしらん。

# re: MBeanServerでプロパティを変更 2007/08/31 10:56 かつのり

確認していないですが、多分Sun独自かも。
JDK6からは設定不要です。

どのみちローカルからのアクセスしか標準では許さないので、
最初から設定不要にすればいいのにってことで、
JDK6からはデフォルトで使えるようになったみたいです。

自分のプロジェクトではリモート接続できるようにして、
さらにプログラム側から操作するためにResourceBundle化しています。
setAttributeでプロパティが変更されたタイミングで、
ファイルへ書き出すように改造してあるので、
設定が永続化されるような仕組みになっています。
改造可能なポイントは多々ありますね。

# ihUdjtqUnv 2011/12/17 19:18 http://www.funniest-joke.com

I`m so grateful that you enlightened me and the most important thing that it happened in time. Just think, I have been using the internet for six years already but it`s the first time I`ve ever heard about it!...

# oxYvVcJiCBz 2011/12/17 19:57 http://www.coursmecanique.com

It`s really useful! Looking through the Internet you can mostly observe watered down information, something like bla bla bla, but not here to my deep surprise. It makes me happy..!

# lsLGrrfhmio 2011/12/18 21:10 http://blackflats.info/

Received the letter. I agree to exchange the articles.

# sOQrPjyMlUAIYn 2013/03/22 23:30 http://crork.com/

CH722J Thanks for the blog article.Thanks Again. Really Great.

# XoBhKYzahf 2014/07/17 19:48 http://crorkz.com/

4lHnxG Major thankies for the article.Thanks Again. Want more.

# GLPjSccTUIvZ 2014/08/30 22:21 http://www.m88so.com/Main/Home.aspx?affiliateId=97

Usually I do not learn post on blogs, but I wish to say that this write-up very forced me to try and do it! Your writing taste has been surprised me. Thanks, very great article.

# ncHFpiNIDwABwTrlrh 2014/09/01 20:20 https://fifa14hackcoins.wordpress.com/

It's arduous to search out knowledgeable folks on this topic, however you sound like you recognize what you're talking about! Thanks

# GwAQrozGDKXjNCWmwSG 2014/09/06 19:48 http://www.investleaks.com

There is clearly a lot to know about this. I assume you made various good points in features also.

# wedhqmaTyTVZd 2014/09/09 12:25 http://vente-sur-internet.com/

Very good written post. It will be beneficial to anybody who utilizes it, including myself. Keep doing what you are doing - for sure i will check out more posts.

# zrlfNUHmHbSFTQF 2014/09/10 15:55 https://www.facebook.com/SunsetValleyHolidayHouses

I don't even know how I stopped up here, but I assumed this publish was great. I do not understand who you're however certainly you are going to a famous blogger if you aren't already ;) Cheers!

# eTdlSFMgkA 2014/09/12 19:50 http://www.youtube.com/watch?v=6eoaR-4GvzQ

There is perceptibly a lot to identify about this. I suppose you made various good points in features also.

# eqmoPmfekOiJQrKHf 2014/09/15 10:45 http://www.theboatonlinestore.com/

This site is mostly a stroll-via for all the data you needed about this and didn't know who to ask. Glimpse right here, and also you'll definitely discover it.

# bquhYNERbSLeEE 2014/09/17 6:49 http://theboatonlinestore.es/

I used to be recommended this website through my cousin. I am now not positive whether this post is written by him as no one else recognize such precise about my trouble. You are amazing! Thanks!

# Heya i am for the first time here. I came across this board and I find It really useful & it helped me out much. I hope to give something back and aid others like you helped me. 2019/04/19 16:08 Heya i am for the first time here. I came across t

Heya i am for the first time here. I came across this
board and I find It really useful & it helped me out much.

I hope to give something back and aid others like you helped me.

# Hello, everything is going sound here and ofcourse every one is sharing facts, that's in fact excellent, keep up writing. 2019/07/09 11:05 Hello, everything is going sound here and ofcourse

Hello, everything is going sound here and ofcourse every one is sharing facts, that's in fact excellent, keep up
writing.

# Hello, everything is going sound here and ofcourse every one is sharing facts, that's in fact excellent, keep up writing. 2019/07/09 11:06 Hello, everything is going sound here and ofcourse

Hello, everything is going sound here and ofcourse every one is sharing facts, that's in fact excellent, keep up
writing.

# Hello, everything is going sound here and ofcourse every one is sharing facts, that's in fact excellent, keep up writing. 2019/07/09 11:07 Hello, everything is going sound here and ofcourse

Hello, everything is going sound here and ofcourse every one is sharing facts, that's in fact excellent, keep up
writing.

# Hello, everything is going sound here and ofcourse every one is sharing facts, that's in fact excellent, keep up writing. 2019/07/09 11:08 Hello, everything is going sound here and ofcourse

Hello, everything is going sound here and ofcourse every one is sharing facts, that's in fact excellent, keep up
writing.

# Good day! This post could not be written any better! Reading through this post reminds me of my good old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing! 2019/07/20 13:14 Good day! This post could not be written any bette

Good day! This post could not be written any better!
Reading through this post reminds me of my good old room mate!
He always kept talking about this. I will forward
this article to him. Pretty sure he will have a
good read. Thanks for sharing!

# Hello there! This is my 1st comment here so I just wanted to give a quick shout out and say I genuinely enjoy reading your posts. Can you suggest any other blogs/websites/forums that deal with the same subjects? Thanks a ton! https://www.kinwiki.me/Use 2022/01/14 23:55 Hello there! This is my 1st comment here so I just

Hello there! This is my 1st comment here so I just wanted
to give a quick shout out and say I genuinely enjoy reading your posts.
Can you suggest any other blogs/websites/forums that deal with the
same subjects? Thanks a ton!

https://www.kinwiki.me/User:PorfirioDees02
http://www.ganymede.thecontinuum.ca/index.php?title=Twosday_2_22_22_Shirt_The_Best_Way_To_Handle_Each_Shirt_Jackets_Problem_With_Ease_Utilizing_The_Following_Tips
https://wiki2020.fri3d.be/index.php?title=Taco_Twosday_Shirt_Best_Shirt_Android_Apps
http://wiki.iht.hn/index.php?title=Twosday_2_22_22_Search_For_Skilled_T_Shirt_Printing_For_Excellent_And_Clear_Designs_-_Clothes
https://guiainformatica.net/wiki/index.php?title=Usuario:DelilaSiddons95
http://gearstation.tk/wiki/index.php?title=Twosday_Shirt_How_6_Issues_Will_Change_The_Best_Way_You_Approach_Shirt

# Greetings! I know this is kinda off topic nevertheless I'd figured I'd ask. Would you be interested in trading links or maybe guest authoring a blog post or vice-versa? My site addresses a lot of the same subjects as yours and I believe we could greatly 2022/04/06 8:41 Greetings! I know this is kinda off topic neverthe

Greetings! I know this is kinda off topic nevertheless I'd figured I'd ask.
Would you be interested in trading links or maybe guest authoring a blog post
or vice-versa? My site addresses a lot of the same
subjects as yours and I believe we could greatly benefit from each other.

If you happen to be interested feel free to shoot me an email.

I look forward to hearing from you! Excellent blog by the way!


https://wiki.epicmafia.org/index.php?title=User:StefanChv4
https://bbarlock.com/index.php/Are_You_Actually_Doing_Enough_Mortgage_Broker_License_In_California
https://bombadil.ling.washington.edu/wiki/index.php/User:AntoniettaMccart
https://wiki.r3public.org/index.php?title=User:SaundraDrew
https://advkey.quest/index.php/Learn_Anything_New_From_Mortgage_Lenders_In_San_Jose_Ca_Recently_We_Requested_You_Answered
http://classicalmusicmp3freedownload.com/ja/index.php?title=%E5%88%A9%E7%94%A8%E8%80%85:VeraSchneider7

タイトル
名前
Url
コメント