我用mina写了一个短信处理器,在本机上测试很快。但是把server和client放在局域网类的两台电脑上。处理速度很慢。不知道这是什么原因。是不是那里没有设置对,请各位支点招吧
下面是一部分代码:
server:
package com.appgateway.networkcore.messageprocess;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
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.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import com.appgateway.appinterface.GeneralMethod_HashMap;
import com.appgateway.clientprocess.ExecuteConnectSmapp;
import com.appgateway.coreprocess.AreaInfoLoad;
import com.appgateway.coreprocess.SchoolInfoLoad;
import com.appgateway.util.Constants;
import com.cdqidi.appgateway.infoload.base.AreaInfoBean;
import com.cdqidi.appgateway.infoload.base.DataCore;
import com.cdqidi.appgateway.infoload.base.Message;
import com.cdqidi.appgateway.infoload.base.SchoolInfoBean;
import SoftFan.Log.wf_Log;
public class MessageProcessServer extends IoHandlerAdapter {
private NioSocketAcceptor nsa;
/**
* 启动数据监听服务
*/
public void startup() {
try {
nsa = new NioSocketAcceptor();
DefaultIoFilterChainBuilder dfc = (DefaultIoFilterChainBuilder) nsa.getFilterChainBuilder();
dfc.addLast("ObjectData", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
dfc.addLast("ThreadPool", new ExecutorFilter(Executors.newCachedThreadPool()));
nsa.setHandler(this);
nsa.bind(new InetSocketAddress(8899));
wf_Log.sys_log("短信通道信息处理端服务监听器已开启......");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭数据监听服务
*/
public void shutdown() {
nsa.dispose();
wf_Log.sys_log("短信通道信息处理端服务监听器已关闭......");
}
public void messageReceived(IoSession session, Object message)
throws Exception {
DataCore bean = (DataCore)message;
wf_Log.sys_log("received current operation code【"+bean.getOpcode()+"】......");
this.subFunction(bean.getOpcode(),bean,session);
//返回一个状态给客户端,告诉客户端,数据已接收。
session.write(1);
wf_Log.sys_log("return the state to the client successfull......");
}
private void subFunction(int operationCode, DataCore datacore,IoSession session) {
switch (operationCode) {
/**
* edit the info of area.
*/
case Constants.area_edit: {
AreaInfoBean areabean = (AreaInfoBean) datacore.getData();
AreaInfoLoad.getInstance().editParameter(((GeneralMethod_HashMap) areabean).getMap(),areabean.getAreaCode(), areabean.getAreaName());
break;
}
/**
* add new value into the memory
*/
case Constants.area_insert: {
AreaInfoBean areabean = (AreaInfoBean) datacore.getData();
AreaInfoLoad.getInstance().addParameter(((GeneralMethod_HashMap) areabean).getMap(),areabean.getAreaCode(), areabean.getAreaName());
break;
}
case Constants.area_singledelete: {
AreaInfoBean areabean = (AreaInfoBean) datacore.getData();
AreaInfoLoad.getInstance().removeSingle(((GeneralMethod_HashMap) areabean).getMap(),areabean.getAreaCode());
break;
}
case Constants.area_removeall: {
AreaInfoBean areabean = (AreaInfoBean) datacore.getData();
AreaInfoLoad.getInstance().removeAll(((GeneralMethod_HashMap) areabean).getMap());
break;
}
/**
* 学校信息加载处理
*/
case Constants.school_edit: {
//SchoolInfoBean schoolinfobean = (SchoolInfoBean) datacore.getData();
//String value = schoolinfobean.getSchoolCode();
//SchoolInfoLoad.editParameter(SchoolInfoLoad.getMap(), value, value, value);
break;
}
case Constants.school_singledelete:{
SchoolInfoBean sclbean = (SchoolInfoBean)datacore.getData();
System.out.println(sclbean.getSchoolCode());
SchoolInfoLoad.removeSingle(SchoolInfoLoad.getMap(), sclbean.getSysSchoolId());
break;
}
case Constants.school_insert:{
SchoolInfoBean sclbean = (SchoolInfoBean)datacore.getData();
String value = sclbean.getAreaID()+","+sclbean.getSchoolName()+","+sclbean.getSchoolSign();
SchoolInfoLoad.addParameter(SchoolInfoLoad.getMap(), sclbean.getSchoolCode(), value);
break;
}
/**
* update school information by xxt and adc
*/
case Constants.school_xxt_synchronous:{
break;
}
//短信发送
case Constants.MESSSAGE_SEND:{
ExecuteConnectSmapp.Do((Message) datacore.getData());
break;
}
}
}
}
client:
package org.cdqidi.sminterface.socket;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Properties;
import java.util.concurrent.Executors;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import SoftFan.Log.wf_Log;
/**
* 此类完成客户端与服务端的连接
* 需完成网络中断后自动重连
* @author jacklei
*
*/
public class DataTansferService extends Thread{
/**
*
*/
private static final long serialVersionUID = 1L;
private static SocketConnector socket;
private final static Properties properties = new Properties();
public static HashMap<Integer,ConnectFuture> _connect = new HashMap<Integer,ConnectFuture>();
private static ConnectFuture socketConnect(){
socket = new NioSocketConnector();
/**
* 加载配置文件
*/
load();
socket.getFilterChain().addLast("ObjectData", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
socket.getFilterChain().addLast("ThreadPool", new ExecutorFilter(Executors.newCachedThreadPool()));
socket.setHandler(new ClientHandler());
ConnectFuture future =socket.connect(new InetSocketAddress(properties.getProperty("server_ip"), Integer.parseInt(properties.getProperty("server_port"))));
future.awaitUninterruptibly();
return future;
}
public void run() {
int key = 0;
ConnectFuture _socketConnect = socketConnect();
while(true){
boolean socketConnect = _socketConnect.isConnected();
if(socketConnect){
try {
_connect.put(key, _socketConnect);
Thread.sleep(10000);
wf_Log.sys_log("网络成功连接,10秒后,重新检测");
wf_Log.sys_log("CurrentSize:"+_connect.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
wf_Log.sys_log("客户端未成功连接到服务器上,每隔3秒重新与服务器连接!");
try {
Thread.sleep(3000);
_socketConnect=socketConnect();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 加载网络配置文件
*/
private static void load(){
try {
properties.load(new FileInputStream(new File("").getAbsolutePath().concat("\\config\\config.properties")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void startup(){
this.start();
}
public void shutdown(){
this.interrupt();
this.close();
}
/**
* 关闭socket通道
*/
private void close(){
if(socket.isActive()){
socket.dispose();
}
}
}
程序入口方法
package org.cdqidi.sminterface.socket;
import java.io.IOException;
import org.apache.mina.core.session.IoSession;
import com.cdqidi.appgateway.infoload.base.DataCore;
import com.cdqidi.appgateway.infoload.base.Message;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
DataTansferService s = new DataTansferService();
s.start();
DataCore data = new DataCore();
Message msg = new Message();
msg.setMOBILES("13981739425");
msg.setCONTENT("我爱你MINA");
msg.setSCHOOL_ID("5001");
msg.setExescheck_code("01");
msg.setFunction_id("7");
data.setOpcode(99);
data.setData(msg);
try {
Thread.sleep(5000);
IoSession session = MessageSender.getSession();
System.out.println(session.isConnected());
if(session.isConnected()){
try {
long start = System.currentTimeMillis();
for(int i =0;i<1000;i++)
MessageSender.send(data,session);
System.out.println(System.currentTimeMillis()-start);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}