[jboss-cvs] JBossAS SVN: r100701 - trunk/varia/src/main/java/org/jboss/monitor/services.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Feb 8 09:58:39 EST 2010
Author: alesj
Date: 2010-02-08 09:58:38 -0500 (Mon, 08 Feb 2010)
New Revision: 100701
Modified:
trunk/varia/src/main/java/org/jboss/monitor/services/ScriptingListener.java
Log:
Remove oswego lib dependency.
Modified: trunk/varia/src/main/java/org/jboss/monitor/services/ScriptingListener.java
===================================================================
--- trunk/varia/src/main/java/org/jboss/monitor/services/ScriptingListener.java 2010-02-08 14:55:39 UTC (rev 100700)
+++ trunk/varia/src/main/java/org/jboss/monitor/services/ScriptingListener.java 2010-02-08 14:58:38 UTC (rev 100701)
@@ -23,6 +23,7 @@
import java.util.LinkedList;
import java.util.List;
+import java.util.concurrent.atomic.AtomicLong;
import javax.management.MBeanServer;
import javax.management.Notification;
@@ -34,32 +35,30 @@
import org.jboss.monitor.alarm.AlarmManager;
import org.jboss.system.ListenerServiceMBeanSupport;
-import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
-
/**
* A simple listener that can subscribe for any combination
* of notifications, and asynchronously process them using
* a script written using any of the languages supported by
* the apache Bean Scripting Framework (BSF).
- *
+ *
* The following variables are setup for the script to use:
- *
+ *
* "log" - service Logger
* "server" - the MBeanServer
* "manager" - alarm manager helper
*
* "notification" - the Notification to be processed
* "handback" - the Object sent with the notification
- *
+ *
* By setting up a Timer using the TimerService to periodicaly
* emit notifications, we can use those notifications as triggers
* for performing any sort of polling operation.
- *
+ *
* One of the intented uses of this service is to use the "manager"
* (see org.jboss.monitor.alarm.AlarmManager) in the script,
* help maintain a list of active system alarms in the
* ActiveAlarmTable service.
- *
+ *
* @author <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
* @version $Revision$
*/
@@ -67,10 +66,10 @@
implements ScriptingListenerMBean
{
// Private Data --------------------------------------------------
-
+
/** The Script */
private String script;
-
+
/** The language the script is written into */
private String language;
@@ -79,47 +78,47 @@
/** Set to deliver notification directly */
private ObjectName targetListener;
-
+
/** The number of notifications received/enqueued */
- private SynchronizedLong notificationsReceived;
-
+ private AtomicLong notificationsReceived;
+
/** The number of notifications processed/dequeued by the script */
- private SynchronizedLong notificationsProcessed;
-
+ private AtomicLong notificationsProcessed;
+
/** The total time (msecs) spent executing the script */
- private SynchronizedLong totalProcessingTime;
-
+ private AtomicLong totalProcessingTime;
+
/** Bean Scripting Framework entry point */
private BSFManager manager;
-
+
/** Enqueued notifications */
private List queue;
-
+
/** Signals stop processing */
private boolean stopRequested;
/** The thread running the script */
private Thread processorThread;
-
+
/** The alarm manager helper */
private AlarmManager alm = new AlarmManager(this);
-
+
// Constructors --------------------------------------------------
-
+
/**
* CTOR
*/
public ScriptingListener()
{
queue = new LinkedList();
-
- notificationsReceived = new SynchronizedLong(0);
- notificationsProcessed = new SynchronizedLong(0);
- totalProcessingTime = new SynchronizedLong(0);
+
+ notificationsReceived = new AtomicLong(0);
+ notificationsProcessed = new AtomicLong(0);
+ totalProcessingTime = new AtomicLong(0);
}
-
+
// ScriptNotificationListenerMBean Implementation ----------------
-
+
/**
* @jmx:managed-attribute
*/
@@ -135,7 +134,7 @@
{
return script;
}
-
+
/**
* @jmx:managed-attribute
*/
@@ -151,7 +150,7 @@
{
return language;
}
-
+
/**
* @jmx:managed-attribute
*/
@@ -167,7 +166,7 @@
{
return this.dynamicSubscriptions;
}
-
+
/**
* @jmx:managed-attribute
*/
@@ -183,7 +182,7 @@
{
return notificationsProcessed.get();
}
-
+
/**
* @jmx:managed-attribute
*/
@@ -191,57 +190,57 @@
{
return totalProcessingTime.get();
}
-
+
/**
* @jmx:managed-attribute
*/
public long getAverageProcessingTime()
{
long processed = notificationsProcessed.get();
-
+
return (processed == 0) ? 0 : totalProcessingTime.get() / processed;
}
-
+
// Lifecycle control (ServiceMBeanSupport) -----------------------
-
+
/**
- * Start
+ * Start
*/
public void startService() throws Exception
{
log.debug("Initializing BSFManager for language '" + language + "'");
-
+
// This is needed until BSF adds it
BSFManager.registerScriptingEngine(
- "groovy",
- "org.codehaus.groovy.bsf.GroovyEngine",
+ "groovy",
+ "org.codehaus.groovy.bsf.GroovyEngine",
new String[] { "groovy", "gy" }
- );
-
+ );
+
// I suppose we need one BSFManager per processing thread
manager = new BSFManager();
-
+
manager.setClassLoader(Thread.currentThread().getContextClassLoader());
manager.loadScriptingEngine(language);
manager.declareBean("log", log, Logger.class);
manager.declareBean("server", server, MBeanServer.class);
manager.declareBean("manager", alm, AlarmManager.class);
-
+
// test with a dummy notification first, to see if the script is valid
Notification testNotification = new Notification("jboss.script.test", serviceName, 0);
manager.declareBean("notification", testNotification, Notification.class);
manager.declareBean("handback", "", Object.class);
-
+
manager.exec(language, "in-memory-script", 0, 0, script);
-
+
// Start the ScriptProcessor in its own thread
processorThread = new Thread(new ScriptProcessor(), "ScriptProcessor[" + serviceName + "]");
processorThread.start();
-
+
// subscribe for notifications
super.subscribe(dynamicSubscriptions);
}
-
+
/**
* Stop
*/
@@ -249,18 +248,18 @@
{
// unsubscribe for notifications
super.unsubscribe();
-
+
log.debug("Stopping " + processorThread.getName());
-
+
// tell the ScriptProcessor to stop
stopRequested = true;
-
+
// notify the processing thread in case it is waiting on the queue
synchronized (queue)
{
- queue.notify();
+ queue.notify();
}
-
+
try
{
// wait for the processor to finish, but not for too long
@@ -271,35 +270,35 @@
// set interrupted status
Thread.currentThread().interrupt();
}
-
+
// cleanup
queue.clear();
manager.terminate();
}
-
+
// ListenerServiceMBeanSupport overrides -------------------------
-
+
/**
* Overriden to add handling!
*/
public void handleNotification2(Notification notification, Object handback)
{
// count the received notifications
- notificationsReceived.increment();
-
+ notificationsReceived.incrementAndGet();
+
// append the received notification to the end of the list,
- // for processing from a different thread
+ // for processing from a different thread
synchronized (queue)
{
queue.add(new QueueEntry(notification, handback));
-
+
// hint to the processing thread to kick-in
queue.notify();
}
}
-
+
// Inner ---------------------------------------------------------
-
+
/**
* Simple data holder
*/
@@ -307,14 +306,14 @@
{
public Notification notification;
public Object handback;
-
+
public QueueEntry(Notification notification, Object handback)
{
this.notification = notification;
this.handback = handback;
}
}
-
+
/**
* Inner class to encapsulate script execution logic
*/
@@ -375,8 +374,8 @@
// measure time spent processing the script
long stop = System.currentTimeMillis();
- totalProcessingTime.add(stop - start);
- notificationsProcessed.increment();
+ totalProcessingTime.addAndGet(stop - start);
+ notificationsProcessed.incrementAndGet();
}
log.debug("Stopped thread: " + name);
}
More information about the jboss-cvs-commits
mailing list