2 meilixinteng meilixinteng 于 2016.09.19 10:09 提问

mq问题,谁能给注释一下。看不懂了 2C

package com.dcsoft.sdfq.activeMq.sender;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import com.dcsoft.sdfq.activeMq.db.DBQuery;
import com.dcsoft.sdfq.activeMq.util.LoadProperties;
import com.dcsoft.sdfq.activeMq.util.ToolSupport;

import dba.DBMap;

public class SendMessageThread extends Thread {

public static Logger log = Logger.getLogger(SendMessageThread.class);

private SmsMQSender sms = null;

private String clientIpTemp = "";

public SendMessageThread(String id) {
    super(id);
}

public void run() {
    // 与消息中间件进行通讯的对象
    sms = new SmsMQSender();
    // 获取本地客户端ip
    String ips = null;
    String[] strs = getAllIP();
    if (strs != null && strs.length > 0) {
        String ipPrefix = LoadProperties.getInstance().readINI("clientID");
        ips = ipPrefix;
    }
    if (ips == null)
        return;
    clientIpTemp = "tcp://" + ips + ":6161";

    while (true) {
        // CheckExistsMessage.check();
        checkAndSend();
        SendMessageThread smt = (SendMessageThread) Thread.currentThread();
        smt.shortWaiting();
    }
}

private void checkAndSend() {
    String returnID = "";
    ResultSet rst, sscs, xzqh = null;
    try {
        DBQuery db = new DBQuery();
        // 查询任务表
        String sql = "SELECT TOP 1 * FROM tbl_sender WHERE senderType = 0";
        ResultSet rs = db.executeQuery(sql);
        if (rs.next()) {
            String id = rs.getString("id");
            String tblName = rs.getString("tblName");
            String idName = rs.getString("idName");
            String idValue = rs.getString("idValue");
            String dealType = rs.getString("dealType");
            String clxxid = rs.getString("clxxid");
            String dataType = rs.getString("dataType");
            String chp = rs.getString("cph");
            db.closeAll();

            // 查询需要同步的数据
            if (dealType.equals("delete")) {
                db = new DBQuery();
                String sqlDel = "update " + tblName + " set datatype='2' where " + idName + " = '" + idValue + "'";
                db.executeUpdate(sqlDel);
                db.closeAll();
                sql = "select '" + idValue + "' as " + idName;
            } else
                sql = "select * from " + tblName + " where " + idName + " = '" + idValue + "'";
            rs.close();
            rs = null;
            db = new DBQuery();
            rs = db.executeQuery(sql);
            DBMap dbmap = new DBMap(rs, tblName, id, clientIpTemp, idName, dealType, clxxid, idValue, dataType, chp, new ToolSupport()
                    .getCityByDwbm(LoadProperties.getInstance().readINI("dwbh")), new ToolSupport().getXzqhByDwbm(LoadProperties.getInstance().readINI(
                    "dwbh")));
            db.closeAll();
            if (dbmap.isEmpty()) {
                sql = "delete from tbl_sender where (tblName = '" + tblName + "' and idValue = '" + idValue + "') ";
                db = new DBQuery();
                db.executeUpdate(sql);
                db.closeAll();
                return;
            }
            // 发送消息
            returnID = sms.sendMessage(dbmap);

            if (!returnID.equalsIgnoreCase("")) {
                sql = "UPDATE tbl_sender SET senderType = 1, sendtime = getdate() WHERE id = '" + returnID + "'";
                db = new DBQuery();
                db.executeUpdate(sql);
                db.closeAll();
            }
        } else {
            db.closeAll();
            SendMessageThread smt = (SendMessageThread) Thread.currentThread();
            smt.longWaiting();
        }
    } catch (SQLException e) {
        log.info("SendMessageThread 异常1:" + e);
        SendMessageThread smt = (SendMessageThread) Thread.currentThread();
        smt.longWaiting();
    } catch (Exception e) {
        log.info("SendMessageThread 异常2:" + e);
        SendMessageThread smt = (SendMessageThread) Thread.currentThread();
        smt.longWaiting();
    }
    return;
}

void shortWaiting() {
    try {
        sleep(Integer.parseInt(LoadProperties.getInstance().readINI("clientSenderThreadTime"))); // 发送消息
    } catch (Exception e) {
        log.info("SendMessageThread.java 休眠异常" + e);
    }
}

void MiddleWaiting() {
    try {
        sleep(Integer.parseInt(LoadProperties.getInstance().readINI("clientSenderThreadMiddleTime")));// 中断情况
    } catch (Exception e) {
        log.info("SendMessageThread.java 休眠异常" + e);
    }
}

void longWaiting() {
    try {
        sleep(Integer.parseInt(LoadProperties.getInstance().readINI("clientSenderThreadLongTime"))); // 异常情况
    } catch (Exception e) {
        log.warn("SendMessageThread.java 休眠异常" + e);
    }
}

public static String[] getAllIP() {
    try {
        InetAddress localHost = InetAddress.getLocalHost();
        String hostName = localHost.getHostName();
        InetAddress[] inetAdds = InetAddress.getAllByName(hostName);
        String[] ips = new String[inetAdds.length];
        for (int i = 0; i < inetAdds.length; i++)
            ips[i] = inetAdds[i].getHostAddress();
        return ips;
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return null;
}

}

1个回答

zqbnqsdsmd
zqbnqsdsmd   2016.09.28 01:13
Csdn user default icon
上传中...
上传图片
插入图片