Commit 10696f02 authored by ncampbell's avatar ncampbell

JMX intrumentation of SocketAccept. Increments an internal

counter for each socket accept that the jvm receives since it
was started.  Also shows how to register an mbean with the 
server.

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/branches@2954 b35dd754-fafc-0310-a699-88a17e54d16e
parent 54287aea
...@@ -27,7 +27,7 @@ import java.net.UnknownHostException; ...@@ -27,7 +27,7 @@ import java.net.UnknownHostException;
* Implements a network front end with a dedicated thread reading * Implements a network front end with a dedicated thread reading
* each incoming socket. * each incoming socket.
*/ */
public class SSLSocketAcceptThread extends Thread { public class SSLSocketAcceptThread extends Thread implements SocketAcceptMBean {
/** /**
* The default Jabber socket * The default Jabber socket
...@@ -64,6 +64,8 @@ public class SSLSocketAcceptThread extends Thread { ...@@ -64,6 +64,8 @@ public class SSLSocketAcceptThread extends Thread {
*/ */
private static final int MAX_SSL_EXCEPTIONS = 10; private static final int MAX_SSL_EXCEPTIONS = 10;
private long acceptCount = 0;
/** /**
* Creates an instance using the default port, TLS transport security, and * Creates an instance using the default port, TLS transport security, and
* JVM defaults for all security settings. * JVM defaults for all security settings.
...@@ -143,6 +145,7 @@ public class SSLSocketAcceptThread extends Thread { ...@@ -143,6 +145,7 @@ public class SSLSocketAcceptThread extends Thread {
Socket sock = serverSocket.accept(); Socket sock = serverSocket.accept();
Log.debug("SSL Connect " + sock.toString()); Log.debug("SSL Connect " + sock.toString());
connManager.addSocket(sock, true, serverPort); connManager.addSocket(sock, true, serverPort);
this.acceptCount++;
} }
catch (SSLException se) { catch (SSLException se) {
long exceptionTime = System.currentTimeMillis(); long exceptionTime = System.currentTimeMillis();
...@@ -185,4 +188,11 @@ public class SSLSocketAcceptThread extends Thread { ...@@ -185,4 +188,11 @@ public class SSLSocketAcceptThread extends Thread {
// we don't care, no matter what, the socket should be dead // we don't care, no matter what, the socket should be dead
} }
} }
/**
* @see org.jivesoftware.messenger.net.SocketAcceptMBean#getAcceptCount()
*/
public long getAcceptCount() {
return this.acceptCount;
}
} }
/**
* Copyright Noah Campbell 2004
*/
package org.jivesoftware.messenger.net;
/**
* @author Noah Campbell
* @version 1.0
*/
public interface SocketAcceptMBean {
/**
* Return the total number of times this socket has accepted an incoming
* socket since the start of the JVM.
*
* @return acceptCount
*/
long getAcceptCount();
}
...@@ -27,7 +27,7 @@ import java.net.Socket; ...@@ -27,7 +27,7 @@ import java.net.Socket;
* *
* @author Iain Shigeoka * @author Iain Shigeoka
*/ */
public class SocketAcceptThread extends Thread { public class SocketAcceptThread extends Thread implements SocketAcceptMBean {
/** /**
* The default XMPP port for clients. * The default XMPP port for clients.
...@@ -66,6 +66,15 @@ public class SocketAcceptThread extends Thread { ...@@ -66,6 +66,15 @@ public class SocketAcceptThread extends Thread {
private ConnectionManager connManager; private ConnectionManager connManager;
private long acceptCount = 0;
/**
* Construct a new <code>SocketAcceptThread</code>.
*
* @param connManager
* @param serverPort
* @throws IOException
*/
public SocketAcceptThread(ConnectionManager connManager, ServerPort serverPort) public SocketAcceptThread(ConnectionManager connManager, ServerPort serverPort)
throws IOException { throws IOException {
super("Socket Listener at port " + serverPort.getPort()); super("Socket Listener at port " + serverPort.getPort());
...@@ -122,6 +131,7 @@ public class SocketAcceptThread extends Thread { ...@@ -122,6 +131,7 @@ public class SocketAcceptThread extends Thread {
* About as simple as it gets. The thread spins around an accept * About as simple as it gets. The thread spins around an accept
* call getting sockets and handing them to the SocketManager. * call getting sockets and handing them to the SocketManager.
*/ */
@Override
public void run() { public void run() {
while (notTerminated) { while (notTerminated) {
try { try {
...@@ -129,6 +139,7 @@ public class SocketAcceptThread extends Thread { ...@@ -129,6 +139,7 @@ public class SocketAcceptThread extends Thread {
if (sock != null) { if (sock != null) {
Log.debug("Connect " + sock.toString()); Log.debug("Connect " + sock.toString());
connManager.addSocket(sock, false, serverPort); connManager.addSocket(sock, false, serverPort);
acceptCount++;
} }
} }
catch (IOException ie) { catch (IOException ie) {
...@@ -153,4 +164,11 @@ public class SocketAcceptThread extends Thread { ...@@ -153,4 +164,11 @@ public class SocketAcceptThread extends Thread {
// we don't care, no matter what, the socket should be dead // we don't care, no matter what, the socket should be dead
} }
} }
/**
* @see org.jivesoftware.messenger.net.SocketAcceptMBean#getAcceptCount()
*/
public long getAcceptCount() {
return this.acceptCount;
}
} }
...@@ -19,15 +19,27 @@ import org.jivesoftware.util.Log; ...@@ -19,15 +19,27 @@ import org.jivesoftware.util.Log;
import org.jivesoftware.util.JiveGlobals; import org.jivesoftware.util.JiveGlobals;
import java.io.IOException; import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Properties;
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;
public class ConnectionManagerImpl extends BasicModule implements ConnectionManager { public class ConnectionManagerImpl extends BasicModule implements ConnectionManager {
private MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
private SocketAcceptThread socketThread; private SocketAcceptThread socketThread;
private SSLSocketAcceptThread sslSocketThread; private SSLSocketAcceptThread sslSocketThread;
private SocketAcceptThread componentSocketThread; private SocketAcceptThread componentSocketThread;
...@@ -92,6 +104,8 @@ public class ConnectionManagerImpl extends BasicModule implements ConnectionMana ...@@ -92,6 +104,8 @@ public class ConnectionManagerImpl extends BasicModule implements ConnectionMana
serverSocketThread.setDaemon(true); serverSocketThread.setDaemon(true);
serverSocketThread.start(); serverSocketThread.start();
register("serverListener", serverPort, serverSocketThread);
List params = new ArrayList(); List params = new ArrayList();
params.add(Integer.toString(serverSocketThread.getPort())); params.add(Integer.toString(serverSocketThread.getPort()));
Log.info(LocaleUtils.getLocalizedString("startup.server", params)); Log.info(LocaleUtils.getLocalizedString("startup.server", params));
...@@ -104,6 +118,26 @@ public class ConnectionManagerImpl extends BasicModule implements ConnectionMana ...@@ -104,6 +118,26 @@ public class ConnectionManagerImpl extends BasicModule implements ConnectionMana
} }
} }
private void register(String type, ServerPort serverPort, Object obj) throws MalformedObjectNameException, NotCompliantMBeanException, InstanceAlreadyExistsException, MBeanRegistrationException {
Properties props = new Properties();
props.setProperty("port", Integer.toString(serverPort.getPort()));
//
// props.setProperty("interface", serverPort.getInterfaceName());
// props.setProperty("ipaddr", serverPort.getIPAddress());
if(serverPort.getSecurityType() != null) {
props.setProperty("securityType", serverPort.getSecurityType());
} else {
props.setProperty("securityType", "NONE");
}
props.setProperty("type", type);
ObjectName objectName = new ObjectName("org.jivesoftware.messenger", props);
StandardMBean mbean = new StandardMBean(obj, SocketAcceptMBean.class);
mbeanServer.registerMBean(mbean, objectName);
}
private void stopServerListener() { private void stopServerListener() {
if (serverSocketThread != null) { if (serverSocketThread != null) {
serverSocketThread.shutdown(); serverSocketThread.shutdown();
...@@ -125,6 +159,7 @@ public class ConnectionManagerImpl extends BasicModule implements ConnectionMana ...@@ -125,6 +159,7 @@ public class ConnectionManagerImpl extends BasicModule implements ConnectionMana
componentSocketThread.setDaemon(true); componentSocketThread.setDaemon(true);
componentSocketThread.start(); componentSocketThread.start();
register("componentListener", serverPort, componentSocketThread);
List params = new ArrayList(); List params = new ArrayList();
params.add(Integer.toString(componentSocketThread.getPort())); params.add(Integer.toString(componentSocketThread.getPort()));
Log.info(LocaleUtils.getLocalizedString("startup.component", params)); Log.info(LocaleUtils.getLocalizedString("startup.component", params));
...@@ -158,6 +193,8 @@ public class ConnectionManagerImpl extends BasicModule implements ConnectionMana ...@@ -158,6 +193,8 @@ public class ConnectionManagerImpl extends BasicModule implements ConnectionMana
socketThread.setDaemon(true); socketThread.setDaemon(true);
socketThread.start(); socketThread.start();
register("clientListener", serverPort, socketThread);
List params = new ArrayList(); List params = new ArrayList();
params.add(Integer.toString(socketThread.getPort())); params.add(Integer.toString(socketThread.getPort()));
Log.info(LocaleUtils.getLocalizedString("startup.plain", params)); Log.info(LocaleUtils.getLocalizedString("startup.plain", params));
...@@ -195,6 +232,7 @@ public class ConnectionManagerImpl extends BasicModule implements ConnectionMana ...@@ -195,6 +232,7 @@ public class ConnectionManagerImpl extends BasicModule implements ConnectionMana
sslSocketThread.setDaemon(true); sslSocketThread.setDaemon(true);
sslSocketThread.start(); sslSocketThread.start();
register("SSLClientListner", serverPort, sslSocketThread);
List params = new ArrayList(); List params = new ArrayList();
params.add(Integer.toString(sslSocketThread.getPort())); params.add(Integer.toString(sslSocketThread.getPort()));
Log.info(LocaleUtils.getLocalizedString("startup.ssl", params)); Log.info(LocaleUtils.getLocalizedString("startup.ssl", params));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment