<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>asyd's blog &#187; jmx</title>
	<atom:link href="http://blog.asyd.net/tag/jmx/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.asyd.net</link>
	<description>unix forever, for everyone. Not convinced, try Mac OS X!</description>
	<lastBuildDate>Wed, 21 Jul 2010 14:26:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Configure glassfish via JMX and Groovy</title>
		<link>http://blog.asyd.net/2009/05/configure-glassfish-via-jmx-and-groovy/</link>
		<comments>http://blog.asyd.net/2009/05/configure-glassfish-via-jmx-and-groovy/#comments</comments>
		<pubDate>Tue, 19 May 2009 15:31:55 +0000</pubDate>
		<dc:creator>asyd</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Sysadmin]]></category>
		<category><![CDATA[glassfish]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[jmx]]></category>

		<guid isPermaLink="false">http://blog.asyd.net/?p=311</guid>
		<description><![CDATA[At my new work, we have a lot of glassfish in production. For some of them, we even have almost 10 instances per DAS (Domain Admin Server). As a (good) system administrator, I&#8217;m a lazy man. After having to installed and created a glassfish with 10 instances, I was really tired to click, click, and [...]]]></description>
			<content:encoded><![CDATA[<p>At my new <a href="http://www.rtl.fr/">work</a>, we have a lot of glassfish in production. For some of them, we even have almost 10 instances per DAS (Domain Admin Server). As a (good) system administrator, I&#8217;m a lazy man. After having to installed and created a glassfish with 10 instances, I was really tired to click, click, and click again through the web interface. Yes I know, it&#8217;s possible to edit the files directly, but I think it&#8217;s not very convenient. First, it&#8217;s easy to make mistakes, it&#8217;s not atomic, and finally you need to restart/reload the instance.</p>
<p>So, I was wondering if it&#8217;s possible to proceed some configuration using JMX/<a href="https://glassfish.dev.java.net/javaee5/amx/docs/amx-impl.html">AMX</a> (AppServer Management Extensions). I start to browse MBeans using JConsole, and check that&#8217;s really possible to modify the configuration, and the answer is yes. Ok, my first try is a simple one, I just want enable access.log, meaning set accessLoggingEnabled property to true for each http-service. Since I&#8217;m not really good to write Java, I wrote a groovy script. Here it comes :</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">javax.management.ObjectName</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">javax.management.remote.JMXConnectorFactory</span> <span style="color: #000000; font-weight: bold;">as</span> JmxFactory
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">javax.management.remote.JMXServiceURL</span> <span style="color: #000000; font-weight: bold;">as</span> JmxUrl
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">javax.management.MBeanServerConnection</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> uri <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'service:jmx:rmi:///jndi/rmi://localhost:8686/jmxrmi'</span>
<span style="color: #000000; font-weight: bold;">def</span> login <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'admin'</span>
<span style="color: #000000; font-weight: bold;">def</span> password <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'adminadmin'</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">javax.management.Query</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> jmxurl <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JmxUrl<span style="color: #66cc66;">&#40;</span>uri<span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">def</span> attributes <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Hashtable</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> buffer <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span> login, password <span style="color: #66cc66;">&#93;</span>
&nbsp;
attributes.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;jmx.remote.credentials&quot;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> buffer<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> server <span style="color: #66cc66;">=</span> JmxFactory.<span style="color: #006600;">connect</span><span style="color: #66cc66;">&#40;</span>jmxurl, attributes<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">MBeanServerConnection</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> enableAccessLog <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> serviceUri <span style="color: #66cc66;">-&gt;</span>
    properties <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;accessLoggingEnabled&quot;</span>, <span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #66cc66;">&#93;</span>
    <span style="color: #000000; font-weight: bold;">def</span> signature <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;java.lang.String&quot;</span>, <span style="color: #ff0000;">&quot;java.lang.String&quot;</span><span style="color: #66cc66;">&#93;</span>
    server.<span style="color: #006600;">invoke</span><span style="color: #66cc66;">&#40;</span>serviceUri, <span style="color: #ff0000;">&quot;setPropertyValue&quot;</span>, properties <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #aaaadd; font-weight: bold;">Object</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>, signature <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #aaaadd; font-weight: bold;">String</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> checkAccessLog <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> serviceUri <span style="color: #66cc66;">-&gt;</span>
    <span style="color: #000000; font-weight: bold;">def</span> properties <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;accessLoggingEnabled&quot;</span> <span style="color: #66cc66;">&#93;</span>
    <span style="color: #000000; font-weight: bold;">def</span> answer <span style="color: #66cc66;">=</span> server.<span style="color: #006600;">invoke</span><span style="color: #66cc66;">&#40;</span>serviceUri, <span style="color: #ff0000;">&quot;getPropertyValue&quot;</span>, properties <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #aaaadd; font-weight: bold;">Object</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">&quot;java.lang.String&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;Access log for: &quot;</span> <span style="color: #66cc66;">+</span> serviceUri <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">&quot; enabled: &quot;</span> <span style="color: #66cc66;">+</span> answer
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> query <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ObjectName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;amx:*,j2eeType=X-HTTPServiceConfig&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
server.<span style="color: #006600;">queryNames</span><span style="color: #66cc66;">&#40;</span>query, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> service <span style="color: #66cc66;">-&gt;</span>
    checkAccessLog<span style="color: #66cc66;">&#40;</span>service<span style="color: #66cc66;">&#41;</span>
    enableAccessLog<span style="color: #66cc66;">&#40;</span>service<span style="color: #66cc66;">&#41;</span>
    checkAccessLog<span style="color: #66cc66;">&#40;</span>service<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>That&#8217;s magic&#8230; Sure I&#8217;ll post some others script to manage more complex configuration, like create a connection pool, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.asyd.net/2009/05/configure-glassfish-via-jmx-and-groovy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
