Skip to content

Commit

Permalink
Add basic JMX functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
James Abley committed May 8, 2009
1 parent 4a3e245 commit bd83012
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 3 deletions.
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0


Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down
2 changes: 1 addition & 1 deletion ivy/ivy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</configurations>
<dependencies>
<dependency org="junit" name="junit" rev="4.5" conf="test->*"/>
<dependency org="net.sf.ehcache" name="ehcache" rev="1.5.0" conf="default->*;test->*">
<dependency org="net.sf.ehcache" name="ehcache" rev="1.5.0" conf="default->*;test->default">
<exclude module="jta" />
<exclude module="servlet" />
<exclude module="hibernate" />
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/eternus/ratelimit/Enablable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2009 James Abley
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.eternus.ratelimit;

/**
* Interface defining whether a service can be enabled or not.
*
* @author jabley
*
*/
public interface Enablable {

/**
* Returns true if this service is enabled, otherwise false.
*
* @return true if this service is enabled, otherwise false
*/
boolean isEnabled();

/**
* Sets the enabled state of this service.
*
* @param enabled
* the enabled state
*/
void setEnabled(boolean enabled);

}
9 changes: 9 additions & 0 deletions src/main/java/com/eternus/ratelimit/FixedBucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,13 @@ public boolean isEnabled() {
return enabled;
}

/**
* {@inheritDoc}
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}



}
2 changes: 1 addition & 1 deletion src/main/java/com/eternus/ratelimit/RateLimiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @author jabley
*
*/
public interface RateLimiter {
public interface RateLimiter extends Enablable {

/**
* Method called by clients to check whether they should service the current request or not. Returns a non-null
Expand Down
117 changes: 117 additions & 0 deletions src/main/java/com/eternus/ratelimit/jmx/ManagedRateLimiter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2009 James Abley
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.eternus.ratelimit.jmx;

import javax.management.MBeanNotificationInfo;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.monitor.MonitorNotification;

import com.eternus.ratelimit.Key;
import com.eternus.ratelimit.RateLimiter;
import com.eternus.ratelimit.Token;

/**
* JMX MBean which will send out notifications around a {@link RateLimiter} implementation.
*
* @author jabley
*
*/
public class ManagedRateLimiter extends NotificationBroadcasterSupport implements RateLimiter, ManagedRateLimiterMBean {

/**
* The name of the JXM notification that will be sent for successfully serviced requests.
*/
private static final String JMX_MONITOR_RATE_LIMIT_SERVICE_TYPE = "jmx.monitor.rate-limit.service";

/**
* The non-null delegate.
*/
private final RateLimiter delegate;

/**
* The JMX notification sequence number.
*/
private long sequenceNumber;

/**
* Creates a new {@link ManagedRateLimiter} which will delegate the implementation to the specified non-null
* {@link RateLimiter}.
*
* @param delegate
* a non-null {@link RateLimiter} around which this MBean will send notifications
*/
public ManagedRateLimiter(RateLimiter delegate) {
if (delegate == null) {
throw new IllegalArgumentException("delegate cannot be null");
}
this.delegate = delegate;
}

/**
* {@inheritDoc}
*/
@Override
public MBeanNotificationInfo[] getNotificationInfo() {
String[] types = new String[] { JMX_MONITOR_RATE_LIMIT_SERVICE_TYPE,
MonitorNotification.THRESHOLD_VALUE_EXCEEDED };
MBeanNotificationInfo info = new MBeanNotificationInfo(types, Notification.class.getName(),
"rate-limited request processed");
return new MBeanNotificationInfo[] { info };
}

/**
* {@inheritDoc}
*/
@Override
public Token getToken(Key key) {
Token token = delegate.getToken(key);

if (token.isUsable()) {
sendNotification(new Notification(JMX_MONITOR_RATE_LIMIT_SERVICE_TYPE, this, getSequenceNumber(),
"allowed request " + key));
} else {
sendNotification(new Notification(MonitorNotification.THRESHOLD_VALUE_EXCEEDED, this, getSequenceNumber(),
"denied request " + key));
}

return token;
}

/**
* {@inheritDoc}
*/
public boolean isEnabled() {
return this.delegate.isEnabled();
}

/**
* {@inheritDoc}
*/
public void setEnabled(boolean enabled) {
this.delegate.setEnabled(enabled);
}

/**
* Returns the next sequence number for the JMX notification.
*
* @return the next positive sequence number
*/
private synchronized long getSequenceNumber() {
return ++this.sequenceNumber;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2009 James Abley
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.eternus.ratelimit.jmx;

import com.eternus.ratelimit.Enablable;
import com.eternus.ratelimit.RateLimiter;

/**
* MBean interface for managing {@link RateLimiter}s.
*
* @author jabley
*
*/
public interface ManagedRateLimiterMBean extends Enablable {

}

0 comments on commit bd83012

Please sign in to comment.