作者 朱兆平

代理人发送主线程 线程保护子线程

@@ -46,9 +46,22 @@ public class Main { @@ -46,9 +46,22 @@ public class Main {
46 if (LOGIN_OK && client != null) { 46 if (LOGIN_OK && client != null) {
47 logger.info("start run Timer,client hasocde=" + client.hashCode()); 47 logger.info("start run Timer,client hasocde=" + client.hashCode());
48 48
49 - //代理人发送程序 49 + while (true){
  50 + if(!AgentSender.isrunning){
50 AgentSender send = new AgentSender(client); 51 AgentSender send = new AgentSender(client);
51 send.start(); 52 send.start();
  53 + }
  54 + try {
  55 + Thread.sleep(50000);
  56 + } catch (Exception e) {
  57 + e.printStackTrace();
  58 + logger.info(e.getMessage());
  59 + }
  60 +
  61 + }
  62 +
  63 + //代理人发送程序
  64 +
52 65
53 //数据仓库给九州的发送程序 66 //数据仓库给九州的发送程序
54 // Timer timer = new Timer(); 67 // Timer timer = new Timer();
@@ -11,7 +11,8 @@ public class AgentSender extends Thread{ @@ -11,7 +11,8 @@ public class AgentSender extends Thread{
11 11
12 private static final Logger logger = Logger.getLogger(AgentSender.class); 12 private static final Logger logger = Logger.getLogger(AgentSender.class);
13 13
14 - public volatile boolean bool = false; 14 + public static boolean isrunning;
  15 +
15 Jedis jedis = new Jedis("10.50.3.73", 6379); 16 Jedis jedis = new Jedis("10.50.3.73", 6379);
16 17
17 private IMFClient client; 18 private IMFClient client;
@@ -24,47 +25,77 @@ public class AgentSender extends Thread{ @@ -24,47 +25,77 @@ public class AgentSender extends Thread{
24 public void run(){ 25 public void run(){
25 logger.info(jedis); 26 logger.info(jedis);
26 27
27 - while(!bool){ 28 + try {
  29 +
  30 + if (this.client == null) {
  31 + logger.info("IMFClient has been closed");
  32 + return;
  33 + }
  34 + isrunning=true;
28 if(Main.isSuc){ 35 if(Main.isSuc){
  36 + logger.info("《《《主线程初始化成功。。。。》》》");
29 if(Main.LOGIN_OK){ 37 if(Main.LOGIN_OK){
  38 + logger.info("《《《登录成功开始轮询500mm一次。。。。》》》");
  39 + while(true){
30 String SendMsg = jedis.rpoplpush("task-queue", "tmp-queue"); //取队列消息 40 String SendMsg = jedis.rpoplpush("task-queue", "tmp-queue"); //取队列消息
31 -  
32 //队列消息不为NULL开始发送 41 //队列消息不为NULL开始发送
33 if(SendMsg !=null && SendMsg.length() != 0){ 42 if(SendMsg !=null && SendMsg.length() != 0){
34 - logger.info("《《《已读取队列消息,请关注下方发送信息LOG回显》》》");  
35 - try{  
36 - String returnMsg = client.sendMSG(SendMsg); 43 +
  44 + String returnMsg = this.client.sendMSG(SendMsg);
37 logger.debug("<<发送返回的信息:"+returnMsg+">>"); 45 logger.debug("<<发送返回的信息:"+returnMsg+">>");
38 46
39 //9是发送成功 47 //9是发送成功
40 if(returnMsg.indexOf("<CODE>9</CODE>")>=0){ 48 if(returnMsg.indexOf("<CODE>9</CODE>")>=0){
  49 + try {
41 jedis.rpop("tmp-queue"); 50 jedis.rpop("tmp-queue");
42 logger.debug("<<移除队列>>"); 51 logger.debug("<<移除队列>>");
  52 + } catch (Exception e) {
  53 + e.printStackTrace();
  54 + logger.info(e.getMessage());
  55 + logger.info("!!移除队列失败!!");
  56 + }
  57 +
  58 +
43 }else{ 59 }else{
44 //如果发送不成功继续发送 60 //如果发送不成功继续发送
  61 + try{
45 jedis.rpoplpush("tmp-queue", "task-queue"); 62 jedis.rpoplpush("tmp-queue", "task-queue");
46 logger.info("!!<<执行未成功发送的消息返回到发送消息队列>>!!"); 63 logger.info("!!<<执行未成功发送的消息返回到发送消息队列>>!!");
47 - }  
48 }catch (Exception e){ 64 }catch (Exception e){
49 e.printStackTrace(); 65 e.printStackTrace();
50 - logger.info("!!<<请注意!发送异常====>>!!"); 66 + logger.info(e.getMessage());
  67 + logger.info("!!回写TMP到TASK队列失败!!");
51 } 68 }
52 69
53 - }else {  
54 - logger.info("*********当前队列为空*********"); 70 +
55 } 71 }
56 72
57 - }else{  
58 - logger.info("---------登陆中"); 73 +
59 } 74 }
60 75
  76 + //发送延迟
61 try { 77 try {
62 - Thread.sleep(500);//发送延迟 78 + Thread.sleep(500);
63 } catch (Exception e) { 79 } catch (Exception e) {
64 e.printStackTrace(); 80 e.printStackTrace();
  81 + logger.info(e.getMessage());
  82 + }
  83 + }
  84 + }else{
  85 + logger.info("---------登陆中");
65 } 86 }
66 } 87 }
  88 +
  89 + }catch (Exception var14) {
  90 + var14.printStackTrace();
  91 + logger.info(var14.getMessage());
67 } 92 }
  93 +
  94 +
  95 +
  96 +
  97 + isrunning=false;
  98 + logger.info("---------线程不在了");
68 } 99 }
69 100
70 } 101 }