1. Trang chủ
  2. » Công Nghệ Thông Tin

Beginning Java SE 6 Platform From Novice to Professional phần 6 pptx

51 415 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Monitoring and Management
Trường học University of Information Technology and Communications
Chuyên ngành Computer Science
Thể loại Lecture notes
Năm xuất bản 2007
Thành phố Hanoi
Định dạng
Số trang 51
Dung lượng 598,59 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

for String option: optionsSystem.out.println option;}} The source code introduces public static void agentmainString agentArgs, Instrumentation instas the entry point into the agent.. Ac

Trang 1

for (String option: options)System.out.println (option);

}}

The source code introduces public static void agentmain(String agentArgs, Instrumentation inst)as the entry point into the agent According to the JDK 6 docu-mentation for the java.lang.instrumentpackage (introduced by Java 5), it is likely that

an application will be running and its public static void main(String [] args)methodwill have been invoked before the virtual machine invokes agentmain()

Note According to the JDK documentation, the target virtual machine will attempt to locate and invoke apublic static void agentmain(String agentArgs)method if it cannot locate public static voidagentmain(String agentArgs, Instrumentation inst) If it cannot find this fallback method, the target virtual machine and its application will keep running, without the agent running in the background.The target virtual machine/application will also keep running if either agentmain()method throws anexception; the uncaught exception is ignored

The agentmain()method specifies a Stringparameter that identifies any argumentspassed to this method These arguments originate from the arguments string passed tooptionsin loadAgent(String agent, String options)(described in Table 7-2) Because thearguments are combined into a single string, the agent is responsible for parsing them.BasicAgentrefers to these arguments as options

After compiling the agent’s source code (javac BasicAgent.java), the resulting classfile must be stored in a JAR file As stated in the JDK documentation, this JAR file’s mani-fest must contain an Agent-Classattribute that identifies the class containing an

agentmain()method Listing 7-4 presents a suitable manifest file with the Agent-Classattribute for the agent’s JAR file

Listing 7-4.manifest.mf

Agent-Class: BasicAgent

After creating a basicAgent.jarfile via jar cvfm basicAgent.jar manifest.mf BasicAgent.class(or a similar command), you are almost ready to use the Attach API toload the JAR file’s agent into a target virtual machine To accomplish this task, I’ve created

an attach application, whose source code appears in Listing 7-5

Trang 2

