本篇介绍的是数据库端服务程序ClientForApp,它主要是主动连接转换端Server,并执行检查连接是否正常,以及接收到转换端发来的网站请求数据进行数据库数据查询并返回结果。
下面首先展示“数据库端服务程序”(socket)端代码:(服务端由1/2/3组成)
1、ClientForApp:
public class ClientForApp {
//记录日志
Log log = LogFactory.getLog(this .getClass());
PublicAppService publicAppService=new PublicAppService();
CommonTool commonTool=new CommonTool();
private static MyBlockingQueue myBlockingQueue=new MyBlockingQueue(10);
public static Socket socketWZ;
final static String IP = "127.0.0.1"; //本地测试IP
final static Integer PORT=9999;//连接服务器的端口
public static void main(String[] args) {
try{
checkSocket checkSocket=new checkSocket(IP,PORT,myBlockingQueue);
for(int i=4;i>0;i--){//每次建立4个长连接,由4个线程持有
clientYWSS nksocket1 = new clientYWSS(IP,PORT,myBlockingQueue);
new Thread(nksocket1).start();
}
Thread checkThread=new Thread(checkSocket);
checkThread.setName("checkThread");
new Thread(checkSocket).start();
}catch (Exception e) {
e.printStackTrace();
}
}
}
2、数据库端检查线程:checkSocket
public class checkSocket implements Runnable {
CommonTool commonTool=new CommonTool();
Socket socket;
MyBlockingQueue myBlockingQueue;
String IP;
int PORT;
/**
* @param socket
*/
public checkSocket(String IP,int PORT,MyBlockingQueue myBlockingQueue) {
super();
this.myBlockingQueue = myBlockingQueue;
this.IP=IP;
this.PORT=PORT;
}
@Override
public void run() {
while(true){
try {
if(myBlockingQueue.size()>0){//当线程安全队列中有socket才去检查,没有直接去创建
for(int i=0;i<=myBlockingQueue.size();i++){
socket=myBlockingQueue.get();
if(null!=socket && !commonTool.isServerClose(socket)){
myBlockingQueue.put(socket);
}else if(null!=socket){
socket.close();
}
Thread.sleep(50);
}
}
if(myBlockingQueue.size()<2){//如果myBlockingQueue中socket数少于2个就会创建新的连接
for(int i=0;i<3-myBlockingQueue.size();i++){
socket = new Socket(IP,PORT);
socket.setKeepAlive(true);
//System.out.println("创建连接成功");
if(!commonTool.isServerClose(socket)){
myBlockingQueue.put(socket);
System.out.println("创建连接成功,当前myBlockingQueue.size:"+myBlockingQueue.size()+";ListElement:"+myBlockingQueue.getListElement());
}else{
socket.close();
}
}
}
Thread.sleep(10000);
} catch (Exception e) {
//e.printStackTrace();
}
}
}
}
3、数据库端服务主查询:clientYWSS,用于接收转换端数据,查询并返回结果给转换端。
public class clientYWSS implements Runnable {
PublicAppService publicAppService=new PublicAppService();
CommonTool commonTool=new CommonTool();
XmlEntity appxmlentity=new XmlEntity();
private static Integer sotimeout=1*1*5000;//超时时间,以毫秒为单位
private boolean close = false; // 关闭连接标志位,true表示关闭,false表示连接
String ip;
Integer port;
Socket socket;
MyBlockingQueue myBlockingQueue;
public clientYWSS(String ip, Integer port, MyBlockingQueue myBlockingQueue) {
super();
this.ip = ip;
this.port = port;
this.myBlockingQueue=myBlockingQueue;
init();
}
/**
* 初始化socket对象
*/
public void init(){
try {
//InetAddress address = InetAddress.getByName(ip);
System.out.println("YW端开启连接。。。");
socket = new Socket(ip,port);
socket.setKeepAlive(true);//开启保持活动状态的套接字
//socket.setSoTimeout(sotimeout);//设置超时时间
System.out.println("YW端开启连接。。。成功!");
close=commonTool.isServerClose(socket);
myBlockingQueue.put(socket);//加入线程安全队列
}catch(Exception e){
//e.printStackTrace();
}
}
@Override
public void run() {
Socket TempSocket;
while(true){
//---------读数据---------------------------
TempSocket=myBlockingQueue.get();
//while(commonTool.isServerClose(TempSocket)){
if(null!=TempSocket){
close = commonTool.isServerClose(TempSocket);//判断是否断开
if(!close){//没有断开,开始读数据
Message readMessage =commonTool.ReadText(TempSocket);
if(null!=readMessage){
System.out.println("YW读取数据:"+readMessage);
appxmlentity=publicAppService.messageReceived(readMessage);//根据请求中请求码查询不同接口
Message response =Message.createDefaultMessage(
appxmlentity.buildAsBytes());
System.out.println("YW端原始数据:"+response.toString());
boolean b=commonTool.Send(TempSocket,response);
if(b){
System.out.println("业务端回复成功!");
}
}
myBlockingQueue.put(TempSocket);
TempSocket=null;
}else{
//---------创建连接-------------------------
try{
TempSocket.close();
socket = new Socket(ip,port);
socket.setKeepAlive(true);
if(!commonTool.isServerClose(socket)){
myBlockingQueue.put(socket);
}else{
socket.close();
}
System.out.println("建立连接成功:"+socket.getLocalAddress()+":"+socket.getLocalPort());*/
}catch(Exception se){
//System.out.println("创建连接失败:"+ip+":"+port);
//close=true;
}
}
}
//}
}
}
}
数据库端服务程序介绍完,下面一篇将介绍网站端部分代码。