⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jmxsession.java

📁 银行项目为后台socket通信写的程序
💻 JAVA
字号:
/****************************************************************************
 * Package		: com.ecSolutions.ecAppServer.server.session.jmx
 * File			: JmxSession.java
 * Create Date  : 2007-7-20
 * Author		: Steven Chen
 * 
 * Copyright(C) 2006 ecSolutions(shanghai) Co.,Limited.All Rights Reserved.
 *			
 ***************************************************************************/
package com.ecSolutions.ecAppServer.server.session.jmx;

import java.net.SocketAddress;
import java.util.Map;

import javax.management.NotCompliantMBeanException;
import javax.management.StandardMBean;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenDataException;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;

import com.ecSolutions.ecAppServer.server.PacketDecoder;
import com.ecSolutions.ecAppServer.server.PacketEncoder;
import com.ecSolutions.ecAppServer.server.Session;
import com.ecSolutions.ecAppServer.server.SessionFilter;
import com.ecSolutions.ecAppServer.server.SessionHandler;
import com.ecSolutions.ecAppServer.server.filter.StatisticFilter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <code>Session</code> which support jmx.
 * 
 * @author Steven Chen
 * @version $Id: JmxSession.java,v 1.2 2007/07/26 03:42:42 stevenchen Exp $
 */
public class JmxSession extends StandardMBean implements SessionMBean {

    private static final Log log = LogFactory.getLog(JmxSession.class);

    private final Session session;

    public JmxSession(Session session) throws NotCompliantMBeanException {
        super(SessionMBean.class);
        this.session = session;
    }

    private String getClassName(Object obj) {
        return obj == null ? null : obj.getClass().getName();
    }

    private Object newInstance(String className) {
        try {
            return Class.forName(className).newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String getSessionType() {
        return session.getSessionType().toString();
    }

    public SocketAddress getLocalAddress() {
        return session.getLocalAddress();
    }

    public SocketAddress getRemoteAddress() {
        return session.getRemoteAddress();
    }

    public void setLocalAddress(SocketAddress address) {
        session.setLocalAddress(address);
    }

    public void setRemoteAddress(SocketAddress address) {
        session.setRemoteAddress(address);
    }

    public void setPacketDecoder(String decoderClassName) {
        session.setPacketDecoder((PacketDecoder) newInstance(decoderClassName));
    }

    public String getPacketDecoder() {
        return getClassName(session.getPacketDecoder());
    }

    public void setPacketEncoder(String encoderClassName) {
        session.setPacketEncoder((PacketEncoder) newInstance(encoderClassName));
    }

    public String getPacketEncoder() {
        return getClassName(session.getPacketEncoder());
    }

    public Map getAttributes() {
        return session.getAttributes();
    }

    public int getSessionTimeout() {
        return session.getSessionTimeout();
    }

    public void setSessionTimeout(int timeout) {
        session.setSessionTimeout(timeout);
    }

    public boolean isStarted() {
        return session.isStarted();
    }

    public void start() {
        session.start();
    }

    public void close() {
        session.close();
    }

    public String[] getSessionFilters() {
        SessionFilter[] filters = session.getSessionFilters();
        String[] result = new String[filters.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = getClassName(filters[i]);
        }
        return result;
    }

    public void removeSessionFilter(int index) {
        session.removeSessionFilter(session.getSessionFilter(index));
    }

    public void addSessionFilter(String filterClassName) {
        session.addSessionFilter((SessionFilter) newInstance(filterClassName));
    }

    public void addSessionFilter(int index, String filterClassName) {
        session.addSessionFilter(index,
                (SessionFilter) newInstance(filterClassName));
    }

    public String getSessionHandler() {
        return getClassName(session.getSessionHandler());
    }

    public void setSessionHandler(String handlerClassName) {
        session
                .setSessionHandler((SessionHandler) newInstance(handlerClassName));
    }

    private static final String[] STAT_NAMES = new String[] { "ReceivedBytes",
            "SentBytes", "ElapsedTime", "AvgReceiveSpeed", "AvgSendSpeed",
            "ReceiveSpeed", "SendSpeed" };
    private static final String[] STAT_DESCS = STAT_NAMES;
    private static final OpenType[] STAT_TYPES = new OpenType[] {
            SimpleType.LONG, SimpleType.LONG, SimpleType.LONG,
            SimpleType.DOUBLE, SimpleType.DOUBLE, SimpleType.DOUBLE,
            SimpleType.DOUBLE };

    private static final CompositeType STAT_COMPOSITE_TYPE;

    static {
        CompositeType type = null;
        try {
            type = new CompositeType("cindy.statistic", "statistic",
                    STAT_NAMES, STAT_DESCS, STAT_TYPES);
        } catch (OpenDataException e) {
            log.error(e, e);
        }
        STAT_COMPOSITE_TYPE = type;
    }

    public CompositeData getStatistic() {
        SessionFilter[] filters = session.getSessionFilters();
        for (int i = 0; i < filters.length; i++) {
            if (filters[i] instanceof StatisticFilter) {
                StatisticFilter stat = (StatisticFilter) filters[i];
                try {
                    return new CompositeDataSupport(STAT_COMPOSITE_TYPE,
                            STAT_NAMES, new Object[] {
                                    new Long(stat.getReceivedBytes()),
                                    new Long(stat.getSentBytes()),
                                    new Long(stat.getElapsedTime()),
                                    new Double(stat.getAvgReceiveSpeed()),
                                    new Double(stat.getAvgSendSpeed()),
                                    new Double(stat.getReceiveSpeed()),
                                    new Double(stat.getSendSpeed()) });
                } catch (OpenDataException e) {
                    log.error(e, e);
                }
            }
        }
        return null;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -