You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2010/11/10 20:42:46 UTC
svn commit: r1033675 - in
/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver:
CommandHandlerResultJMXMonitor.java CommandHandlerStats.java
CommandHandlerStatsMBean.java HookResultJMXMonitor.java
Author: norman
Date: Wed Nov 10 19:42:46 2010
New Revision: 1033675
URL: http://svn.apache.org/viewvc?rev=1033675&view=rev
Log:
Expose JMX stats for SMTP CommandHandlers
Added:
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerResultJMXMonitor.java
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerStats.java
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerStatsMBean.java
Modified:
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookResultJMXMonitor.java
Added: james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerResultJMXMonitor.java
URL: http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerResultJMXMonitor.java?rev=1033675&view=auto
==============================================================================
--- james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerResultJMXMonitor.java (added)
+++ james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerResultJMXMonitor.java Wed Nov 10 19:42:46 2010
@@ -0,0 +1,91 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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 org.apache.james.smtpserver;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.james.protocols.api.CommandHandler;
+import org.apache.james.protocols.api.ExtensibleHandler;
+import org.apache.james.protocols.api.ProtocolSession;
+import org.apache.james.protocols.api.Response;
+import org.apache.james.protocols.api.ResponseResultHandler;
+import org.apache.james.protocols.api.WiringException;
+import org.apache.james.protocols.smtp.SMTPResponse;
+import org.apache.james.protocols.smtp.SMTPSession;
+
+/**
+ * Expose JMX statistics for {@link CommandHandler}
+ *
+ */
+public class CommandHandlerResultJMXMonitor implements ResponseResultHandler<SMTPResponse, SMTPSession>, ExtensibleHandler {
+
+ private Map<String, CommandHandlerStats> cStats = new HashMap<String, CommandHandlerStats>();
+
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.protocols.api.ResponseResultHandler#onResponse(org.apache.james.protocols.api.ProtocolSession, org.apache.james.protocols.api.Response, org.apache.james.protocols.api.CommandHandler)
+ */
+ public Response onResponse(ProtocolSession session, SMTPResponse response, CommandHandler<SMTPSession> handler) {
+ String name = handler.getClass().getName();
+ CommandHandlerStats stats = cStats.get(name);
+ if (stats != null) {
+ stats.increment(response);
+ }
+ return response;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.protocols.api.ExtensibleHandler#getMarkerInterfaces()
+ */
+ public List<Class<?>> getMarkerInterfaces() {
+ List<Class<?>> marker = new ArrayList<Class<?>>();
+ marker.add(CommandHandler.class);
+ return marker;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.protocols.api.ExtensibleHandler#wireExtensions(java.lang.Class, java.util.List)
+ */
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ public void wireExtensions(Class<?> interfaceName, List<?> extension) throws WiringException {
+ if (interfaceName.equals(CommandHandler.class)) {
+ // add stats for all hooks
+ for (int i = 0; i < extension.size(); i++ ) {
+ CommandHandler c = (CommandHandler) extension.get(i);
+ if (equals(c) == false) {
+ String cName = c.getClass().getName();
+ try {
+ Collection<String> col = c.getImplCommands();
+ cStats.put(cName, new CommandHandlerStats(cName, col.toArray(new String[col.size()])));
+ } catch (Exception e) {
+ throw new WiringException("Unable to wire Hooks", e);
+ }
+ }
+ }
+ }
+ }
+
+}
Added: james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerStats.java
URL: http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerStats.java?rev=1033675&view=auto
==============================================================================
--- james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerStats.java (added)
+++ james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerStats.java Wed Nov 10 19:42:46 2010
@@ -0,0 +1,139 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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 org.apache.james.smtpserver;
+
+import java.lang.management.ManagementFactory;
+import java.util.concurrent.atomic.AtomicLong;
+
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.MBeanRegistrationException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.NotCompliantMBeanException;
+import javax.management.ObjectName;
+import javax.management.StandardMBean;
+
+import org.apache.james.lifecycle.Disposable;
+import org.apache.james.protocols.api.CommandHandler;
+import org.apache.james.protocols.smtp.SMTPResponse;
+
+/**
+ * Expose statistics for {@link CommandHandler} via JMX
+ *
+ */
+public class CommandHandlerStats extends StandardMBean implements CommandHandlerStatsMBean, Disposable {
+
+ private AtomicLong temp = new AtomicLong(0);
+ private AtomicLong perm = new AtomicLong(0);
+ private AtomicLong ok = new AtomicLong(0);
+ private AtomicLong all = new AtomicLong(0);
+
+ private String name;
+ private String handlerName;
+ private MBeanServer mbeanserver;
+ private String[] commands;
+
+ public CommandHandlerStats(String handlerName, String[] commands) throws NotCompliantMBeanException, MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException {
+ super(CommandHandlerStatsMBean.class);
+ this.handlerName = handlerName;
+ this.commands = commands;
+
+ name = "org.apache.james:type=server,name=smtpserver,handler=commandhandler,commandhandler=" + handlerName;
+ mbeanserver = ManagementFactory.getPlatformMBeanServer();
+ ObjectName baseObjectName = new ObjectName(name);
+ mbeanserver.registerMBean(this, baseObjectName);
+ }
+
+ public void increment(SMTPResponse response) {
+ try {
+ String code = response.getRetCode();
+ char c = code.charAt(0) ;
+ if (c == '5') {
+ perm.incrementAndGet();
+ } else if (c == '4') {
+ temp.incrementAndGet();
+ } else if ( c == '2' || c == '3') {
+ ok.incrementAndGet();
+ }
+ all.incrementAndGet();
+ } catch (NumberFormatException e) {
+ // should never happen
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.smtpserver.CommandHandlerStatsMBean#getTemporaryError()
+ */
+ public long getTemporaryError() {
+ return temp.get();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.smtpserver.CommandHandlerStatsMBean#getPermantError()
+ */
+ public long getPermantError() {
+ return perm.get();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.smtpserver.CommandHandlerStatsMBean#getOk()
+ */
+ public long getOk() {
+ return ok.get();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.smtpserver.CommandHandlerStatsMBean#getAll()
+ */
+ public long getAll() {
+ return all.get();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.smtpserver.CommandHandlerStatsMBean#getName()
+ */
+ public String getName() {
+ return handlerName;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.lifecycle.Disposable#dispose()
+ */
+ public void dispose() {
+ try {
+ mbeanserver.unregisterMBean(new ObjectName(name));
+ } catch (Exception e) {
+ // ignore here;
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.smtpserver.CommandHandlerStatsMBean#getCommands()
+ */
+ public String[] getCommands() {
+ return commands;
+ }
+}
Added: james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerStatsMBean.java
URL: http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerStatsMBean.java?rev=1033675&view=auto
==============================================================================
--- james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerStatsMBean.java (added)
+++ james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CommandHandlerStatsMBean.java Wed Nov 10 19:42:46 2010
@@ -0,0 +1,69 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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 org.apache.james.smtpserver;
+
+
+/**
+ * JMX MBean for CommandHandler
+ *
+ */
+public interface CommandHandlerStatsMBean {
+
+ /**
+ * Return the count of temporary errors returned by the handler
+ *
+ * @return tempCount
+ */
+ public long getTemporaryError();
+
+ /**
+ * Return the count of permanent errors returned by the handler
+ *
+ * @return permCount
+ */
+ public long getPermantError();
+
+ /**
+ * Return the count of successful handling returned by the handler
+ *
+ * @return tempCount
+ */
+ public long getOk();
+
+ /**
+ * Return the count of all processed transactions by the handler
+ *
+ * @return all
+ */
+ public long getAll();
+
+ /**
+ * Return the name of the handler
+ *
+ * @return name
+ */
+ public String getName();
+
+ /**
+ * Return all implemented commands by this handler
+ *
+ * @return commands
+ */
+ public String[] getCommands();
+}
Modified: james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookResultJMXMonitor.java
URL: http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookResultJMXMonitor.java?rev=1033675&r1=1033674&r2=1033675&view=diff
==============================================================================
--- james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookResultJMXMonitor.java (original)
+++ james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/HookResultJMXMonitor.java Wed Nov 10 19:42:46 2010
@@ -49,7 +49,9 @@ public class HookResultJMXMonitor implem
Object hook) {
String hookName = hook.getClass().getName();
HookStats stats = hookStats.get(hookName);
- stats.increment(result.getResult());
+ if (stats != null) {
+ stats.increment(result.getResult());
+ }
return result;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org