网站端clientWZ 只是一个测试类,实际用的时候可以进行封装,将message通过FutureTask<String> futureTask = new FutureTask<String>(new WZcreateClientToTra(message));发送并接收结果。这里是测试,跟前面的代码有些出入,这里先说明一下,这个测试类发送的是String类型的数据,前面两篇都是Message类型数据,大家测试的时候换成message或者String都行,但要注意commonTool里面的接收、发送代码。
public class clientWZ {
final String IP="127.0.0.1";
final int PORT=8888;
private Integer sotimeout=1*1*3000;//超时时间,以毫秒为单位
/**
* @param args
*/
@SuppressWarnings("static-access")
public static void main(String[] args) {
XmlEntity xml= new XmlEntity().createDefaultRequest(1101);
Message message=new Message().createDefaultMessage(xml.buildAsBytes());
System.out.println("WZ端原始数据:"+message.toString());
long stat=System.currentTimeMillis();
int erro=0;
for(int i=100;i>0;i--){
try {
Thread.sleep(1000*(100-i));
} catch (InterruptedException e1) {
e1.printStackTrace();
}
FutureTask<String> futureTask = new FutureTask<String>(new WZcreateClientToTra(message));
Thread thread=new Thread(futureTask);
thread.setName("thread"+i);
thread.start();
while(!futureTask.isDone()){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
if(futureTask.get()!=null){
//xml=(XmlEntity)futureTask.get();
String xmlStr=futureTask.get();
System.out.println(Thread.currentThread().getName()+":网站端收到回复xmlStr:"+xmlStr);
}
}catch (Exception e) {
XmlEntity responseXml = null;
System.out.println("❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉出现错误:"+e.getMessage()+"错误❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉❉");
XmlEntity requestXml = XmlEntity.parse(message.getContents());
responseXml = XmlEntity.create().createDefaultResponse(requestXml);
responseXml.setResponseCodeAndMsg("000001", "系统异常!请重新查询!");
Message message2=Message.createDefaultMessage(responseXml.buildAsBytes());
erro++;
}
}
long end=System.currentTimeMillis();
System.out.println("花费时间:"+(end-stat)/1000);
System.out.println("错误个数:"+erro);
}
}
网站端查询方法WZcreateClientToTra,是一个实现Callable接口方法。
class WZcreateClientToTra implements Callable<String>{//XmlEntity
final String IP="127.0.0.1";
final int PORT=8888;
Message message=new Message();
ObjectInputStream dis = null;
ObjectOutputStream os = null;
Socket socket = null;
boolean flag=true;
boolean close;
public WZcreateClientToTra(){}
public WZcreateClientToTra(Message message){
this.message=message;
}
public String call() throws Exception {//XmlEntity
socket = new Socket(IP,PORT);
socket.setSoTimeout(3000);
try {
System.out.println(Thread.currentThread().getName()+"开始执行。。。。");
while(flag){
//---------读数据---------------------------
close = isServerClose(socket);//判断是否断开
if(!close){//没有断开,开始读数据
boolean b=Send(socket,message);
//boolean b=Send(socket,"我是客户端请求线程:"+Thread.currentThread().getName());
if(b){
System.out.println("网站发送请求成功后,开始读取业务端回复...:"+Thread.currentThread().getName());
message = ReadText(socket);
Thread.sleep(50);
if(message!=null){
//System.out.println("读取到业务端数据:"+message);
flag=false;
}
}
}else{
System.out.println("socket 断开连接,无法读取回复数据。。。。。");
flag=false;
}
}
} catch (Exception e) {
System.out.println("网站端出现异常:"+e);
}finally{
if(null!=socket){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return message.toString();
}