Java Reference
In-Depth Information
props.load( f );
f.close();
}
catch( Exception e )
{
e.printStackTrace();
}
}
}//class
This MBean is straightforward and exposes only the methods present in the
java.util.Properties class. The only attribute, Source , is used to reset the entire
properties set with a new properties file.
Properties are the most obvious way to use MBeans to make applications con-
figurable. However, you can also use an MBean to configure a single part of your
application, such as database access.
Configuring a DataSource
Many applications need the services of a database. Java applications use the
JDBC API to open database connections by creating a Connection object or by
acquiring Connection objects from a DataSource object. In both cases, it might be
useful to configure the creation of database connections. You can do so by encap-
sulating the acquisition of database connections inside an MBean.
The following example is a simple Standard MBean that acquires database
connections from a DataSource object. It gets the DataSource object by using a
Java Naming and Directory Interface ( JNDI ) lookup; it could just create the con-
nection directly, as well. The following is the MBean interface for the DBSource
MBean (to learn more about using JNDI or JDBC , go to http://www.javasoft.com):
package jmxbook.ch4;
import java.sql.*;
public interface DBSourceMBean
{
public void resetDataSource( String name );
public void setAutoCommit( boolean commit );
public boolean getAutoCommit( );
public Connection getConnection();
}
As you can see, the DBSourceMBean interface appears to expose one read/write
attribute, AutoCommit , and one readable attribute, Connection . It also exposes an
operation, resetDataSource() . Listing 4.2 shows the DBSource class.
Search WWH ::




Custom Search