[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: McKoi with JBoss 3
I've written a simple MBean to accomplish the task of running McKoi as a
JBoss process. The following source code will start (or create) the database
and bind it to a specifie address (this could easily be modifed to run in
embedded mode). Some of the code is specific to our app and may not be
required, but this should get you started (following the source code is the
example configuration):
<SOURCE_START>
package com.openlab.jmx.mckoi;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.Properties;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.jboss.system.ServiceMBeanSupport;
import com.mckoi.database.control.DBController;
import com.mckoi.database.control.DBSystem;
import com.mckoi.database.control.DefaultDBConfig;
import com.mckoi.database.control.TCPJDBCServer;
/**
* An MBean used to start and stop an embedded instance of
* <a href="http://www.mckoi.com/database">McKoi Database</a>.
*
* @author Chris Shorrock
*/
public class McKoiDB extends ServiceMBeanSupport implements McKoiDBMBean {
/**
* defines the name of the database configuration file we will load from
* the classpath to ourline the baseline properties
*/
public static final String DB_PROPERTIES = "db.conf";
private String _dbpath;
private DBSystem _database;
private int _port = 9888;
private String _address;
private TCPJDBCServer _server;
/**
* get's the address this server is bound to
*
* @return
*/
public String getAddress() {
return _address;
}
/**
* set's the address this server is bound to
*
* @param address
*/
public void setAddress( String address ) {
_address = address;
}
/**
* set's the port this database will run on
*
* @param port
*/
public void setDbPort( int port ) {
_port = port;
}
/**
* get's the port this database will run on
*
* @return
*/
public int getDbPort() {
return _port;
}
/**
* returns the path to the configuration and datbase files
*
* @return the path to the db and configuration files.
*/
public String getDbPath() {
return _dbpath;
}
/**
* Sets the config path, the path to a properties file that sets
database
* parameters, this is also the path to the actul database files.
*
* @param path the path to the db and config files
*/
public void setDbPath(String path) {
log.debug( "DB path set to:" + path );
_dbpath = path;
}
/**
* Returns the name of this MBean service.
*
* @see org.jboss.system.ServiceMBean#getName()
*/
public String getName() {
return "McKoiDB";
}
/**
* Returns the object name for this service, called as part of the
lifcycle
* for this MBean.
*
* @see
javax.management.MBeanRegistration#preRegister(javax.management.MBeanServer,
javax.management.ObjectName)
*/
public ObjectName preRegister(MBeanServer server, ObjectName name)
throws Exception {
if (name != null) {
return name;
} else {
return new ObjectName(":service=McKoiDB");
}
}
/**
* method used to start this DB service
*
* @see org.jboss.system.ServiceMBeanSupport#startService()
*/
public void startService() throws Exception
{
if ( _dbpath == null ) {
throw new NullPointerException("McKoiDB: database path not
specified.");
} else {
log.debug("DB path: " + _dbpath);
File dbDir = new File(
_dbpath ).getAbsoluteFile().getParentFile();
File confFile = new File( _dbpath + "/db.conf" );
DefaultDBConfig config = new DefaultDBConfig();
DBController controller = null;
try {
// first we'll load the configuration from the local
// classpath (JAR file) then override the properties
// with those set in the MBean configuration, following
// this we'll then write the configuration back out to
// the file so that the DB can find it. (and thus we
// don't have to hard code the location of the jar file)
InputStream is =
this.getClass().getClassLoader().getResourceAsStream( DB_PROPERTIES );
Properties dbProperties = new Properties();
dbProperties.load( is );
dbProperties.put("database_path",_dbpath);
dbProperties.put("log_path",_dbpath);
dbProperties.store( new FileOutputStream( confFile ) ,
null);
config.loadFromFile( confFile );
controller = DBController.getDefault();
// check's to see if the database exists if it doesn't exist
// we'll create it, if it does then we'll just start this
// bad-boy up.
if( controller.databaseExists( config ) ) {
log.info( "starting database instance" );
_database = controller.startDatabase( config );
log.info( "datbase instance started" );
} else {
log.info( "creating new database" );
_database = controller.createDatabase( config,
dbProperties.getProperty("username"), dbProperties.getProperty("password"));
log.info( "new database created" );
}
if( _address == null ) { _address = "127.0.0.1"; }
log.info( "starting JDBC TCP server on port:" + _port + " on
" + _address );
InetAddress inet = InetAddress.getByName( _address );
_server = new TCPJDBCServer ( _database, inet,
_port );
_server.start();
}
catch (IOException ex) {
log.error("Unable to initialize McKoi database.", ex);
throw ex;
}
}
}
/**
* this method is called when the MBean is remvoed, it handles shutting
* down the database correctly.
*
* @see org.jboss.system.ServiceMBeanSupport#stopService()
*/
public void stopService() {
if( null != _database ) {
log.info( "shutting down database instance" );
_server.stop();
_database.close();
}
}
}
</SOURCE_START>
The MBean interface for this looks like:
<SOURCE_START>
package com.openlab.jmx.mckoi;
import org.jboss.system.ServiceMBean;
/**
* MBean interface for {@link com.openlab.jmx.mckoi.McKoiDB}.
*
* @author Chris Shorrock
*/
public interface McKoiDBMBean extends ServiceMBean {
/**
* returns the path to the configuration and datbase files
*
* @return the path to the db and configuration files.
*/
public String getDbPath();
/**
* Sets the config path, the path to a properties file that sets
database
* parameters, this is also the path to the actul database files.
*
* @param path the path to the db and config files
*/
public void setDbPath(String path);
/**
* set's the port this database will run on
*
* @param port
*/
public void setDbPort( int port );
/**
* get's the port this database will run on
*
* @return
*/
public int getDbPort();
/**
* get's the address this server is bound to
*
* @return
*/
public String getAddress();
/**
* set's the address this server is bound to
*
* @param address
*/
public void setAddress( String address );
}
</SOURCE_START>
And finally our example configuration for this MBean service looks like:
<CONFIG_START>
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: jboss-service.xml,v 1.2 2003/04/07 21:02:16 shorrock Exp $ -->
<!--
===================================================================== -->
<!-- -
->
<!-- McKoi Database 0.94h
-->
<!-- -
->
<!--
===================================================================== -->
<server>
<mbean code="com.openlab.jmx.mckoi.McKoiDB" name="jboss:service=McKoiDB">
<attribute name="DbPath">../server/openlab/db</attribute>
</mbean>
<mbean code="org.jboss.resource.connectionmanager.LocalTxConnectionManager"
name="jboss.jca:service=LocalTxCM">
<depends>jboss:service=McKoiDB</depends>
<depends optional-attribute-name="ManagedConnectionFactoryName">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.RARDeployment"
name="jboss.jca:service=LocalTxDS,name=McKoiDB">
<attribute name="JndiName">McKoiDB</attribute>
<!-- NOTE: Solid wants the username/password in the URL, it will
- ignore the specific arguments.-->
<attribute name="ManagedConnectionFactoryProperties">
<properties>
<config-property name="ConnectionURL"
type="java.lang.String">jdbc:mckoi://127.0.0.1:9888/</config-property>
<config-property name="DriverClass"
type="java.lang.String">com.mckoi.JDBCDriver</config-property>
<config-property name="UserName"
type="java.lang.String">exampleusername</config-property>
<config-property name="Password"
type="java.lang.String">examplepassword</config-property>
</properties>
</attribute>
<!--hack -->
<depends
optional-attribute-name="OldRarDeployment">jboss.jca:service=RARDeployment,n
ame=JBoss LocalTransaction JDBC Wrapper</depends>
</mbean>
</depends>
<depends optional-attribute-name="ManagedConnectionPool">
<!--embedded mbean-->
<mbean
code="org.jboss.resource.connectionmanager.JBossManagedConnectionPool"
name="jboss.jca:service=LocalTxPool,name=McKoiDB">
<attribute name="MinSize">0</attribute>
<attribute name="MaxSize">50</attribute>
<attribute name="BlockingTimeoutMillis">5000</attribute>
<attribute name="IdleTimeoutMinutes">15</attribute>
<attribute name="Criteria">ByNothing</attribute>
</mbean>
</depends>
<depends
optional-attribute-name="CachedConnectionManager">jboss.jca:service=CachedCo
nnectionManager</depends>
<depends
optional-attribute-name="JaasSecurityManagerService">jboss.security:service=
JaasSecurityManager</depends>
<attribute name="TransactionManager">java:/TransactionManager</attribute>
<!--make the rar deploy! hack till better deployment -->
<depends>jboss.jca:service=RARDeployer</depends>
</mbean>
</server>
</CONFIG_START>
If anyone has any questions please ask and I will do my best to accomodate
them.
- Chris Shorrock
----- Original Message -----
From: "Tobias Downer" <toby@mckoi.com>
To: <mckoidb@mckoi.com>
Sent: Friday, May 02, 2003 1:54 PM
Subject: Re: McKoi with JBoss 3
> david.horner@yeg.co.uk wrote:
> > Hi,
> > I'm a complete beginner with the McKoi database and would be very
> > grateful if someone could step me through what I need to do to configure
> > JBoss 3 to use it.
> > I've picked up from other posts that I need to add some details to the
> > login-config.xml file and create a mckoi-service.xml file but wasn't
> > sure whether there was more to it than that.
> > Thanks for any help
> > Dave
>
> Does anyone have any advice for David? Is using Mckoi with JBoss a
> simple process? I pointed David to the list in the hope of getting some
> feedback on JBoss myself. I know there are successful deployments of
> Mckoi with JBoss.
>
> Toby.
>
>
>
> ---------------------------------------------------------------
> Mckoi SQL Database mailing list http://www.mckoi.com/database/
> To unsubscribe, send a message to mckoidb-unsubscribe@mckoi.com
>