Skip to content

Commit

Permalink
Merge branch 'metrics'
Browse files Browse the repository at this point in the history
* metrics:
  It's body() now, not body
  It's stop with 3.0.0
  Added JmxReporter
  Added static MetricRegistry and requests Meter
  Added dependency
  • Loading branch information
timyates committed Apr 18, 2013
2 parents 004034b + fa64117 commit 1b1167b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ install - install any jars produced to the local Maven repository (.m2)
dependencies {
compile "com.mchange:c3p0:$c3p0Version"
compile "commons-dbutils:commons-dbutils:$dbutilsVersion"
compile "com.yammer.metrics:metrics-core:$metricsVersion"
testCompile "org.hsqldb:hsqldb:$hsqldbVersion"
testCompile "junit:junit:$junitVersion"
testCompile( "io.vertx:testtools:$toolsVersion" ) {
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/com/bloidonia/vertx/mods/JdbcProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

import com.mchange.v2.c3p0.* ;

import com.yammer.metrics.JmxReporter ;
import com.yammer.metrics.Meter ;
import com.yammer.metrics.MetricRegistry ;
import static com.yammer.metrics.MetricRegistry.name ;

import java.sql.Connection ;
import java.sql.Driver ;
import java.sql.DriverManager ;
Expand Down Expand Up @@ -60,6 +65,10 @@ public class JdbcProcessor extends BusModBase implements Handler<Message<JsonObj
private int batchTimeout ;
private int transTimeout ;

private static MetricRegistry metrics ;
private static Meter requests ;
private static JmxReporter jmxReporter ;

private volatile static ConcurrentHashMap<String,ComboPooledDataSource> poolMap = new ConcurrentHashMap<String,ComboPooledDataSource>( 8, 0.9f, 1 ) ;

private static boolean setupPool( String address,
Expand Down Expand Up @@ -96,6 +105,10 @@ private static boolean setupPool( String address,
pool.close() ;
}
else {
metrics = new MetricRegistry( String.format( "%s.metrics", address ) ) ;
requests = metrics.meter( name( JdbcProcessor.class, "requests" ) ) ;
jmxReporter = JmxReporter.forRegistry( metrics ).build() ;
jmxReporter.start() ;
return true ;
}
}
Expand All @@ -113,6 +126,7 @@ private static void closePool( String address, String url ) throws SQLException
if( pool != null ) {
pool.close() ;
DriverManager.deregisterDriver( DriverManager.getDriver( url ) ) ;
jmxReporter.stop() ;
}
}
}
Expand Down Expand Up @@ -188,7 +202,8 @@ public void stop() {
}

public void handle( final Message<JsonObject> message ) {
String action = message.body.getString( "action" ) ;
String action = message.body().getString( "action" ) ;
requests.mark() ;
logger.debug( "** HANDLE ** " + this.toString() + " (main handler) RECEIVED CALL " + action ) ;
if( action == null ) {
sendError( message, "action must be specified" ) ;
Expand Down Expand Up @@ -300,7 +315,7 @@ private void doExecute( Message<JsonObject> message,
Statement statement = null ;
try {
statement = connection.createStatement() ;
statement.execute( message.body.getString( "stmt" ) ) ;
statement.execute( message.body().getString( "stmt" ) ) ;
if( transaction == null ) {
sendOK( message ) ;
}
Expand Down Expand Up @@ -339,10 +354,10 @@ private void doUpdate( Message<JsonObject> message,
new BatchHandler( connection, message, transaction ) {
void initialiseStatement( Message<JsonObject> initial ) throws SQLException {
if( insert ) {
this.statement = connection.prepareStatement( initial.body.getString( "stmt" ), Statement.RETURN_GENERATED_KEYS ) ;
this.statement = connection.prepareStatement( initial.body().getString( "stmt" ), Statement.RETURN_GENERATED_KEYS ) ;
}
else {
this.statement = connection.prepareStatement( initial.body.getString( "stmt" ) ) ;
this.statement = connection.prepareStatement( initial.body().getString( "stmt" ) ) ;
}
}
public JsonObject process() throws SQLException {
Expand Down Expand Up @@ -404,7 +419,7 @@ private void doTransaction( Message<JsonObject> message, Connection connection )
JsonObject reply = new JsonObject() ;
reply.putString( "status", "ok" ) ;

int timeout = message.body.getNumber( "timeout", transTimeout ).intValue() ;
int timeout = message.body().getNumber( "timeout", transTimeout ).intValue() ;
final long timerId = vertx.setTimer( timeout, new TransactionTimeoutHandler( connection ) ) ;

message.reply( reply, new TransactionalHandler( connection, timerId, timeout ) ) ;
Expand Down Expand Up @@ -441,7 +456,7 @@ private class TransactionalHandler implements Handler<Message<JsonObject>> {

public void handle( Message<JsonObject> message ) {
vertx.cancelTimer( timerId ) ;
String action = message.body.getString( "action" ) ;
String action = message.body().getString( "action" ) ;
logger.debug( "** HANDLE ** " + this.toString() + " (TRANSACTION handler) RECEIVED CALL " + action ) ;
if( action == null ) {
sendError( message, "action must be specified" ) ;
Expand Down Expand Up @@ -543,12 +558,12 @@ private abstract class BatchHandler implements Handler<Message<JsonObject>> {
this.connection = connection ;
this.transaction = transaction ;
this.timerId = -1 ;
this.batchSize = initial.body.getNumber( "batchsize", -1 ).intValue() ;
this.batchSize = initial.body().getNumber( "batchsize", -1 ).intValue() ;
if( this.batchSize <= 0 ) this.batchSize = -1 ;
this.timeout = initial.body.getNumber( "batchtimeout", batchTimeout ).intValue() ;
this.timeout = initial.body().getNumber( "batchtimeout", batchTimeout ).intValue() ;

// create a List<List<Object>> from the values
this.values = JsonUtils.arrayNormaliser( initial.body.getArray( "values" ) ) ;
this.values = JsonUtils.arrayNormaliser( initial.body().getArray( "values" ) ) ;
if( this.values != null ) {
this.valueIterator = values.iterator() ;
}
Expand All @@ -559,7 +574,7 @@ private abstract class BatchHandler implements Handler<Message<JsonObject>> {
}

void initialiseStatement( Message<JsonObject> initial ) throws SQLException {
this.statement = connection.prepareStatement( initial.body.getString( "stmt" ) ) ;
this.statement = connection.prepareStatement( initial.body().getString( "stmt" ) ) ;
}

abstract JsonObject process() throws SQLException ;
Expand Down
1 change: 1 addition & 0 deletions vertx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ testtimeout=300
c3p0Version=0.9.5-pre1
dbutilsVersion=1.5
hsqldbVersion=2.2.9
metricsVersion=3.0.0-BETA1

0 comments on commit 1b1167b

Please sign in to comment.