本文介绍了Apache的MINA从IoSession阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的Apache MINA亲切指导我如何从IoSession读取。我有一个存储在POJO它。
公共静态EchoUDPServerDiscoveryObjectResponseProperties echoPropertiessession.write(echoProperties);
解决方案
自定义客户端包的客户端;进口java.net.InetSocketAddress;
进口java.nio.charset.CharacterCodingException;
进口java.nio.charset.Charset中;
进口java.util.logging.Level中;
进口org.apache.mina.core.buffer.IoBuffer;
进口org.apache.mina.core.future.ConnectFuture;
进口org.apache.mina.core.future.IoFutureListener;
进口org.apache.mina.core.future.ReadFuture;
进口org.apache.mina.core.service.IoConnector;
进口org.apache.mina.core.service.IoHandlerAdapter;
进口org.apache.mina.core.session.IdleStatus;
进口org.apache.mina.core.session.IoSession;
进口org.apache.mina.example.udp.client.MemMonClient;
进口org.apache.mina.transport.socket.nio.NioDatagramConnector;/ **
*
* @author AZ
* /
公共类CustomClient类扩展IoHandlerAdapter { 私人IoSession会议;
私人IoConnector连接器;
私人ConnectFuture connFuture; 公共CustomClient类()抛出InterruptedException的{
连接器=新NioDatagramConnector();
connector.setHandler(本);
connFuture = connector.connect(新的InetSocketAddress(192.168.3.22,6502));
connFuture.addListener(新IoFutureListener< ConnectFuture>(){
公共无效operationComplete(ConnectFuture未来){
如果(future.isConnected()){
会话= future.getSession();
尝试{
尝试{
送出数据();
// connFuture.await(); }赶上(CharacterCodingException前){
。java.util.logging.Logger.getLogger(MemMonClient.class.getName())日志(Level.SEVERE,空,前);
}
}赶上(InterruptedException的E){
e.printStackTrace();
}
}
}
}); }
私人无效的SendData()抛出InterruptedException的,CharacterCodingException {
IoBuffer缓冲液= IoBuffer.allocate(8);
buffer.setAutoExpand(真);
buffer.putString(任何,Charset.forName(UTF-8)尼文codeR());
buffer.flip();
session.write(缓冲液);
} @覆盖
公共无效exceptionCaught(IoSession会议,Throwable的原因)
抛出异常{
cause.printStackTrace();
}
@覆盖
公共无效的messageReceived(IoSession会话,对象消息)
抛出异常{
。connFuture.getSession()getConfig()setUseReadOperation(真)。 ReadFuture R = connFuture.getSession()阅读()。
connFuture.await();
。connFuture.getSession()getConfig()setUseReadOperation(真)。
obj对象= r.getMessage();
的System.out.println(r.getMessage();+物镜);
IoBuffer缓冲= IoBuffer.allocate(2048);
buffer.setAutoExpand(真);
对象objReceived = buffer.getObject();
的System.out.println(objReceived.toString()); 的System.out.println(收讫会议的recv ......);
} @覆盖
公共无效messageSent(IoSession会话,对象消息)抛出异常{
的System.out.println(发送信息......);
} @覆盖
公共无效sessionClosed(IoSession会话)抛出异常{
的System.out.println(会议闭幕......);
} @覆盖
公共无效sessionCreated(IoSession会话)抛出异常{
的System.out.println(会话创建......);
} @覆盖
公共无效sessionIdle(IoSession会议,IdleStatus状态)
抛出异常{
的System.out.println(会话空闲...);
} @覆盖
公共无效sessionOpened(IoSession会话)抛出异常{
的System.out.println(会议开了......);
}
公共静态无效的主要(字串是[]){
尝试{
新CustomClient类();
}赶上(例外前){ex.printStackTrace();}
}
}Java的POJO
包POJO;的Bean;/ **
*
* @author AZ
* /
公共类科乔实现Serializable {
私人字符串名称=空;
私人字符串的地址=空; / **
返回:名称
* /
公共字符串的getName(){
返回名称;
} / **
* @参数命名的名称设置
* /
公共无效setname可以(字符串名称){
this.name =名称;
} / **
返回:地址
* /
公共字符串的getAddress(){
退货地址;
} / **
* @参数的地址,其设置
* /
公共无效setAddress(字符串地址){
this.address =地址;
}}自定义服务器Java
包服务器;进口java.io.IOException异常;
进口java.net.InetSocketAddress;
进口org.apache.mina.transport.socket.DatagramSessionConfig;
进口org.apache.mina.transport.socket.nio.NioDatagramAcceptor;/ **
*
* @author AZ
* /
公共类CustomServer { 公共CustomServer(){
尝试{
NioDatagramAcceptor受体=新NioDatagramAcceptor();
acceptor.setHandler(新ServerHandler(本));
// DefaultIoFilterChainBuilder过滤= acceptor.getFilterChain();
DatagramSessionConfig DCFG = acceptor.getSessionConfig();
dcfg.setReuseAddress(真);
acceptor.bind(新的InetSocketAddress(6501));
}赶上(IOException异常前){
ex.printStackTrace();
}
} 公共无效receiveUpdate(){ }
公共静态无效的主要(字串是[]){
新CustomServer();
}
}服务器处理器
包服务器;进口java.nio.charset.Charset中;
进口org.apache.mina.core.buffer.IoBuffer;
进口org.apache.mina.core.future.WriteFuture;
进口org.apache.mina.core.service.IoHandlerAdapter;
进口org.apache.mina.core.session.IdleStatus;
进口org.apache.mina.core.session.IoSession;/ **
*
* @author AZ
* /
公共类ServerHandler扩展IoHandlerAdapter { 私人CustomServer服务器; 公共ServerHandler(CustomServer服务器){
this.server =服务器;
} @覆盖
公共无效的messageReceived(IoSession会话,对象消息)
抛出异常{ 如果(消息的instanceof IoBuffer){
//德code POJO并发送至客户端
IoBuffer缓冲=(IoBuffer)消息;
的System.out.println(buffer.getString(Charset.forName(UTF-8)newDe codeR()));
buffer.setAutoExpand(真);
buffer.putObject(新pojo.POJO());
buffer.flip();
session.write(缓冲液); System.out.print(对象连接和发送); }
} @覆盖
公共无效messageSent(IoSession会话,对象消息){
的System.out.println(消息派);
} @覆盖
公共无效sessionClosed(IoSession会话)抛出异常{
的System.out.println(会议闭幕......);
} @覆盖
公共无效sessionCreated(IoSession会话)抛出异常{
的System.out.println(会话创建......);
} @覆盖
公共无效sessionIdle(IoSession会议,IdleStatus状态)
抛出异常{
的System.out.println(会话空闲...);
} @覆盖
公共无效sessionOpened(IoSession会话)抛出异常{
的System.out.println(会话中打开...);
}
}
I am new to Apache MINA kindly guide me how to read from IoSession. I have stored a POJO in it.
public static EchoUDPServerDiscoveryObjectResponseProperties echoProperties
session.write(echoProperties);
解决方案
Custom Client
package client;
import java.net.InetSocketAddress;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.logging.Level;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.future.ReadFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.example.udp.client.MemMonClient;
import org.apache.mina.transport.socket.nio.NioDatagramConnector;
/**
*
* @author az
*/
public class CustomClient extends IoHandlerAdapter{
private IoSession session;
private IoConnector connector;
private ConnectFuture connFuture;
public CustomClient() throws InterruptedException{
connector = new NioDatagramConnector();
connector.setHandler(this);
connFuture = connector.connect(new InetSocketAddress("192.168.3.22",6502));
connFuture.addListener(new IoFutureListener<ConnectFuture>() {
public void operationComplete(ConnectFuture future) {
if (future.isConnected()) {
session = future.getSession();
try {
try {
sendData();
// connFuture.await();
} catch (CharacterCodingException ex) {
java.util.logging.Logger.getLogger(MemMonClient.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
private void sendData() throws InterruptedException, CharacterCodingException {
IoBuffer buffer = IoBuffer.allocate(8);
buffer.setAutoExpand(true);
buffer.putString("any", Charset.forName("UTF-8").newEncoder());
buffer.flip();
session.write(buffer);
}
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
cause.printStackTrace();
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
connFuture.getSession().getConfig().setUseReadOperation(true);
ReadFuture r = connFuture.getSession().read();
connFuture.await();
connFuture.getSession().getConfig().setUseReadOperation(true);
Object obj = r.getMessage();
System.out.println("r.getMessage(); "+obj);
IoBuffer buffer = IoBuffer.allocate(2048);
buffer.setAutoExpand(true);
Object objReceived = buffer.getObject();
System.out.println(objReceived.toString());
System.out.println("reveived Session recv...");
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
System.out.println("Message sent...");
}
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println("Session closed...");
}
@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println("Session created...");
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
System.out.println("Session idle...");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("Session opened...");
}
public static void main (String are[]){
try{
new CustomClient();
}catch(Exception ex){ex.printStackTrace();}
}
}
POJO Java
package pojo;
import java.io.Serializable;
/**
*
* @author az
*/
public class kojo implements Serializable{
private String name = "null";
private String address = "null";
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
}
Custom Server Java
package server;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.mina.transport.socket.DatagramSessionConfig;
import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
/**
*
* @author az
*/
public class CustomServer {
public CustomServer(){
try {
NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
acceptor.setHandler(new ServerHandler(this));
//DefaultIoFilterChainBuilder filter = acceptor.getFilterChain();
DatagramSessionConfig dcfg = acceptor.getSessionConfig();
dcfg.setReuseAddress(true);
acceptor.bind(new InetSocketAddress(6501));
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void receiveUpdate(){
}
public static void main(String are[]){
new CustomServer();
}
}
Server Handler
package server;
import java.nio.charset.Charset;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.future.WriteFuture;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
/**
*
* @author az
*/
public class ServerHandler extends IoHandlerAdapter {
private CustomServer server;
public ServerHandler(CustomServer server) {
this.server = server;
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
if (message instanceof IoBuffer) {
//decode POJO and send to client
IoBuffer buffer = (IoBuffer) message;
System.out.println(buffer.getString(Charset.forName("UTF-8").newDecoder()));
buffer.setAutoExpand(true);
buffer.putObject(new pojo.POJO());
buffer.flip();
session.write(buffer);
System.out.print("Object Attached and Sent");
}
}
@Override
public void messageSent(IoSession session, Object message) {
System.out.println("Message sent");
}
@Override
public void sessionClosed(IoSession session) throws Exception {
System.out.println("Session closed...");
}
@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println("Session created...");
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
System.out.println("Session idle...");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("Session Opened...");
}
}
这篇关于Apache的MINA从IoSession阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!