- 浏览: 7378102 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (1546)
- 企业中间件 (236)
- 企业应用面临的问题 (236)
- 小布Oracle学习笔记汇总 (36)
- Spring 开发应用 (54)
- IBatis开发应用 (16)
- Oracle基础学习 (23)
- struts2.0 (41)
- JVM&ClassLoader&GC (16)
- JQuery的开发应用 (17)
- WebService的开发应用 (21)
- Java&Socket (44)
- 开源组件的应用 (254)
- 常用Javascript的开发应用 (28)
- J2EE开发技术指南 (163)
- EJB3开发应用 (11)
- GIS&Mobile&MAP (36)
- SWT-GEF-RCP (52)
- 算法&数据结构 (6)
- Apache开源组件研究 (62)
- Hibernate 学习应用 (57)
- java并发编程 (59)
- MySQL&Mongodb&MS/SQL (15)
- Oracle数据库实验室 (55)
- 搜索引擎的开发应用 (34)
- 软件工程师笔试经典 (14)
- 其他杂项 (10)
- AndroidPn& MQTT&C2DM&推技术 (29)
- ActiveMQ学习和研究 (38)
- Google技术应用开发和API分析 (11)
- flex的学习总结 (59)
- 项目中一点总结 (20)
- java疑惑 java面向对象编程 (28)
- Android 开发学习 (133)
- linux和UNIX的总结 (37)
- Titanium学习总结 (20)
- JQueryMobile学习总结 (34)
- Phonegap学习总结 (32)
- HTML5学习总结 (41)
- JeeCMS研究和理解分析 (9)
最新评论
-
lgh1992314:
[u][i][b][flash=200,200][url][i ...
看看mybatis 源代码 -
尼古拉斯.fwp:
图片根本就不出来好吧。。。。。。
Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传 -
ln94223:
第一个应该用排它网关吧 怎么是并行网关, 并行网关是所有exe ...
工作流Activiti的学习总结(八)Activiti自动执行的应用 -
ZY199266:
获取不到任何消息信息,请问这是什么原因呢?
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息 -
xiaoyao霄:
DestinationSourceMonitor 报错 应该导 ...
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息
在客户端和服务端的中定义相关的业务逻辑类的实现:通过实现IoHandler 中实现方法,操作各种业务。
一般的开发中采用IoHandlerAdapter,通过重写部分的方法实现的相关的业务逻辑;
IoHandler
IoHandler具有以下功能
- sessionCreated sessionCreated
- sessionOpened sessionOpened
- sessionClosed sessionClosed
- sessionIdle sessionIdle
- exceptionCaught exceptionCaught
- messageReceived messageReceived
- messageSent messageSent
sessionCreated事件
Session Created event is fired when a new connection is created.
会话创建事件被激发时,一个新的连接被创建。
For TCP its the result of connection accept, and for UDP this is generated when a UDP packet is received.
对于TCP的连接其结果接受,这是为UDP时生成一个UDP数据包接收。
This function can be used to initialize session attributes, and perform one time activities for a particular connection.
该功能可用于初始化会话属性,并执行一次活动的特定连接。
This function is invoked from the I/O processor thread context, hence should be implemented in a way that it consumes minimal amount of time, as the same thread handles multiple sessions.
调用此函数从I / O处理器线程上下文,因此应该采取一种方式,它消耗最少量的时间,相同的线程处理多个会话执行。
sessionOpened Event sessionOpened事件
Session opened event is invoked when a connection is opened.
调用的事件是打开一个连接。
Its is always called after sessionCreated event.它总是sessionCreated事件后调用。 If a thread model is configured, this function is called in a thread other than the I/O processor thread.
如果一个线程模型配置,该函数被调用的线程比I / O处理器线程等。
sessionClosed Event sessionClosed事件
Session Closed event is closed, when a session is closed.
会话关闭时,当一个会话结束。
Session cleaning activities like cash cleanup can be performed here.
sessionIdle Event sessionIdle事件
Session Idle event is fired when a session becomes idle.
会议空闲事件被激发时,会变为空闲。
This function is not invoked for UDP transport.
此功能没有援引UDP传输。
exceptionCaught Event exceptionCaught事件
This functions is called, when an Exception is thrown by user code or by MINA.
这个函数被调用,当异常是由用户代码或MINA的异常。
The connection is closed, if its an IOException.连接关闭,如果它的一个IOException。
messageReceived Event messageReceived事件
Message Received event is fired whenever a message is received.
消息收稿事件被激发时,收到一条消息。
This is where the most of the processing of an application happens.
这就是对一个应用程序处理大部分发生。
You need to take care of all the message type you expect here.
您需要考虑所有类型的信息在这里照顾你期望。
messageSent Event messageSent事件
Message Sent event is fired, whenever a message aka response has been sent(calling IoSession.write()).消息发送的事件被激发,每当有消息又名反应已发送(呼叫IoSession.write())。
客户端的业务逻辑处理器:
源代码如下:
package cn.com.unutrip.java.opensource.mina.sumup;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.com.unutrip.java.opensource.mina.sumup.message.AddMessage;
import cn.com.unutrip.java.opensource.mina.sumup.message.ResultMessage;
/**
*
* 处理所有I / O活动的MINA的激發。
* The interface is hub of all activities done at the end of the Filter Chain.
* 该接口是在过滤器链的末端进行的所有活动的中心。
* IoHandler has following functions IoHandler具有以下功能
* sessionCreated :
* 会话创建事件被激发时,一个新的连接被创建。 For TCP its the result of connection accept, and for UDP this is generated when a UDP packet is received.对于TCP的连接其结果接受,这是为UDP时生成一个UDP数据包接收。 This function can be used to initialize session attributes, and perform one time activities for a particular connection.该功能可用于初始化会话属性,并执行一次活动的特定连接。
*This function is invoked from the I/O processor thread context, hence should be implemented in a way that it consumes minimal amount of time, as the same thread handles multiple sessions.调用此函数从I / O处理器线程上下文,因此应该采取一种方式,它消耗最少量的时间,相同的线程处理多个会话执行。
*
* sessionOpened :会话开始一个连接时调用此事件,它总在sessionCreated事件后调用,如果一个线程模型配置
* 该函数被调用的的线程IoHandler。
*
* sessionClosed : 当一个会话结束,session清理活动的活动的连接。
*
* sessionIdle : 此方法被激发时,会变成空闲,此功能没有援引UDP传输。
*
* exceptionCaught : 当用户代码或者mina异常是,连接关闭,如果它的一个连接IOException
*
* messageReceived : 当接受到一个消息时被激发,对应一个应用程序的处理的大部分业务逻辑,
*
* messageSent : 消息发送的时间被激发,
*
* 调用的事件是打开一个连接。 Its is always called after sessionCreated event.它总是sessionCreated事件后调用。
* If a thread model is configured, this function is called in a thread other than the I/O processor thread.
* 如果一个线程模型配置,该函数被调用的线程比I / O处理器线程等。
*
* 客户端业务处理器的应用
*
* @author longgangbai
*
*/
public class ClientSessionHandler extends IoHandlerAdapter {
private final static Logger logger = LoggerFactory.getLogger(ClientSessionHandler.class);
private final int[] values;
private boolean finished;
public ClientSessionHandler(int[] values) {
this.values = values;
}
public boolean isFinished() {
return finished;
}
/**
*
* 调用的事件是打开一个连接。 Its is always called after sessionCreated event.
* 它总是sessionCreated事件后调用。
* If a thread model is configured, this function is called in a thread other than the I/O processor thread.
* 如果一个线程模型配置,该函数被调用的线程比I / O处理器线程等。
*/
@Override
public void sessionOpened(IoSession session) {
logger.error("ClientSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%sessionOpened ");
// send summation requests
for (int i = 0; i < values.length; i++) {
AddMessage m = new AddMessage();
m.setSequence(i);
m.setValue(values[i]);
session.write(m);
}
}
/**
* 接受来自服务端的数据信息
* 接受到消息事件被激发时,收到一条消息。
* This is where the most of the processing of an application happens.
* 这就是对一个应用程序处理大部分发生。
* You need to take care of all the message type you expect here.
* 您需要考虑所有类型的信息在这里照顾你期望。(重要的业务逻辑的写这里)
*/
@Override
public void messageReceived(IoSession session, Object message) {
logger.error("ClientSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%messageReceived ");
// server only sends ResultMessage. otherwise, we will have to identify
// its type using instanceof operator.
ResultMessage rm = (ResultMessage) message;
if (rm.isOk()) {
// server returned OK code.
// if received the result message which has the last sequence
// number,
// it is time to disconnect.
if (rm.getSequence() == values.length - 1) {
// print the sum and disconnect.
logger.error("The sum: " + rm.getValue());
session.close(true);
finished = true;
}
} else {
// seever returned error code because of overflow, etc.
logger.error("Server error, disconnecting...");
session.close(true);
finished = true;
}
}
/**
* 发生异常时的操作
* 当异常是由用户代码或MINA的异常。 The connection is closed, if its an IOException.连接关闭,如果它的一个IOException
*/
@Override
public void exceptionCaught(IoSession session, Throwable cause) {
logger.error("ClientSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%exceptionCaught ");
session.close(true);
}
}
服务端的业务逻辑处理器:
package cn.com.unutrip.java.opensource.mina.sumup;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.com.unutrip.java.opensource.mina.sumup.message.AddMessage;
import cn.com.unutrip.java.opensource.mina.sumup.message.ResultMessage;
/**
* 创建服务端的业务处理器
* @author longgangbai
*
*/
public class ServerSessionHandler extends IoHandlerAdapter{
private static final String SUM_KEY="sum";
private final static Logger logger=LoggerFactory.getLogger(ServerSessionHandler.class);
@Override
public void sessionOpened(IoSession session){
logger.error("ServerSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%sessionOpened ");
//设置Idle的时间为60秒
session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60);
//初始化总的和为0
session.setAttribute(SUM_KEY,new Integer(0));
}
@Override
public void sessionIdle(IoSession session,IdleStatus statis)
{
logger.error("ServerSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%sessionIdle ");
logger.info("Disconnecting the idle.");
// disconnect an idle client
session.close(true);
}
@Override
public void exceptionCaught(IoSession session, Throwable cause) {
logger.error("ServerSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%exceptionCaught ");
// close the connection on exceptional situation
session.close(true);
}
@Override
public void messageReceived(IoSession session ,Object message){
logger.error("ServerSessionHandler =%%%%%%%%%%%%%%%%%%%%%%%%%%%messageReceived ");
//客户端发送的对象必须住注册的对象一致
AddMessage am=(AddMessage)message;
//将客户端的发送的数据添加到当前总的金额中
int sum=(Integer)session.getAttribute(SUM_KEY);
int value=am.getValue();
long expectedsum=(long)sum+value;
if(expectedsum>Integer.MAX_VALUE||expectedsum<Integer.MIN_VALUE)
{
//
ResultMessage rm=new ResultMessage();
rm.setSequence(am.getSequence());
rm.setOk(false);
session.write(rm);
}else{
sum=(int)expectedsum;
session.setAttribute(SUM_KEY,new Integer(sum));
ResultMessage rm=new ResultMessage();
rm.setSequence(am.getSequence());
rm.setOk(true);
rm.setValue(sum);
session.write(rm);
}
}
}
文章尚未写完,请关注下文。
发表评论
-
[转]Jython初探
2014-01-07 11:19 2432转载自: ... -
ireport导出各种格式(pdf,excel,word,html,print)
2013-05-02 16:59 10093import java.io.IOException; ... -
【转】使用Atomikos Transactions Essentials实现多数据源JTA分布式事务
2013-04-03 12:11 6828Atomikos数据源配置方法有三种 Atomikos数 ... -
【转】Apache Thrift入门1-架构&介绍
2013-04-02 13:26 2072Thrift 是什么? Thrift ... -
【转】Thrift入门及Java实例演示
2013-04-02 12:47 2626目录: 概述 下载配置 基本概念 数据类型 ... -
【转】Thrift入门试用
2013-04-02 12:39 2211在新的项目中公司在平台内部系统间使用Thrift通讯,都没 ... -
【转】thrift的安装
2013-04-02 12:38 2119一、ubuntu下thrift的安装 1.下载源代码 ... -
GIS的学习(二十五)geoserver wms中的各种操作API详细讲解和使用
2012-09-10 17:42 9747官方geoserver中WMS服务中几种操作的API的详细说明 ... -
POI3.8组件研究(九)----让POI架起Java与Office之间的桥梁
2012-06-17 14:37 4370本文将阐述如何用POI来读取/写入完整的Excel文 ... -
POI3.8组件研究(八)--基于SXSSF (Streaming Usermodel API)的写文件
2012-06-17 14:17 14466在POI3.8中SXSSF仅仅支持excel2 ... -
POI3.8组件研究(七)--基于XSSF and SAX (Event API)事件的解析
2012-06-17 14:00 5397针对Event API事件解析仅仅支持excel97~ ... -
POI3.8组件研究(六)---struts2.0 视图层文件页面点击导出
2012-06-17 13:23 2440在struts2.0中点击导出按钮将信息导出为exce ... -
POI3.8组件研究(五)---excel文件内容抽取为文本
2012-06-15 09:15 4389在一个搜索引擎的使用中需要将各种文件转化为文本 ... -
POI3.8组件研究(四)--Event API (HSSF Only)事件的解析
2012-06-14 17:37 9115通过eventusermodel读取文件 ... -
POI3.8组件研究(二)---基于User API (HSSF and XSSF)解析Excel2003和2007文件
2012-06-14 09:46 3243在解析生成excel2003和 ... -
POI3.8组件研究(一)---基于User API (HSSF and XSSF)解析Excel2003和2007文件
2012-06-14 09:29 5388在以前的Excel解析时候,我们通常需要编写Ex ... -
EasyPOI的使用
2012-02-12 17:06 5372EasyPOI 的目的是封装了poi的写excel的API。 ... -
Commons-net FTPClient上传下载的封装
2011-08-25 08:30 11542在项目中使用到FTP功能,于是采用类似Spri ... -
Java将第三方jar文件打包到一个jar中的插件(fatjar)
2011-08-19 22:17 4376<!-- google_ad_section_star ... -
Apache的Commons-configuration自动加载特性
2011-07-24 19:04 4117在一些项目可能配置文件经常变化,配置文件的类型可能 ...
相关推荐
Apache Mina是一个开源框架,主要用于构建高性能、高可用性的网络应用程序。这个压缩包包含了全面的学习资料,适合初学者深入理解Mina的原理和使用方法。其中,中文参考手册的加入使得学习过程更加友好,便于非英语...
Apache MINA(Multipurpose Infrastructure for Network Applications)是一个Java框架,用于构建高性能、高可用性的网络应用程序。MINA 提供了一种简单而强大的API,开发者可以使用它来处理TCP/IP和UDP/IP协议,如...
对于希望提高网络应用性能或熟悉Java NIO编程的开发者来说,Apache Mina 2.0.4的源码分析是一次宝贵的学习机会。你可以通过阅读源码学习到如何有效地处理高并发场景,如何设计和实现高效的网络协议,以及如何利用...
Apache MINA(Multipurpose Infrastructure for Network Applications)是一个Java框架,专为高性能、异步网络应用程序设计。MINA 提供了一套高级的网络通信抽象层,简化了开发过程,特别是对于处理TCP/IP和UDP/IP...
Apache Mina是一个高度可扩展的网络通信框架,主要用于构建高性能、高效率的服务端应用程序。...通过学习和使用"Mina帮助文档",开发者可以更好地理解和利用这个框架,构建出高效、稳定的网络应用。
Apache MINA是一个高性能、异步事件驱动的网络应用程序框架,主要设计用于简化开发服务器端的高性能网络应用。这个框架提供了一种抽象层,允许开发者使用相同的API处理多种不同的传输协议,如TCP/IP、UDP/IP以及SSL/...
通过深入学习和实践这个Apache Mina入门Demo,你将掌握如何利用Mina构建网络应用,并了解其核心特性和工作原理,这对于从事Java网络编程或者需要处理大规模并发连接的开发者来说是非常有价值的。
Apache MINA(Multipurpose Infrastructure for Network Applications)是一个开源框架,主要设计用于简化网络应用程序的开发,尤其是基于TCP和UDP协议的应用。它提供了高度可扩展和高性能的非阻塞I/O模型,使得...
apache-mina-2.0.4 架包 源码 学习教程.apache mina是Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应用程序提供了非常便利的框架。当前发行的 MINA 版本支持基于 Java NIO 技术的 TCP/UDP 应用程序...
Apache MINA(Multipurpose Infrastructure for Network Applications)是一个高性能、异步事件驱动的网络应用程序框架,主要用于简化开发高质量的网络服务。这个框架适用于多种协议,如TCP/IP和UDP/IP,以及NIO(非...
Apache Mina Server 2.0 是一款高性能、...此外,下载的压缩包文件“minaTest”可能包含示例代码或测试项目,可以作为学习和实践Apache Mina的好材料。通过这些实践,你可以加深对Mina的理解,并将其应用到实际项目中。
Apache Mina是一个高度可扩展的网络通信框架,主要用于构建高性能、高并发的网络应用程序,如服务器端应用。它提供了一种简单而强大的编程模型,使得开发者可以专注于业务逻辑,而不是底层的网络通信细节。Spring...
Apache Mina是一个强大的开源项目,专门设计用于构建网络应用程序。这个框架的核心目标是...通过深入学习和实践,开发者可以创建出满足各种需求的网络服务,无论是在企业级应用还是小型项目中,Mina都能发挥重要作用。
Apache Mina是一个开源项目,主要用于构建高性能、高可用性的网络应用程序。这个例子是关于如何使用Apache Mina实现一个简单的服务端和客户端通信的示例。在这个案例中,`MinaTimeServer`是服务端程序,它负责监听...
Apache Mina 2是Apache项目下的一款高性能、高可扩展性的网络应用框架,它简化了Java NIO编程,使得开发者能够更加轻松地构建复杂的网络应用。Mina通过提供一个抽象的、事件驱动的异步API,支持多种传输协议,包括但...
通过深入学习“Apache Mina Server 2.0中文参考手册V1.0”,开发者可以全面了解 Mina 的核心功能,并掌握利用 Mina 构建高效、稳定网络服务的技术。手册还可能提供了丰富的示例代码和实战指导,帮助开发者快速上手。...
Apache MINA(Multipurpose Infrastructure for Network Applications)是一个Java框架,专为开发高性能和高可用性的网络应用程序而设计。MINA提供了异步的、事件驱动的网络应用编程接口(API),适用于TCP和UDP协议...
总的来说,Apache Mina通过其过滤器架构和处理器模型,为开发网络应用程序提供了强大的抽象。通过对`MinaTimeServer`的源代码分析,我们可以看到如何配置服务器,设置过滤器,处理网络事件,并与客户端进行有效通信...
Apache Mina是一个开源的Java框架,它简化了网络应用程序的开发,特别是对于TCP/IP和UDP协议的应用。在本文中,我们将通过一个简单的实例来探讨如何使用Apache Mina进行网络通信。 首先,Apache Mina的核心是它的...