快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门新葡新京:MINA学习(二)mina2.0.0M3集成spring的示例



1.首先导入jar包咯

po澳门新葡新京m.xml如下:

org.slf4j

slf4j-api

1.5.2

log4j

log4j

1.2.14

org.apache.mina

mina-core

2.0.0-M3

org.apache.mina

mina-integration-spring

1.1.7

org.apache.mina

mina-integration-beans

2.0.0-M3

org.slf4j

slf4j-simple

1.5.2

此中对照麻烦的便是这个slf4j了。版本问题还要留意下,而且版本要和log4j共同,不然无意偶尔会报log4j里面的Method not found

2.SERVER真个实现类

只简单的实现了收到client的信息,然后打印出来,然后再回覆client一个信息

从MINA下面的例子chat里面收拾出来的代码

MinaProtocolHandler.java:

package com.sillycat.api.mina;

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;

public class MinaProtocolHandler extends IoHandlerAdapter {

private final Logger logger = LoggerFactory.getLogger(getClass());

// private final Set sessions = Collections

// .synchronizedSet(new HashSet());

public void messageReceived(IoSession session, Object message) {

String msg = (String) message;

System.out.println("Server Received: " + msg);

session.write("Server Send: " + msg);

// sessions.add(session);

}

public void sessionIdle(IoSession session, IdleStatus status) {

// disconnect an idle client

session.close();

}

// public void sessionClosed(IoSession session) throws Exception {

// sessions.remove(session);

// }

public void exceptionCaught(IoSession session, Throwable cause) {

logger.warn("Unexpected exception澳门新葡新京.", cause);

session.close();

}

}

3.spring的设置设置设备摆设摆设文件applicationContext-mina.xml:

启动T澳门新葡新京OMCAT,里面spring的Contentloader装载applicationContext-mina.xml,那么就启动了1235这个端口了

就可以telnet localhost 1235来测试了。嘿嘿。

4.Client的测试示例

MinaClientSupport.java:

package com.sillycat.api.mina.client;

import java.net.InetSocketAddress;

import java.net.SocketAddress;

import org.apache.mina.core.future.ConnectFuture;

import org.apache.mina.core.service.IoHandler;

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

import org.apache.mina.filter.澳门新葡新京logging.MdcInjectionFilter;

import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class MinaClientSupport {

private IoHandler handler = null;

private IoSession session;

private String host;

private int port;

public boolean send(Object message) {

if (session != null && session.isConnected()) {

throw new IllegalStateException(

"Already connected. Disconnect first.");

}

SocketAddress address = new InetSocketAddress(host, port);

NioSocketConnector connector = new NioSocketConnector();

try {

connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());

connector.setHandler(handler);

ConnectFuture future1 = connector.connect(address);

future1.awaitUninterruptibly();

if (!future1.isConnected()) {

return false;

}

session = future1.getSession();

session.write(message);

return true;

} catch (Exception e) {

return false;

}

}

public void close(){

if (session != null) {

if (session.isConnected()) {

// Wait until the chat ends.

session.getCloseFuture().awaitUninterruptibly();

}

session.close();

}

}

public String getHost() {

return host;

}

public void setHost(String host) {

this.host = host;

}

public int getPort() {

return port;

}

public void setPort(int port) {

this.port = port;

}

public void setHandler(IoHandler handler) {

this.handler = handler;

}

public static void main(String[] args) {

System.out.println("---------------------");

MinaClientSupport client = new MinaClientSupport();

MinaClientHandler handler = new MinaClientHandler();

client.setHandler(handler);

client.setHost("localhost");

client.setPort(1235);

String msg = "hello world!";

cli澳门新葡新京ent.send(msg);

System.out.println("Client Send: " + msg);

client.close();

}

}

别的Client端设置一个handler,接管办事端回覆过来的信息

MinaClientHandler.java:

package com.sillycat.api.mina.client;

import org.apache.mina.core.filterchain.IoFilter;

import org.apache.mina.core.service.IoHandlerAdapter;

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

import org.apache.mina.filter.codec.ProtocolCodecFilter;

import org.apache.mina.filter.codec.textline.TextLineCodecFactory;

import org.apache.mina.filter.logging.LoggingFilter;

public class MinaClientHandler extends IoHandlerAdapter {

private static final IoFilter LOGGING_FILTER = new LoggingFilter();

private static final IoFilter CODEC_FILTER = new ProtocolCodecFilter(

new TextLineCodecFactory());

@Override

public void sessionCreated(IoSession session) throws Exception {

session.getFilterChain().addLast("codec", CODEC_FILTER);

session.getFilterChain().addLast("logger", LOGGING_FILTER);

}

@Override

public void messageReceived(IoSession session, Object message)

throws Exception {

String msg = (String) message;

System.out.println("Client Received: " + msg);

}

}

您可能还会对下面的文章感兴趣: