webservice中使用socket

来源:互联网 发布:北京 软件编程培训班 编辑:程序博客网 时间:2024/06/05 08:18

做的一个基于jdk 使用webservice 发布的接口,项目中服务端是登陆修改密码等接口,当用户登录之后,服务端要判断客户端的状态是否在线。所以就使用了Socket 发送心跳包。

中间开发过程中也出现很多bug,说一下心得吧。
在客户端调用的时候发送着心跳,过不到一分钟就会进异常。
原因就是Socket性能本身就低,所以当他每发送一次就把socket置空。这个问题就解决了。

这是webservice发布的接口,当启动main方法时,接口发布,同时socket也被调用

package com.topwalk.main;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import javax.xml.ws.Endpoint;import com.topwalk.service.impl.DstServiceImpl;import com.topwalk.util.PropertiesUtil;public class ClientUserClass {    public static void main(String[] args) {        LoginServiceImpl loginServiceImpl = new LoginServiceImpl();        String url = PropertiesUtil.getValue("webservice_url");        Endpoint.publish(url, loginServiceImpl);        System.out.println("接口发布成功:"+url);        // 启动监听端口 30000        ServerSocket ss;        try {            ss = new ServerSocket(30000);            // 没有连接这个方法就一直堵塞            Socket s = ss.accept();            // 将请求指定一个线程去执行            System.out.println("执行Socket");            new Thread(new DstServiceImpl(s)).start();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

这是main方法加载的类,也是在服务端new一个类

package com.topwalk.service.impl;import java.net.Socket;import javax.jws.WebService;import javax.xml.ws.WebServiceContext;import com.topwalk.main.LoginServiceImpl;import com.topwalk.util.ClientUtil;/** * @description 服务的启动的线程类 * @author csc */@WebServicepublic class DstServiceImpl implements Runnable {    private WebServiceContext wsContext;    Socket socket = null;    public DstServiceImpl(Socket s) {        this.socket = s;    }    public void run() {        String  clientName=LoginServiceImpl.clientName;//获取客户端已登陆的用户名        String   clientip=LoginServiceImpl.clientIP;//获取客户端登陆的ip        try {            int index = 1;            while (true) {                // 10秒后中断连接                if (index >10) {                    socket.close();                    //ClientUtil.operateRule(clientip, clientName, 1);//这是判断客户端关闭之后调用的公司内部脚本                    System.out.println("客户端关闭");                    break;                }                index++;                Thread.sleep(1 *1000 );// 程序睡眠1秒钟            }        } catch (Exception e) {            e.printStackTrace();        }    }}

=========服务端也就到此结束了==========

=========来客户端开始调用========

在登录成功之后调用,单独放在一个线程里面

 new Thread(new Runnable() {                    @Override                    public void run() {                        Socket socket;                        try {                            while (true) {                                socket = new Socket(LandingMain.ip, 30000);                                socket.sendUrgentData(0xFF); // 发送心跳包                                System.out.println("目前处于链接状态!");                                socket.close();                                socket = null;                                Thread.sleep(3 * 1000);// 线程睡眠3秒                            }                        } catch (Exception e1) {                            socket = null;                            new ExceptionWindows();                        }                    }                    }).start();

//这样,当启动时,就会一直发送心跳。