System.err.println ("Unix usage : "+

Trang 3

VirtualMachine vm = VirtualMachine.attach (vmd.id ());

// Identify the location and name of the agent JAR file to// load The location is relative to the target virtual machine// not the virtual machine running BasicAttach The location// and JAR name are passed to the target virtual machine, which// (in this case) is responsible for loading the basicAgent.jar// file from the location

String agent = vm.getSystemProperties ()

According to the source code, BasicAttachrequires a single command-line argumentthat serves as the name of an application running on a target virtual machine The appli-cation uses this argument to locate an appropriate VirtualMachineDescriptorso that itcan obtain the target virtual machine identifier and then attach to the target virtualmachine

Trang 4

After attaching, BasicAttachneeds to locate basicAgent.jarso that this JAR file can beloaded into the target virtual machine It assumes that basicAgent.jaris placed in the

same location as the JMX agent’s management-agent.jarfile This location is the lib

subdi-rectory of the target virtual machine’s JRE home disubdi-rectory (%JAVA_HOME%\jreunder

Windows)

Open a command window and run the BuggyThreadsapplication presented earlier (if

it is not already running) In another command window, compile BasicAttach.javavia theinstructions near the top of the source code To attach BuggyThreadson Windows systems,

invoke the following:

java -cp %JAVA_HOME%/lib/tools.jar; BasicAttach BuggyThreads

On Unix systems, invoke the following:

java -cp $JAVA_HOME/lib/tools.jar: BasicAttach BuggyThreads

If all goes well, BasicAttachends immediately, returning to the command promptwith no output In contrast, the command window that shows the output from

BuggyThreadswill most likely intermingle BasicAgent’s output with the BuggyThreads

output You might want to redirect the standard output device to a file when running

BuggyThreadsso that you can see the agent’s output Here’s an abbreviated example of

the output:

Starting Thread A

Starting Thread B

Entering infinite loop

Thread A acquiring Lock A

Thread A acquiring Lock B

Thread A releasing Lock B

Thread B acquiring Lock B

Thread A releasing Lock A

Thread B acquiring Lock A

Thread A releasing Lock A

Thread A acquiring Lock A

Thread A acquiring Lock B

Thread A releasing Lock B

Thread B acquiring Lock B

Thread A releasing Lock A

Thread B acquiring Lock A

Basic agent invoked

Trang 5

No options passed

Thread B releasing Lock A

Thread B releasing Lock B

Thread B acquiring Lock B

Thread B acquiring Lock A

Thread B releasing Lock A

Thread A acquiring Lock A

Thread B releasing Lock B

Thread A acquiring Lock B

Basic agent invoked

Options

a=b

c=d

x=y

Thread A releasing Lock B

Thread A releasing Lock A

Thread A acquiring Lock A

Thread A acquiring Lock B

Thread A releasing Lock B

Thread B acquiring Lock B

Improved Instrumentation API

The instrumentation built into HotSpot and other virtual machines provides informationabout virtual machine resources, such as the number of running threads that are stillalive, the peak usage of the heap memory pool since the virtual machine started, and so

on Collectively, this information is useful when you want to monitor an application’s

“health” and take corrective action if its health declines

Although monitoring application health is important, you might prefer to ment an application’s classes (by adding bytecodes to their methods for the purpose

instru-of gathering statistics without modifying application state or behavior) to accomplish

another goal For example, you might be interested in creating a coverage analyzer,

which systematically tests application code

Trang 6

Note Steve Cornett’s “Code Coverage Analysis” paper (http://www.bullseye.com/coverage.html)

describes what a coverage analyzer does

To support instrumentation for coverage analysis, event logging, and other health-related tasks, Java 5 introduced the java.lang.instrumentpackage This package’s

non-Instrumentationinterface provides services needed to instrument classes, such as

registering a transformer (a class that implements the java.lang.instrument

ClassFileTransformerinterface) to take care of instrumentation

Note Java 5’s Instrumentationinterface also provides services for redefining classes In contrast

to transformation, which focuses on changing classes from an instrumentation perspective, redefinition

focuses on replacing a class’s definition For example, you might want to develop a tool that supports

fix-and-continue debugging This is an alternative to the traditional edit-compile-debug cycle, which lets you

change a program from within the debugger and continue debugging without needing to leave the debugger,

recompile, enter the debugger, and restart debugging from scratch You would use redefinition to change the

class’s definition to include new class bytes resulting from compilation

Instrumentationis one of the parameters in the two-parameter agentmain()method

Both overloaded versions of this method were added in Java SE 6 Instrumentationis also

one of the parameters in the two-parameter premain()method, which was introduced by

Java 5 and has a parameter list identical to that of agentmain() Unlike premain(), which is

always invoked before an application’s main()method runs, agentmain()is often (but not

necessarily) invoked after main()has run Also, whereas agentmain()is invoked as a result

of dynamic attach, premain()is invoked as a result of starting the virtual machine with

the -javaagentoption, which specifies an agent JAR file’s path and name When an

Instrumentationinstance is passed to either method, the method can access the

instance’s methods to transform/redefine classes

Note According to Simone Bordet’s “Attaching to a Mustang, explained” blog entry (http://bordet

blogspot.com/2005_11_01_archive.html), Java SE 6 also introduces a single-parameter premain()

method to complement the single-parameter agentmain()method As with agentmain(), this method’s

single parameter is also String agentArgs Furthermore, it serves as a fallback to the two-parameter

premain()method

Trang 7

Retransformation Support

Java SE 6 adds four new methods to the Instrumentationinterface to support mation:

retransfor-• void retransformClasses(Class<?> classes)

• void addTransformer(ClassFileTransformer transformer, boolean canRetransform)

• boolean isModifiableClass(Class<?> theClass)

• boolean isRetransformClassesSupported()Agents use these methods to retransform previously loaded classes without needing

to access their class files Sun developer Sundar Athijegannathan demonstrates the firsttwo of these methods in his class-dumper agent, presented as an example of a usefulagent in his “Retrieving class files from a running app” blog entry (http://blogs.sun.com/sundararajan/entry/retrieving_class_files_from_a) He passes trueas addTransformer()’scanRetransformargument so that retransformClasses()invokes transform()for each candidate class If falsewere passed, transform()would not be invoked

The isModifiableClass()method returns true if a specific class can be modified viaredefinition or retransformation Java 5 made it possible to determine if the current vir-tual machine configuration supports redefinition via boolean

isRedefineClassesSupported() Java SE 6 complements this method with boolean

isRetransformClassesSupported(), which returns true if retransformation is supported

Note Java 5 provided a Can-Redefine-Classesattribute that had to be initialized to truein an agent’sJAR manifest so that the agent could redefine classes Java SE 6’s new Can-Retransform-Classesattributecomplements this other attribute The agent can retransform classes only if Can-Retransform-Classesis initialized to truein its JAR manifest

Native Method Support

Java SE 6 adds two new methods to the Instrumentationinterface that agents can use toprepare native methods for instrumentation:

• void setNativeMethodPrefix(ClassFileTransformer transformer, String prefix)

• boolean isNativeMethodPrefixSupported()

Trang 8

Native methods cannot be directly instrumented because they have no bytecodes.

According to the setNativeMethodPrefix()method’s documentation, you can use a

trans-former to wrap a native method call inside a nonnative method, which can be instrumented

For example, consider native boolean foo(int x) To apply instrumentation, this method

must be wrapped in a same-named nonnative method:

boolean foo (int x)

{

record entry to foo

// Specifying return foo (x); would result in recursion

return $$$myagent_wrapped_foo (x);

}

native boolean $$$myagent_wrapped_foo (int x);

A new problem arises in how to resolve the name of the called native method to thenative method’s implementation name For example, suppose the original fooname for

the native method resolves to Java_somePackage_someClass_foo Because $$$myagent_

wrapped_foomight correspond to Java_somePackage_someClass_$$$myagent_ wrapped_foo

(which doesn’t exist), resolution fails

Invoking setNativeMethodPrefix()with $$$myagent_as this method’s prefixparametervalue solves this problem After unsuccessfully trying to resolve $$$myagent_wrapped_foo

to Java_somePackage_someClass_$$$myagent_wrapped_foo, the virtual machine deletes the

prefix from the native name and resolves $$$myagent_wrapped_footo Java_somePackage_

someClass_foo

Note For an agent to set the native method prefix, the agent’s JAR manifest must initialize Java SE 6’s

Can-Set-Native-Method-Prefixattribute to true Call the isNativeMethodPrefixSupported()

method to determine this attribute’s value

Support for Additional Instrumentation Classes

Finally, two more new Instrumentationmethods can be used to make additional JAR files

with instrumentation classes available to the bootstrap and system classloaders:

• void appendToBootstrapClassLoaderSearch(JarFile jarfile)

• void appendToSystemClassLoaderSearch(JarFile jarfile)

Trang 9

These methods allow you to specify JAR files containing instrumentation classes thatare to be defined by the bootstrap or system classloaders When the classloader’s searchfor a class is unsuccessful, it will search a specified JAR file for the class The JAR file mustnot contain any classes or resources other than those to be defined by the classloader foruse in instrumentation.

Improved JVM Tool Interface

The Attach API’s VirtualMachineclass includes a pair of loadAgentLibrary()methods and

a pair of loadAgentPath()methods All four methods accomplish the same goal: they load

a native agent library developed with the JVM Tool Interface The loadAgentLibrary()methods require only the name of the library The loadAgentPath()methods require theabsolute path (including the name) of the library

Java 5 introduced the JVM Tool Interface as a replacement for the JVM Debug face and JVM Profiler Interface, which were deprecated; JVM Debug is not present in Java

Inter-SE 6 Java Inter-SE 6 cleans up and clarifies the JVM Tool Interface specification and offers thefollowing new and improved features:

Support for class-file retransformation: A RetransformClasses()function has beenadded to facilitate the dynamic transformation of classes that have previously beenloaded Access to the original class file is no longer required to instrument a loadedclass Retransformation can easily remove an applied transformation, and retransfor-mation is designed to work in a multiple-agent environment

Support for enhanced heap traversal: The IterateThroughHeap()and FollowReferences()functions have been added to traverse objects in the heap IterateThroughHeap()traverses all reachable and unreachable objects in the heapwithout reporting references between objects FollowReferences()traverses objectsdirectly and indirectly reachable from either a specified object or heap roots (the set

of system classes, for example) These functions can be used to examine the tive values in arrays, Strings, and fields via special callback functions Various heapfilter flags control which objects and primitive values are reported by the callbacks.For example, JVMTI_HEAP_FILTER_TAGGEDexcludes tagged objects

primi-Additional class information: GetConstantPool(), GetClassVersionNumbers(), andIsModifiableClass()functions have been added to return additional class informa-tion

Support for instrumenting native methods: SetNativeMethodPrefix()and SetNativeMethodPrefixes()functions have been added to allow native methods

to be instrumented via a virtual machine-aware mechanism for wrapping thesemethods in nonnative methods

Trang 10

Enhanced support for instrumentation under the system classloader: An

AddToSystemClassLoaderSearch()function allows the system classloader to defineinstrumentation support classes

Support for early return from methods: “ForceEarlyReturn” functions, such as ForceEarlyReturnObject(), have been added to allow a debugger-like agent to force

a method to return from any point during its execution

Ability to access monitor stack-depth information: A GetOwnedMonitorStackDepthInfo()

function has been added to obtain information about a thread’s owned monitors andthe depth of the stack frame when the monitors were locked

Support for notification when a resource has been exhausted: A ResourceExhausted()

function has been added to notify the virtual machine (via an event) when a criticalresource, such as the heap, has been exhausted

In addition to these enhancements, Java SE 6 introduces a new JVMTI_ERROR_CLASS_

LOADER_UNSUPPORTEDerror code constant to indicate that the classloader does not support

an operation It also allows the AddToBootstrapClassLoaderSearch()function to be called

during the live phase (the agent’s execution phase between calls to VMInit()and VMDeath())

Note For a JVM Tool Interface tutorial, check out “The JVM Tool Interface (JVM TI): How VM Agents Work”

article (http://java.sun.com/developer/technicalArticles/J2SE/jvm_ti/) and the JVM Tool face demos (such as heapViewer) that are included in the JDK distribution

Inter-Improved Management and JMX APIs

The Management API focuses on providing a variety of MXBean interfaces and their

methods for accessing virtual machine instrumentation The JMX API focuses on

provid-ing the infrastructure for the JMX agent and applications like JConsole that access the

JMX agent

Note For background on MXBeans, check out Sun JMX team leader Eamonn McManus’s “What is an

MXBean?” blog entry (http://weblogs.java.net/blog/emcmanus/archive/ 2006/02/what_is_an_

mxbe.html)

Trang 11

Management API Enhancements

Java SE 6 introduces several enhancements to the java.lang.managementpackage Forstarters, five new methods have been added to this API’s ThreadMXBeaninterface In addi-tion to the new long [] findDeadlockedThreads()method, which returns an array of IDsfor deadlocked threads (demonstrated in Listing 7-1), ThreadMXBeanincludes the fourmethods described in Table 7-3

Table 7-3.Additional New ThreadMXBean Methods

ThreadInfo[] dumpAllThreads(boolean Returns thread information for all live threads lockedMonitors, boolean Pass true to lockedMonitors to include

lockedSynchronizers) information on all locked monitors Pass true to

lockedSynchronizers to include information on

all ownable synchronizers An ownable synchronizer is a synchronizer that can be

exclusively owned by a thread Its synchronization property is implemented via a java.util.concurrent.locks.

AbstractOwnableSynchronizer subclass.

ThreadInfo[] getThreadInfo(long[] Similar to the previous method, but restricts ids, boolean lockedMonitors, thread information to only those threads whose boolean lockedSynchronizers) identifiers are stored in the ids array.

boolean isObjectMonitorUsageSupported() Returns true if the monitoring of object monitor

usage is supported Because a virtual machine might not support this kind of monitoring, you will want to call

isObjectMonitorUsageSupported() before passing true to lockedMonitors

boolean isSynchronizerUsageSupported() Returns true if the monitoring of ownable

synchronizer usage is supported Because a virtual machine might not support this kind

of monitoring, you will want to call isSynchronizerUsageSupported() before passing true to lockedSynchronizers

To support locked monitors, the ThreadInfoclass includes a new public MonitorInfo[]getLockedMonitors()method that returns an array of MonitorInfoobjects To support ownable synchronizers, ThreadInfohas a new public java.lang.management.LockInfo[] getLockedSynchronizers()method that returns an array of LockInfoobjects MonitorInfoandLockInfoare new classes in Java SE 6

Trang 12

Note ThreadInfoalso includes a new public LockInfo getLockInfo()method that returns

infor-mation related to a lock based on a built-in object monitor, as opposed to a lock based on an ownable

synchronizer

The OperatingSystemMXBeaninterface has been assigned a new double getSystemLoadAverage()method that returns the system load average for the last minute

(The system load average is the number of runnable entities queued to available

proces-sors, plus the number of runnable entities running on the available procesproces-sors, averaged

over a period of time.) The method returns a negative value if the load average is not

available

Note Sun offers com.sun.managementas its platform extension to java.lang.management This

package’s management interfaces provide access to platform-specific instrumentation For example, the

UnixOperatingSystemMXBeaninterface includes a long getOpenFileDescriptorCount()method that

returns the number of open Unix file descriptors Java SE 6 enhances com.sun.managementby adding a

new platform-neutral VMOptionclass and a VMOption.Originenumeration to provide information about

virtual machine options and their origins

JMX API Enhancements

The two biggest enhancements that Java SE 6 brings to the JMX API have an impact on

descriptors and MXBeans, and are as follows:

Attach arbitrary extra metadata to all kinds of MBeans: The new javax.management.

DescriptorKeyannotation type lets you attach extra metadata to MBeans other thanmodel MBeans For more information, check out Eamonn McManus’s “AddingDescriptors to MBeans in Mustang” blog entry (http://weblogs.java.net/blog/

emcmanus/archive/2005/10/adding_descript.html)

Define your own MBeans: The new javax.management.MXBeanannotation type lets youexplicitly mark an interface as being an MXBean interface or as not being an MXBeaninterface

Additional enhancements include notification improvements, a convenient way toretrieve a javax.management.remote.JMXServiceURLfrom a javax.management.remote

JMXConnector, and the generification of the JMX API To learn about these, check out

Eamonn McManus’s “Mustang Beta and the JMX API” blog entry (http://weblogs.java.net/

blog/emcmanus/archive/2006/02/mustang_beta_an.html)

Trang 13

JConsole GUI Makeover

JConsole’s GUI has been given an extensive makeover in Java SE 6 This makeover takesadvantage of the system look and feel on Windows and GNOME desktops, which givesJConsole a more professional appearance This professionalism is especially evident inthe revamped connection dialog that appears when you start JConsole As you can seefrom Figure 7-1, the biggest change to this dialog is the removal of its former tabbedinterface The GUI components previously located on the Local, Remote, and Advancedtabs have been merged into a more intelligent and simpler layout

Figure 7-1.The system look and feel gives the connection dialog a more professional appearance.

JConsole’s tabbed interface has also changed The previous Summary and VM tabshave morphed into Overview and VM Summary tabs, as follows:

• The Overview tab is the equivalent of the previous Summary tab However, unlikethe Summary tab’s textual display, the Overview tab presents live charts of heapmemory usage, threads, classes, and CPU usage

• The VM Summary tab is equivalent to the previous VM tab, but rearranges the VMtab’s information The Memory, Threads, Classes, and MBeans tabs are present inthe new JConsole, although the MBeans tab has shifted position Also, a conven-ient Detect Deadlock button has been added to the Threads tab

Trang 14

Note Sun JMX team member Luis-Miguel Alventosa’s “Changes to the MBeans tab look and feel in

Mustang JConsole” blog entry (http://blogs.sun.com/lmalventosa/entry/changes_to_the_

mbeans_tab) visually compares the Java 5 and Java SE 6 versions of JConsole’s MBeans tab to reveal

Java SE 6’s structural changes to this tab

JConsole Plug-ins and the JConsole API

In late 2004, Bug 6179281 “Provide a jconsole plug-in interface to allow loading

user-defined tabs” was submitted to Sun’s Bug Database, requesting that JConsole be extended

with a plug-in API This API would allow a developer to introduce new tabs to JConsole’s

user interface, for interacting with custom MBeans and performing other tasks This

request has been fulfilled in Java SE 6

Java SE 6 supports JConsole plug-ins via the Sun-specific com.sun.tools.jconsolepackage (http://java.sun.com/javase/6/docs/jdk/api/jconsole/spec/index.html), which

is stored in jconsole.jar A plug-in must subclass this package’s abstract JConsolePlugin

class and implement the two methods listed in Table 7-4

Table 7-4.Methods for Adding JConsole Plug-ins

public abstract Map<String, Returns a java.util.Map of tabs to be added in the

JPanel>getTabs() JConsole window Each Map entry describes one tab,

with the tab’s name stored in a String and the tab’s GUI components stored in a javax.swing.JPanel An empty map is returned if this plug-in does not add any tabs This method is called on the event- dispatching thread when a new connection is being made.

public abstract SwingWorker<?, Returns a javax.swing.SwingWorker that updates

?>newSwingWorker() the plug-in’s GUI, at the same interval as JConsole

updates its GUI jconsole’s -interval command-line option specifies the interval (4 seconds is the default) This method is called at each update to obtain a new SwingWorker for the plug-in It returns null if the plug-in schedules its own updates.

Trang 15

A Basic Plug-in

Consider a basic plug-in that adds a Basic tab to the JConsole window’s list of tabs Whenyou select this tab, it will present the current date, updated once per interval Becausethis plug-in also outputs various messages to the standard output device, JConsole willpresent another window that displays this output in real time Listing 7-6 presents thebasic plug-in’s source code

private Map<String, JPanel> tabs = null;

private BasicTab basicTab = null;

public Map<String, JPanel> getTabs ()

{System.out.println ("getTabs() called");

if (tabs == null){

tabs = new LinkedHashMap<String, JPanel> ();

basicTab = new BasicTab ();

tabs.put ("Basic", basicTab);

}return tabs;

}

Trang 16

public SwingWorker<?, ?> newSwingWorker ()

{System.out.println ("newSwingWorker() called");

return new BasicTask (basicTab);

}}

class BasicTab extends JPanel

{

private JLabel label = new JLabel ();

BasicTab (){

add (label);

}

void refreshTab (){

label.setText (new Date ().toString ());

}}

class BasicTask extends SwingWorker<Void, Void>

{

private BasicTab basicTab;

BasicTask (BasicTab basicTab){

this.basicTab = basicTab;

}

@Overridepublic Void doInBackground (){

System.out.println ("doInBackground() called");

// Nothing needs to be done, but this method needs to be present

return null;

}

@Override

Trang 17

public void done (){

System.out.println ("done() called");

basicTab.refreshTab ();

}}

The plug-in consists of BasicPlugin, BasicTab, and BasicTaskclasses The BasicPluginclass is the entry point into the plug-in The BasicTabclass describes a GUI container thatcontains a single label, which presents the current date The BasicTaskclass describes aSwingWorkerthat refreshes the GUI component with the current date

BasicPlugin’s getTabs()method lazily initializes the tabsand basicTabfields to newjava.util.LinkedHashMap(which stores the Basic tab’s name and GUI) and BasicTabinstances Additional calls to getTabs()will not result in unnecessary LinkedHashMapandBasicTabinstances being created This method returns the solitary LinkedHashMap

instance

BasicPlugin’s newSwingWorker()method, which is regularly called after getTabs()finishes, creates and returns a BasicTask SwingWorkerobject that stores the BasicTabinstance This instance is stored so that BasicTab’s refreshTab()method can be called toupdate the label’s text with the next current date when BasicTask’s done()method iscalled

Build and run this plug-in as follows:

1. Compile BasicPlugin.javaas appropriate for Unix or Windows (see the commentsnear the top of the source code)

2. Create a META-INF/servicesdirectory structure In the servicesdirectory, place

a com.sun.tools.jconsole.JConsolePlugintext file whose single line specifies BasicPlugin

3. Create the plug-in’s JAR file by invoking this command:

jar cvf basicPlugin.jar -C META-INF/ services *.classThe jconsoletool includes a new -pluginpathcommand-line option whose pluginsargument lists directories or JAR files that are searched for JConsole plug-ins As with

a JAR file, a directory must contain a META-INF/services/com.sun.tools.jconsole

JConsolePlugintext file that identifies its plug-in entry-point classes, one per line

To run JConsole with the basic plug-in, invoke jconsole -pluginpath basicplugin.jar.Figure 7-2 shows JConsole’s GUI after a new connection has been made (via JConsole’sNew Connection dialog)

Trang 18

Figure 7-2.The Basic plug-in tab added to the JConsole window Notice that plug-in tabs

appear to the right of the MBeans tab.

In addition to showing a Basic tab with the current date (updated at the specifiedinterval), the basic plug-in displays a console window that presents a real-time update of

various messages sent to the standard output device These messages help you to

under-stand the behavior of the basic plug-in in terms of calls to its various methods and the

order in which these calls occur

Beyond the Basic Plug-in

After experimenting with the basic in, you will want to try out more advanced

plug-ins Mandy Chung, a senior staff engineer at Sun, and Sundar Athijegannathan have

created sample JConsole ins, which are included with the JDK Mandy’s JTop

plug-in is used to monitor the CPU usage of an application’s threads; Sundar’s script-shell

plug-in demonstrates the power of using a scripting language with JMX technology The

following Windows-oriented command line runs JConsole with both plug-ins:

jconsole -pluginpath %JAVA_HOME%/demo/management/JTop/JTop.jar;

%JAVA_HOME%/demo/scripting/jconsole-plugin/jconsole-plugin.jarInvoke this command line (which must be specified as a single line; it is shownacross two lines because of its length) In response, you should see the JTop and Script

Shell tabs appear to the right of the MBeans tab The JTop tab, shown in Figure 7-3, gives

you more information about running threads The Script Shell tab lets you interactively

access the operations and attributes of MBeans via a scripting language

Trang 19

Figure 7-3.Observe thread names, their CPU usages, and their current states on the

JTop tab.

You can learn how both of these plug-ins work by studying their source code, which

is included in the JDK To discover how JTop can run as a stand-alone JMX client, checkout Alan Bateman’s “Two fine demos” blog entry (http://blogs.sun.com/alanb/entry/two_ fine_demos) To learn more about the script-shell plug-in, check out Sundar

Athijegannathan’s “Using script shell plugin with jconsole” blog entry (http://blogs.sun.com/sundararajan/entry/using_script_shell_plugin_with)

Note Blogger and Java developer Peter Doornbosch has created a top-threads JConsole plug-in as areplacement for JTop You can learn about this plug-in, examine the Top threads tab’s GUI, and download the plug-in’s topthreads.jarfile (source code does not appear to be available) from http://blog.luminis.nl/luminis/entry/top_threads_plugin_for_jconsole

Another advanced plug-in is described by Luis-Miguel Alventosa in his “Per-threadCPU Usage JConsole Plugin” blog entry (http://blogs.sun.com/lmalventosa/entry/per_thread_cpu_usage_jconsole) This thread CPU usage JConsole plug-in graphs threadusage for multiple threads According to Luis-Miguel, “The aim of this plugin is to showhow easy it is to add a custom UI to JConsole based on the Java SE platform instrumenta-tion MXBeans in conjunction with the JFreeChart chart library.”

Trang 20

Java SE 6 enhances its support for monitoring and management by providing a new

dynamic attach capability and the Attach API, an improved Instrumentation API, an

improved JVM Tool Interface, improved Management and JMX APIs, a JConsole GUI

makeover, and support for JConsole plug-ins via the new JConsole API

The dynamic attach mechanism allows JConsole to connect to and start the JMXagent in a target virtual machine JConsole and other Java applications take advantage

of this mechanism by using the Attach API

The Instrumentation API has been improved through the addition of eight newmethods to the Instrumentationinterface Four of these methods support retransforma-

tion, two methods allow agents to prepare native methods for instrumentation, and two

methods can be used to make additional JAR files with instrumentation classes available

to the bootstrap and system classloaders

Java SE 6 also cleans up and clarifies the JVM Tool Interface specification and offers

a variety of new and improved features These features provide support for class-file

retransformation, enhanced heap traversal, instrumenting native methods, early return

from methods, and notification when a resource has been exhausted They also provide

enhanced support for instrumentation under the system classloader, access to additional

class information, and the ability to access monitor stack-depth information

The Management API has been improved by introducing five new methods to thisAPI’s ThreadMXBeaninterface, new ThreadInfomethods for identifying locked monitors and

ownable synchronizers, and a new OperatingSystemMXBeanmethod for returning the

system load average Also, the JMX API has been improved, primarily through the ability

to attach arbitrary extra metadata to all kinds of MBeans and the ability to define your

own MBeans

JConsole’s GUI has been given a makeover, which takes advantage of the system lookand feel on Windows and GNOME desktops In addition to revamping the connection

dialog, Java SE 6 reorganizes JConsole’s tabbed interface

Finally, Java SE 6 introduces a plug-in API for JConsole The JConsole API allowsdevelopers to add new tabs to JConsole’s user interface, for interacting with custom

MBeans and performing other tasks

Test Your Understanding

How well do you understand Java SE 6’s new monitoring and management features? Test

your understanding by answering the following questions and performing the following

exercises (The answers are presented in Appendix D.)

Trang 21

1. Describe local monitoring Under Java SE 6, does the com.sun.management.jmxremotesystem property need to be specified when starting an application

to be locally monitored?

2. What is the difference between class definition and transformation? Does redefinition cause a class’s initializers to run? What steps are followed duringretransformation?

3. What is the difference between agentmain()and premain()?

4. Create a LoadAverageViewerapplication modeled after ThreadInfoViewer This newapplication will invoke OperatingSystemMXBean’s getSystemLoadAverage()method

If this method returns a negative value, output a message stating that the loadaverage is not supported on this platform Otherwise, repeatedly output the loadaverage once per minute, for a specific number of minutes as determined by acommand-line argument

5. The JConsole API includes a JConsoleContextinterface What is the purpose of this interface?

6. JConsolePlugin’s public final void addContextPropertyChangeListener(PropertyChangeListener listener)method is used to add a java.beans

PropertyChangeListenerto a plug-in’s JConsoleContext When is this listenerinvoked, and how does this benefit a plug-in?

Trang 22

Have you ever needed a network interface’s hardware address, but had to resort to

executing an external program to obtain this information because Java did not provide

the appropriate API? Java SE 6 addresses this need and more by adding a variety of new

networking features to Java:

Server programs commonly use cookies (state objects) to persist small amounts of

infor-mation on clients For example, the identifiers of currently selected items in a shopping

cart can be stored as cookies It is preferable to store cookies on the client, rather than on

the server, because of the potential for millions of cookies (depending on a web site’s

popularity) In that case, not only would a server require a massive amount of storage justfor cookies, but also searching for and maintaining cookies would be time consuming

Note Check out Netscape’s “Persistent Client State: HTTP Cookies” preliminary specification

(http://wp.netscape.com/newsref/std/cookie_spec.html) for a quick refresher on cookies

A server program such as a web server sends a cookie to a client as part of an HTTPresponse A client program such as a web browser sends a cookie to the server as part of

253

C H A P T E R 8

Trang 23

an HTTP request Prior to Java 5, applications worked with the java.net.URLConnectionclass (and its java.net.HttpURLConnectionsubclass) to get an HTTP response’s cookies and

to set an HTTP request’s cookies The public String getHeaderFieldKey(int n)and publicString getHeaderField(int n)methods were used to access a response’s Set-Cookie head-ers, and the public void setRequestProperty(String key, String value)method was used

to create a request’s Cookie header

Note RFC 2109: HTTP State Management Mechanism (http://www.ietf.org/rfc/rfc2109.txt)describes the Set-Cookie and Cookie headers

Java 5 introduced the abstract java.net.CookieHandlerclass as a callback mechanismthat connects HTTP state management to an HTTP protocol handler (think

HttpURLConnectionsubclass) An application installs a concrete CookieHandlersubclass

as the system-wide cookie handler via the CookieHandlerclass’s public static void setDefault(CookieHandler cHandler)method A companion public static CookieHandlergetDefault()method returns this cookie handler, which is null if a system-wide cookiehandler has not been installed If a security manager has been installed and deniesaccess, a SecurityExceptionwill be thrown when setDefault()or getDefault()is called

An HTTP protocol handler accesses response and request headers This handlerinvokes the system-wide cookie handler’s public void put(URI uri,

Map<String,List<String>> responseHeaders)method to store response cookies in a cookiecache, and the public Map<String,List<String>> get(URI uri, Map<String,List<String>>requestHeaders)method to fetch request cookies from this cache Unlike Java 5, Java SE 6provides a concrete implementation of CookieHandlerso that HTTP protocol handlersand applications can work with cookies

The concrete java.net.CookieManagerclass extends CookieHandlerto manage cookies

A CookieManagerobject is initialized as follows:

• With a cookie store for storing cookies The cookie store is based on the

java.net.CookieStoreinterface

• With a cookie policy for determining which cookies to accept for storage The

cookie policy is based on the java.net.CookiePolicyinterface

Create a cookie manager by calling either the public CookieManager()constructor orthe public CookieManager(CookieStore store, CookiePolicy policy)constructor The public CookieManager()constructor invokes the latter constructor with nullarguments,using the default in-memory cookie store and the default accept-cookies-from-the-original-server-only cookie policy Unless you plan to create your own CookieStoreandCookiePolicyimplementations, you will work with the default constructor The following

Trang 24

code fragment creates and establishes a new CookieManageras the system-wide cookie

handler:

CookieHandler.setDefault (new CookieManager ());

Along with its constructors, CookieManagerprovides four methods, which Table 8-1describes

Table 8-1.CookieManager Methods

public Map<String, List<String>> Returns an immutable map of Cookie and

get(URI uri, Map<String, List<String>> Cookie2 request headers for cookies obtained

requestHeaders) from the cookie store whose path matches the

uri’s path Although requestHeaders is not used by the default implementation of this method, it can be used by subclasses A java.io.IOException is thrown if an I/O error occurs.

public CookieStore getCookieStore() Returns the cookie manager’s cookie store.

CookieManager currently works with CookieStore’s void add(URI uri, HttpCookie cookie) and List<HttpCookie> get(URI uri) methods only Other CookieStore methods are present to support more sophisticated implementations of CookieStore.

public void put(URI uri, Map<String, Stores all applicable cookies whose Set-Cookie List<String>> responseHeaders) and Set-Cookie2 response headers were

retrieved from the specified uri and placed (with all other response headers) in the immutable responseHeaders map in the cookie store An IOException is thrown if an I/O error occurs.

public void setCookiePolicy(CookiePolicy Sets the cookie manager’s cookie policy to

cookiePolicy) one of CookiePolicy.ACCEPT_ALL (accept all

cookies), CookiePolicy.ACCEPT_NONE (accept

no cookies), or CookiePolicy.

ACCEPT_ORIGINAL_SERVER (accept cookies from original server only) Passing null to this method has no effect on the current policy.

In contrast to the get()and put()methods, which are called by HTTP protocol dlers, an application works with the getCookieStore()and setCookiePolicy()methods

han-Consider a command-line application that obtains and lists all cookies from its single

domain-name argument The source code appears in Listing 8-1

Trang 25

System.err.println ("usage: java ListAllCookies url");

new URL (args [0]).openConnection ().getContent ();

List<HttpCookie> cookies = cm.getCookieStore ().getCookies ();

for (HttpCookie cookie: cookies)

{

System.out.println ("Name = "+cookie.getName ());

System.out.println ("Value = "+cookie.getValue ());

System.out.println ("Lifetime (seconds) = "+cookie.getMaxAge ());

System.out.println ("Path = "+cookie.getPath ());

System.out.println ();

}}}

After creating a cookie manager and invoking setCookiePolicy()to set the cookiemanager’s policy to accept all cookies, the application installs the cookie manager as thesystem-wide cookie handler It next connects to the domain identified by the command-line argument and reads the content The cookie store is obtained via getCookieStore()and used to retrieve all nonexpired cookies via its List<HttpCookie> getCookies()

method For each of these java.net.HttpCookies, public String getName(), public StringgetValue(), and other HttpCookiemethods are invoked to return cookie-specific informa-tion The following output resulted from invoking java ListAllCookies http://apress.com:

Ngày đăng: 09/08/2014, 14:21

TỪ KHÓA LIÊN QUAN