javawebsocket断开重连
#java websocket断开重连,java客户端websocket断开重连
最近⼀个需求。需要⽤java客户端的⽅式连接远程的webscoket 站点。需要保证连接断开⾃动重连,于是基于Java-WebSocket这个开源代码实现了断开重连,代码⽐较简单,如下,还在完善中。。。⾸先需要注意的是为了防⽌重试给远端的websocket 站点带来过⼤的压⼒,我们需要设计⼀个规则,重试间隔随着重试次数的增加⽽不断增加,防⽌给远端站点带来过⼤压⼒。具体代码如下:
Java-WebSocket pom
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.9</version>
</dependency>
package com.utils;
import java.URI;
import java.util.Map;
import java.util.Timer;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ServerHandshake;党员民主评议个人总结
public abstract class ReconnectingWebSocketClient extends WebSocketClient {
private boolean debug =true;
private Integer reconnectInterval =1000;
private Integer maxReconnectInterval =30000;
蓝莲花
private Double reconnectDecay =1.5;
private Integer reconnectAttempts =0;
private Integer maxReconnectAttempts =5000;
private Boolean forcedClo =fal;
private Timer reconnectTimer;
private Boolean isReconnecting =fal;
private ReschedulableTimerTask reconnectTimerTask;
public ReconnectingWebSocketClient( URI rverUri , Draft protocolDraft ){
super( rverUri, protocolDraft, null,0);披肩围巾
}自食其言
public ReconnectingWebSocketClient(URI rverUri, Draft protocolDraft, Map<String, String> httpHeaders,
int connectTimeout){
super(rverUri, protocolDraft, httpHeaders, connectTimeout);
// TODO Auto-generated constructor stub
}
@Override
public void onClo(int arg0, String arg1,boolean arg2){ if(forcedClo){
// 调⽤clo ⽅法
clo(arg0, arg1, arg2);
}el{
if(!isReconnecting){
restartReconnectionTimer();
}
isReconnecting =true;
}
}
@Override
public void onError(Exception exception){
error(exception);
}
@Override
public void onMessage(String message){
message(message);
芝士焗土豆}
@Override
public void onOpen(ServerHandshake arg0){
open(arg0);
}
private void restartReconnectionTimer(){ cancelReconnectionTimer();
reconnectTimer =new Timer("reconnectTimer"); reconnectTimerTask =new ReschedulableTimerTask(){ @Override
public void run(){
if(reconnectAttempts >= maxReconnectAttempts){ cancelReconnectionTimer();
cancelReconnectionTimer();
if(debug){
System.out.println("以达到最⼤重试次数:"+ maxReconnectAttempts +",已停⽌重试"); }
}
reconnectAttempts++;
try{
Boolean isOpen =reconnectBlocking();
if(isOpen){
if(debug){
System.out.println("连接成功,重试次数为:"+ reconnectAttempts);
}
cancelReconnectionTimer();
reconnectAttempts =0;
isReconnecting =fal;
}el{
if(debug){
System.out.println("连接失败,重试次数为:"+ reconnectAttempts);
}
double timeoutd = reconnectInterval * Math.pow(reconnectDecay, reconnectAttempts); int timeout = Integer.parInt(DecimalFormat("0").format(timeoutd)); timeout = timeout > maxReconnectInterval ? maxReconnectInterval : timeout; System.out.println(timeout);
<_schedule2(timeout);
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
};
reconnectTimerTask.schedule(reconnectTimer,reconnectInterval);
}
private void cancelReconnectionTimer(){
if(reconnectTimer != null){
reconnectTimer.cancel();
reconnectTimer = null;
}
if(reconnectTimerTask != null){
沉寂是什么意思
reconnectTimerTask.cancel();
reconnectTimerTask = null;
}
}
// ABTRACT METHODS /
public abstract void open(ServerHandshake handshakedata); public abstract void message(String message);
public abstract void clo(int code, String reason,boolean remote); public abstract void error(Exception ex);
}
// 另⼀个核⼼,修改java定时器的执⾏频率
package com.utils;
import flect.Field;
SimpleDateFormat;
import java.util.Date;
托福词汇import java.util.Timer;
import java.util.TimerTask;
public abstract class ReschedulableTimerTask extends TimerTask{
public int i =0;
椰汁红豆糕
public void schedule(Timer timer,int checkInterval){
timer.scheduleAtFixedRate(this,0, checkInterval);
}
public void re_schedule2(int newCheckInterval){
Date now =new Date();
SimpleDateFormat dateFormat =new SimpleDateFormat(
"E MMMMM dd HH:mm:ss zzz yyyy");
System.out.println("re_schedule2:"+ dateFormat.format(now).toString());
long nextExecutionTime = Time()+ newCheckInterval; tDeclaredField(TimerTask.class,this,"nextExecutionTime", nextExecutionTime); tDeclaredField(TimerTask.class,this,"period", newCheckInterval);
}
static boolean tDeclaredField(Class<?> clazz, Object obj,
String name, Object value){
try{
Field field = DeclaredField(name);
field.tAccessible(true);
field.t(obj, value);
return true;
}catch(Exception ex){
ex.printStackTrace();
return fal;
}
}
}