定时任务(注解方式)判断两台web服务是否挂掉

该文章介绍了一个使用Spring配置的定时任务,该任务每三分钟执行一次,目的是检查两台服务器间的Web服务是否可达,以此判断服务是否挂掉。通过注解`@Scheduled`定义定时任务,结合`SchedulerService`管理定时任务的启停,根据服务器配置文件判断主从服务器角色,动态调整定时任务状态。
摘要由CSDN通过智能技术生成

此定时任务,用于监测两台服务器,互相之间的web是否能通,来判断web服务是否挂掉。
-------- spring配置的XML 需要加上红色代码 ----------------

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans ;
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task.xsd"
default-autowire="byName" default-lazy-init="false">

    <!-- task任务扫描注解 -->
    <task:annotation-driven/>
    <!-- 扫描位置 -->
    <context:component-scan base-package="com.hotent.mini.job.quartz"/>

--------- 定时任务执行的Java 通过注解@Scheduled(cron = “0 0/3 * * * ?”) 每三分钟执行一次 ---------

package com.hotent.mini.job.quartz;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.hotent.base.core.scheduler.SchedulerService;
import com.hotent.base.core.util.FileUtil;
/**
 * 定时任务,此类没有在计划任务内跑,使用注解的方式运行
 * @author KingFu
 */
@Component
public class QuartzJob {
protected Logger logger = LoggerFactory.getLogger(QuartzJob.class);
@Resource
SchedulerService schedulerService;
/** 
* 检查服务器是否挂了.现每3分钟检查一次。
*  前提,主机默认打开定时计划
*  场景一:如果是主机,且当前已经开了定时,后续不用操作
*  场景二:如果是副机,且当前已经开了定时,判断主机是否已经开启web,如果主机已开启且副机已经开了则把当前副机的定时关掉,如果主机未开且主
*  场景二:如果是副机,且当前已经未开定时,判断主机是否已经开启web,如果副机已经开了则后续不用操作,否则打开副机的定时
*  schedulerService.start()开户定时,schedulerService.shutdown()关闭定时,schedulerService.isInStandbyMode()是否挂起
*/
@Scheduled(cron = "0 0/3 * * * ?")
    public void checkServer() {
InetAddress addr;
try {
addr = InetAddress.getLocalHost();
//System.out.println("Hello quartz! now ->" + new Date() + ",Local HostAddress: "+addr.getHostAddress()+","+schedulerService.isStarted()+","+schedulerService.isInStandbyMode());
//        logger.debug("Hello quartz! now ->" + new Date() + ",Local HostAddress: "+addr.getHostAddress()+","+schedulerService.isStarted()+","+schedulerService.isInStandbyMode());
        String path= FileUtil.getClassesPath() +"/conf/x5-base-db.properties".replace("/", File.separator);
String localServerHost = FileUtil.readFromProperties(path, "localServerHost");
int localServerPort = Integer.valueOf(FileUtil.readFromProperties(path, "localServerPort"));
String otherServerHost = FileUtil.readFromProperties(path, "otherServerHost");
int otherServerPort = Integer.valueOf(FileUtil.readFromProperties(path, "otherServerPort"));
String isMain = FileUtil.readFromProperties(path, "isMain");
//logger.debug("aaaaaaaa:"+localServerHost+","+localServerPort+","+otherServerHost+","+otherServerPort+","+isMain);
//logger.debug("cccccccc:"+isConnect(localServerHost, localServerPort)+"dddddddd:"+isConnect(otherServerHost, otherServerPort));
if( "true".equals(isMain) && isConnect(localServerHost, localServerPort) ) {
    // 主机需要保证定时计划打开
if(schedulerService.isInStandbyMode()) {
schedulerService.start();
}
    }else {
    // 如果是副机,且当前已经开了定时,判断主机是否已经开启web,如果主机已开启且副机已经开了则把当前副机的定时关掉,如果主机未开且主
    if( isConnect(localServerHost, localServerPort) && !schedulerService.isInStandbyMode() ) { // 本机已启动定时计划 
    if( isConnect(otherServerHost, otherServerPort) ) { // 能连主服务器
    schedulerService.shutdown();
    }
    } // 如果是副机,且当前已经未开定时,判断主机是否已经开启web,如果副机已经开了则后续不用操作,否则打开副机的定时
    else if( isConnect(localServerHost, localServerPort) && schedulerService.isInStandbyMode() ) { // 本机未启动定时计划 
    if( !isConnect(otherServerHost, otherServerPort) ) { // 不能连主服务器
    schedulerService.start();
    }
    }
    }
     
} catch (Exception e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(baos));
        String exception = baos.toString();
        logger.error("QuartzJob checkServer Exception ----------------------------------  "+exception);
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
// 检测IP和端口是否能连通
public boolean isConnect(String host,int port){
Socket socket = new Socket();
try{
socket.connect(new InetSocketAddress(host, port));
}catch (IOException e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(baos));
        String exception = baos.toString();
        logger.error("QuartzJob isConnect Exception ----------------------------------  "+exception);
return false;
}finally{
try{
socket.close();
}catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
 
public static void main(String args[]) {
QuartzJob q = new QuartzJob();
String path= FileUtil.getClassesPath() +"/conf/x5-base-db.properties".replace("/", File.separator);
String localServerHost = FileUtil.readFromProperties(path,"localServerHost");
int localServerPort = Integer.valueOf(FileUtil.readFromProperties(path,"localServerPort"));
String otherServerHost = FileUtil.readFromProperties(path,"otherServerHost");
int otherServerPort = Integer.valueOf(FileUtil.readFromProperties(path,"otherServerPort"));
String isMain = FileUtil.readFromProperties(path,"isMain");
SchedulerService schedulerService = new SchedulerService();
System.out.println("true".equals(isMain) +","+ q.isConnect(localServerHost, localServerPort));
 
if("true".equals(isMain) && q.isConnect(localServerHost, localServerPort)) {
     
    }else {
   
    }
System.out.println(q.isConnect(otherServerHost,80)+","+otherServerHost+","+isMain);
}
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值