14 posts categorized "httpbuilder"

05/12/2009

Tomcat Management: Using the JMXProxy

One hidden gem bundled with the Tomcat Manager is the JMXProxy, which allows you to do a whole bunch of cool and frightening things with your tomcat container and running applications. It is enabled by default once the Tomcat Manager is enabled. You'll need more than a browser to interact with it, however, because simply loading http://localhost:8080/manager/jmxproxy dumps everything in regular text, which is useful for getting MBean names.

Below are some groovy scripts that will highlight some of the interesting things you can get from the JMXProxy.

Get Tomcat Thread Pool

#!/usr/bin/env groovy 

package groovyx.net.http
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder( 'http://localhost:8080/manager/jmxproxy/?qry=*:type=ThreadPool,name=http-8080' )
http.request( GET,TEXT ) { req ->
http.auth.basic( 'tomcat', 'tomcat' )
headers.'User-Agent' = 'GroovyHTTPBuilderTest/0.4'

response.success = { resp, reader ->
println "-----Response-----"
println System.out << reader
println "\n------------------"
}
response.failure = { resp ->
println "Something bad happened. ${resp.statusLine}"
}
}
The script above will print out some useful stats regarding Thread Pool statistics on the HTTP connector. Output looks something like the following:
-----Response-----
OK - Number of results: 1

Name: Catalina:type=ThreadPool,name=http-8080
modelerType: org.apache.tomcat.util.modeler.BaseModelMBean
maxThreads: 40
backlog: 100
currentThreadsBusy: 1
acceptorThreadCount: 1
threadPriority: 5
paused: false
port: 8080
tcpNoDelay: true
soLinger: -1
soTimeout: 20000
daemon: true
running: true
currentThreadCount: 6
name: http-8080

org.codehaus.groovy.runtime.FlushingStreamWriter@2580b3

------------------
Are you interested in connector-related statistics? Well, the following script will provide all the useful statistics exposed by the HTTP Connector.

Get Tomcat Connector Statistics

#!/usr/bin/env groovy 

package groovyx.net.http
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder( 'http://localhost:8080/manager/jmxproxy/?qry=*:type=Connector,port=8080' )
http.request( GET,TEXT ) { req ->
http.auth.basic( 'tomcat', 'tomcat' )
headers.'User-Agent' = 'GroovyHTTPBuilderTest/0.4'

response.success = { resp, reader ->
println "-----Response-----"
println System.out << reader
println "\n------------------"
}
response.failure = { resp ->
println "Something bad happened. ${resp.statusLine}"
}
}
Output will look similar to the following:
-----Response-----
OK - Number of results: 1

Name: Catalina:type=Connector,port=8080
modelerType: org.apache.catalina.mbeans.ConnectorMBean
connectionUploadTimeout: 300000
keepAliveTimeout: -1
protocol: HTTP/1.1
maxThreads: 40
acceptCount: 100
connectionTimeout: 20000
connectionLinger: -1
scheme: http
port: 8080
tcpNoDelay: true
xpoweredBy: false
compression: off
maxHttpHeaderSize: 8192
proxyPort: 0
secure: false
maxPostSize: 2097152
redirectPort: 8443
bufferSize: 2048
emptySessionPath: false
threadPriority: 5
useBodyEncodingForURI: false
protocolHandlerClassName: org.apache.coyote.http11.Http11Protocol
enableLookups: false
allowTrace: false
maxKeepAliveRequests: 100
disableUploadTimeout: true

org.codehaus.groovy.runtime.FlushingStreamWriter@2580b3

------------------

List Running Servlets

#!/usr/bin/env groovy 


package groovyx.net.http
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder( 'http://localhost:8080/manager/jmxproxy/?qry=*:j2eeType=Servlet,*' )
http.request( GET,TEXT ) { req ->
http.auth.basic( 'tomcat', 'tomcat' )
headers.'User-Agent' = 'GroovyHTTPBuilderTest/0.4'

response.success = { resp, reader ->
println "-----Response-----"
println System.out << reader
println "\n------------------"
}
response.failure = { resp ->
println "Something bad happened. ${resp.statusLine}"
}
}
There is an awful lot of output for this one so I will not be including the output for this particular script in this article.

Don't assume that interaction with the jmxproxy is all 'getting' and no 'setting'. This article dealt with the cool things you can do with the jmxproxy—the next one will deal with the frightening things.

05/08/2009

Tomcat Management: Use Groovy to Interact with Tomcat Manager

With the Tomcat Manager enabled, you can script a whole bunch of interesting ways to interact with your tomcat server. I've been using the HTTPBuilder module for Groovy a lot lately to get certain statistics from my Tomcat Manager enabled Tomcat servers. Below are some examples.

