概述
本次练习中是监听2个端口
applicationContext-mina.xml:
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
class="org.apache.mina.integration.spring.InetSocketAddressEditor" />
class="org.apache.mina.integration.spring.IoAcceptorFactoryBean">
class="org.apache.mina.transport.socket.nio.SocketAcceptor" />
//这里是个list 里面可以创建多个监听
class="org.apache.mina.integration.spring.Binding">
//监听端口:8888
// SampleHandler:定义服务器的handler
class="org.apache.mina.transport.socket.nio.SocketAcceptorConfig">
ref="filterChainBuilder" /> //指向: filterChainBuilder
class="org.apache.mina.integration.spring.Binding">
//第二个监听端口:9999
//监听该端口的 handler
class="org.apache.mina.transport.socket.nio.SocketAcceptorConfig">
ref="filterChainBuilder" />
//定义filter
class="org.apache.mina.integration.spring.DefaultIoFilterChainBuilderFactoryBean">
class="org.apache.mina.filter.codec.ProtocolCodecFilter">
class="org.apache.mina.filter.codec.textline.TextLineCodecFactory" /> //这个是编码格式filter
//日志的filter
sprng的简单配置文件就是这样:
然后看 handler:
SampleHandler 和 bossSampleHandler 。我写的都是一样的:
package cn.org.handler;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
public class HandlerOne extends IoHandlerAdapter {
public HandlerOne() {
// TODO Auto-generated constructor stub
}
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
// TODO Auto-generated method stub
System.out.println("收到信息");
System.out.println("message :"+message.toString());
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
System.out.println("小心发送");
}
@Override
public void sessionClosed(IoSession session) throws Exception {
// TODO Auto-generated method stub
super.sessionClosed(session);
}
@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println(session.getRemoteAddress().toString() +"---create");
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
System.out.println(session.getServiceAddress() +"IDS");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("连接打开:"+session.getLocalAddress());
}
}
现在测试:
Test.java
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext-mina.xml");
}
}
控制台打印出:
2010-1-16 19:00:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@4b4333: display name [org.springframework.context.support.ClassPathXmlApplicationContext@4b4333]; startup date [Sat Jan 16 19:00:51 CST 2010]; root of context hierarchy
2010-1-16 19:00:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext-mina.xml]
2010-1-16 19:00:51 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@4b4333]:org.springframework.beans.factory.support.DefaultListableBeanFactory@1975b59
2010-1-16 19:00:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons inorg.springframework.beans.factory.support.DefaultListableBeanFactory@1975b59: defining beans [org.springframework.beans.factory.config.CustomEditorConfigurer#0,ioAcceptor,SampleHandler,bossSampleHandler,filterChainBuilder]; root of factory hierarchy
表明服务器就已经启动了:
现在你可以用telent的方式测试。也可以写个客户端的测试类:
写了个客户端的测试类:Clint.java
public class Clint {
/**
* @param args
*/
final static char end = 0x1a;
public static void main(String[] args) {
SocketConnector connector = new SocketConnector();
SocketAddress address = new InetSocketAddress("127.0.0.1", 8888);
SocketConnectorConfig config = new SocketConnectorConfig();
config.setConnectTimeout(10);// 秒
connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "GB2312" )))); //设置编码过滤器
connector.getFilterChain().addLast( "logger", new org.apache.mina.filter.LoggingFilter() );
IoSession session =null;
ConnectFuture cf = connector.connect(address, new ClintHandler(),
config);
cf.join();
if (cf.isConnected()) {
session = cf.getSession();
System.out.println("连接成功");
session.write("abc");
}else{
System.out.println("连接失败!!!");
}
}
}
最后
以上就是坚定鸡翅为你收集整理的java nio 多个端口_mina在spring中的配置多个端口的全部内容,希望文章能够帮你解决java nio 多个端口_mina在spring中的配置多个端口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复