基于FLEX,MINA消息推送核心代码

阅读更多
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.webframe.web.util.WebFrameUtils;

import com.berheley.bi.grp.nio.messageUtil.IProcessor;
import com.berheley.bi.grp.nio.messageUtil.IWebMessageHelper;
import com.berheley.bi.grp.nio.messageUtil.SocketManager;

/**
 * 
 * 类功能描述:消息推送核心类
 *
 * @author mengqingyu 
 * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp  $
 * Create:  2013-7-30 下午03:35:40
 */
public class ServerHandler extends IoHandlerAdapter {

	private Log log = LogFactory.getLog(ServerHandler.class);

	private IWebMessageHelper webMessageHelper;
	
	public IWebMessageHelper getWebMessageHelper() {
		return webMessageHelper;
	}

	public void setWebMessageHelper(IWebMessageHelper webMessageHelper) {
		this.webMessageHelper = webMessageHelper;
	}

	/**
	 * 接受消息,登陆时执行此方法
	 */
	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		String msg = message.toString().trim();
		if (msg.startsWith("{")) {
			String username = JSONObject.fromObject(msg).getString("sd").toString();
			SocketManager.getInstance().addUserToOnlineMap(username, session);
			List> userList = webMessageHelper.findUserByBinding(username);
			if(userList.size()>0){
				String jsonMessage = webMessageHelper.changeListToJSONArray(webMessageHelper.findMobileByBinding()).toString();
				webMessageHelper.sendMessage(username, jsonMessage, (IProcessor) WebFrameUtils.getBean("webMessage"));
			}
		}
		log.info("<-------------------- Received Message: " + msg + "--------------------->");
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		super.sessionCreated(session);
		log.info("<--------------------MINA Server Connection Created--------------------->");

	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		super.sessionOpened(session);
		session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 1800); //读写 通道均在1800 秒内无任何操作就进入空闲状态  
		log.info("<--------------------MINA Server Connection Opened--------------------->");
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		this.removeUserBySessionId(session);
		session.close(true);
		log.info("<--------------------MINA Server Connection Closed--------------------->");
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		this.removeUserBySessionId(session);
		session.close(true);
		log.info("Session Id: " + session.getId() + "" + cause.getMessage());
		log.error(cause);
	}

	/**
	 * 超出空闲时间执行
	 */
	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		this.removeUserBySessionId(session);
		session.close(true);
		log.info("<--------------------Session Idle--------------------->");
	}

	/**
	 * 
	 * @function:移除在线列表,关闭session
	 * @param session
	 * @author: mengqingyu    2013-7-30 下午03:35:22
	 */
	private void removeUserBySessionId(IoSession session) {
		SocketManager.getInstance().removeUserBySessionId(session.getId());
	}

}

import java.beans.PropertyEditorSupport;
import java.nio.charset.Charset;

/**
 * 
 * 类功能描述:中文编码
 * 
 * @author mengqingyu 
 * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp $
 *          Create: 2014-1-15 下午01:42:30
 */
public class CharsetEditor extends PropertyEditorSupport {

	private Object value;

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (text != null)
			this.value = Charset.forName(text);
		else
			this.value = Charset.forName("UTF-8");
	}

	@Override
	public Object getValue() {
		return value;
	}
}

import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.webframe.web.util.WebFrameUtils;

/**
 * 
 * 类功能描述:获取端口号
 *
 * @author mengqingyu 
 * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp  $
 * Create:  2013-7-30 下午03:42:04
 */
public class MinaHelper {
	
	private static final NioSocketAcceptor acc = (NioSocketAcceptor) WebFrameUtils.getBean("ioAcceptor");
	
	public static int getMinaPort() {
		return acc.getDefaultLocalAddress().getPort();
	}
}

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.session.IoSession;

import com.berheley.bi.grp.nio.mina.ServerHandler;

/**
 * 
 * 类功能描述:消息处理器抽象类
 * 
 * @author mengqingyu 
 * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp $
 *          Create: 2013-7-30 下午03:52:59
 */
public abstract class AbstractProcessor implements IProcessor {
	protected Log log = LogFactory.getLog(ServerHandler.class);

	protected SocketManager manager = SocketManager.getInstance();

	public abstract void process(JSONObject jsonMessage, IoSession session);
}

import net.sf.json.JSONObject;

import org.apache.mina.core.session.IoSession;

/**
 * 
 * 类功能描述:统一消息处理接口
 * 
 * @author mengqingyu 
 * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp $
 *          Create: 2014-1-15 下午01:42:02
 */
public interface IProcessor {
	/**
	 * 
	 * @function:统一消息处理接口
	 * @param jsonMessage
	 * @param session
	 * @author: mengqingyu 2013-7-30 下午03:56:10
	 */
	public void process(JSONObject jsonMessage, IoSession session);
}

import net.sf.json.JSONObject;

import org.apache.mina.core.session.IoSession;
import org.springframework.stereotype.Component;

/**
 * 
 * 类功能描述:消息提醒处理器
 *
 * @author mengqingyu 
 * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp  $
 * Create:  2013-7-3 下午04:04:50
 */
@Component("webMessage")
public class WebMessageProcessor extends AbstractProcessor {
	
	@Override
	public void process(JSONObject jsonMessage, IoSession session) {
		if (jsonMessage != null) {
			IoSession userSession = manager.getUserSession(jsonMessage.getString("sender"));
			if (userSession != null) {
				userSession.write(jsonMessage);
				log.info("***********************【页面提醒】:" + jsonMessage);
			}
		}
	}
}





	
	
	
		
		
		
	
	
	
		
	
	
	
	
		
			
				
				
			
		
	

	
	
		
			
				
					UTF-8
				
				
				
			
		
	
	
	
	
	

	
	
		
			
				
					
				
				
					
				
			
		
	


mina.properties文件内容
mina.server.port=9094
mina.flex.port=840
  • 消息推送核心代码.rar (838 KB)
  • 下载次数: 20

你可能感兴趣的:(mina,java)