public class isConnected implements Runnable, Callable<Object> {
private static final Logger logger = (Logger) LoggerFactory.getLogger(isConnected.class);
Properties contextProperties = (Properties) ApplicationContextProvider.getApplicationContext().getBean("contextProperties");
String port1 = contextProperties.getProperty("vms.server.localport");
String ip;
int port;
CMRATemplate cmraInfo;
int checkLimit;
CMRA_STTS_UPDATE CMRAupdater;
isConnected(CMRATemplate cmraInfo, int checkLimit) {
this.checkLimit = checkLimit;
this.cmraInfo = cmraInfo;
voCMRA cmra = cmraInfo.cmra;
CMRAupdater = new CMRA_STTS_UPDATE();
this.ip = cmra.getCMRA_IP();
this.port = parseInt(port1);
// this.port = Integer.parseInt(cmra.getCMRA_PORT());
System.out.println("isConnected 객체 생성. IP : " + ip + " PORT : " + port);
try {
call();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public Object call() throws Exception {
System.out.println("call() 호출");
run();
return null;
}
@Override
public void run() {
System.out.println("run() 호출");
boolean result;
// String ip = "127.0.0.1";
// int port = 7300;
result = availableConnect(ip, port);
// logger.info("ip : "+ip+" port : "+port+" 접속 "+result); ip = "192.168.5.9"; port = 7300;
// result = availableConnect (ip, port );
// logger.info("ip : "+ip+" port : "+port+" 접속 "+result); ip = "14.47.241.143";port = 7300;
// result = availableConnect (ip, port );
// logger.info("ip : "+ip+" port : "+port+" 접속 "+result);
// return;
cmraInfo.cmra_stts.setCMNC_STTS_CD("CMS1");
if (result) {
cmraInfo.cmra_stts.setCMNC_STTS_CD("CMS0");
}
logger.info("ip : " + ip + " port : " + port + " 접속 여부 확인 : " + result);
CMRAupdater.updateCMRA(cmraInfo.cmra_stts);
}
public boolean availableConnect(String host, int port) {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
boolean result = false;
String time = (String) contextProperties.getProperty("Time_timeout");
String checke = (String) contextProperties.getProperty("check_time");
System.out.println(" 타임아웃 설정 : " + time + " 체크 횟수 : " + checke);
// int check = parseInt(checke);
// int timeout = parseInt(time);
int check = parseInt(checke);
int timeout = parseInt(time);
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("result : " + result);
System.out.println("check : " + check);
System.out.println("checkLimit : " + checkLimit);
while (check < checkLimit && result == false) {
System.out.println("===========================================================");
System.out.println("try : " + check + " connect to ip : " + host + " port : " + port);
check++;
Socket socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(host, port);
try {
logger.info("try : " + check + " connect to ip : " + host + " port : " + port + "접속시도");
socket.setSoTimeout(timeout);
socket.connect(socketAddress, timeout);
result = true;
logger.info("try : " + check + " connect to ip : " + host + " port : " + port + "접속성공");
return result;
} catch (SocketException e) {
logger.error("try : " + check + " connect to ip : " + host + " port : " + port + "접속실패1 : "+e);
// e.printStackTrace();
continue;
} catch (IOException e) {
logger.info("try : " + check + " connect to ip : " + host + " port : " + port + "접속실패2");
// e.printStackTrace();
continue;
}
finally {
try {
logger.info("try : " + check + " connect to ip : " + host + " port : " + port + "소켓close");
socket.close();
} catch (IOException e) {
logger.info("try : " + check + " connect to ip : " + host + " port : " + port + "소켓close실패");
e.printStackTrace();
}
}
}
logger.info("try : " + check + " connect to ip : " + host + " port : " + port + "finally이후");
return result;
}
private int parseInt(String time) {
// TODO Auto-generated method stub
return Integer.parseInt(time);
}
}
코드가 정리가 안되고 중구난방으로 적혀있다. 중심적인 내용을 말하자면 run()만 사용하면 실행되지 않는다. 그래서 call() 을 동원하면 아주 잘 동작한다.
'netty' 카테고리의 다른 글
TCP/IP 통신 네티(netty)(1) (0) | 2021.05.24 |
---|---|
JAVA pom.xml 오류 해결 (0) | 2021.05.24 |
netty spring start (0) | 2021.05.17 |
NETTY DECODER (0) | 2021.05.12 |
정보처리기사 시험 팁 (0) | 2021.05.11 |
댓글