The first example is a basic script that gets ServerInfo from tomcat. It, and the following scripts, also demonstrate how to pass basic authentication over http requests with HTTPBuilder.

Get Tomcat ServerInfo Script

#!/usr/bin/env groovy 


package groovyx.net.http
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder( 'http://localhost:8080/manager/serverinfo' )
http.request( GET,TEXT ) { req ->
http.auth.basic( 'tomcat', 'tomcat' )
headers.'User-Agent' = 'GroovyHTTPBuilderTest/0.4'

response.success = { resp, reader ->
println "-----Response-----"
println System.out << reader
println "\n------------------"
}
response.failure = { resp ->
println "Something bad happened. ${resp.statusLine}"
}
}
Output will look similar to the following:
-----Response-----
OK - Server info
Tomcat Version: Apache Tomcat/6.0.18
OS Name: Mac OS X
OS Version: 10.5.6
OS Architecture: i386
JVM Version: 1.5.0_16-b06-284
JVM Vendor: Apple Inc.
org.codehaus.groovy.runtime.FlushingStreamWriter@2580b3

------------------
A second example will list all running applications.

Script to List All Running Applications

#!/usr/bin/env groovy 


package groovyx.net.http
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder( 'http://localhost:8080/manager/list')
http.request( GET,TEXT ) { req ->
http.auth.basic( 'tomcat', 'tomcat' )
headers.'User-Agent' = 'GroovyHTTPBuilderTest/0.4'

response.success = { resp, reader ->
println "-----Response-----"
println System.out << reader
println "\n------------------"
}
response.failure = { resp ->
println "Something bad happened. ${resp.statusLine}"
}
}
Output will look similar to the following:
-----Response-----
OK - Listed applications for virtual host localhost
/lenya:running:0:lenya
/examples:running:0:examples
/host-manager:running:0:host-manager
/docs:running:0:docs
/:running:0:ROOT
/manager:running:6:manager
org.codehaus.groovy.runtime.FlushingStreamWriter@2580b3

------------------

This final example polls the tomcat manager for a specific application, (in this case the /manager application) to report on session activity.

Script to Get Tomcat Session Info

#!/usr/bin/env groovy 


package groovyx.net.http
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder( 'http://localhost:8080/manager/sessions?path=/manager')
http.request( GET,TEXT ) { req ->
http.auth.basic( 'tomcat', 'tomcat' )
headers.'User-Agent' = 'GroovyHTTPBuilderTest/0.4'

response.success = { resp, reader ->
println "-----Response-----"
println System.out << reader
println "\n------------------"
}
response.failure = { resp ->
println "Something bad happened. ${resp.statusLine}"
}
}
Output will look similar to the following:
-----Response-----
OK - Session information for application at context path /manager
Default maximum session inactive interval 30 minutes
<1 minutes:1 sessions
1 - <2 minutes:2 sessions
4 - <5 minutes:1 sessions
11 - <12 minutes:1 sessions
>=30 minutes:1 sessions
org.codehaus.groovy.runtime.FlushingStreamWriter@2580b3

------------------
The next article in this series will discuss another hidden gem inside of Tomcat Manager: The JMXProxy.

05/04/2009

Groovy HTTPBuilder and XMLRPC: Another Blog Ping Script

Here's another blog ping script—this one is for Moreover. There are two versions of this script; one uses the HTTPBuilder module and the other uses the XMLRPC module.

ping Moreover using HTTP

#!/usr/bin/env groovy 

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder('http://rpc.weblogs.com/pingSiteForm?name=Blogging%20Techstacks&url=http%3A%2F%2Fblog.techstacks.com&changesURL=http%3A%2F%2Ffeeds2.feedburner.com/BloggingTechstacks')
http.request(GET,TEXT) { req ->
headers.'User-Agent' = 'GroovyHTTPBuilderScript/0.4'
headers.'Cache-Control' = 'no-cache'
headers.'Connection' = 'close'
req.getParams().setParameter("http.connection.timeout", new Integer(5000));
req.getParams().setParameter("http.socket.timeout", new Integer(5000));

response.success = { resp, reader ->
println "-----Response-----"
println "${resp.statusLine}"
println System.out << reader
println "\n------------------"
}
response.failure = { resp ->
println "Something happened: ${resp.statusLine}"
}
}

ping Moreover using XML-RPC

#!/usr/bin/env groovy

import groovy.net.xmlrpc.*

def server = new XMLRPCServerProxy("http://rpc.weblogs.com/RPC2")

def result = server.weblogUpdates.extendedPing("blogging techstacks" , "http://blog.techstacks.com/" , "http://feeds2.feedburner.com/BloggingTechstacks")

if (result != null)
println "Thanks for the ping!"