IMF_Task.java
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.tianbo.imfClient.schedul;
import com.caac.imf.api.IMFClient;
import com.caac.imf.api.IMFClientFactory;
import com.tianbo.util.IO.FileTool;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;
import java.text.SimpleDateFormat;
@Component
public class IMF_Task {
protected static final Logger logger = Logger.getLogger(IMF_Task.class);
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static boolean LOGIN_OK = false;
public static boolean isSuc = true;
public static IMFClient client = null;
public static String loginname;
public static String loginpass;
public static String isNeedSend = "N";
@Autowired
private RestOperations restTemplate;
// @Scheduled(fixedRate = 5000)
public void start() throws Exception {
PropertyConfigurator.configure("config/log4j.properties");
client = IMFClientFactory.createInstance();
loginname= FileTool.readProperties("loginname");
loginpass= FileTool.readProperties("loginpass");
isNeedSend= FileTool.readProperties("isNeedSend");
logger.info(restTemplate);
restTemplate.put("http://fimsagentserver/msgAgent","<MSG>\n" +
"\t<META>\n" +
"\t\t<SNDR>FIMS</SNDR>\n" +
"\t\t<RCVR/>\n" +
"\t\t<SEQN>315756</SEQN>\n" +
"\t\t<DDTM>20190401020521</DDTM>\n" +
"\t\t<TYPE>DFOE</TYPE>\n" +
"\t\t<STYP>DFDL</STYP>\n" +
"\t</META>" +
"</MSG>");
//登录
if (!LOGIN_OK) {
loginIMF(client, loginname, loginpass, "config/imf_config.properties");
}
//启动读取线程
if (client != null) {
IMF_Reader reader = new IMF_Reader();
if (!IMF_Reader.isrunning) {
reader.start();
logger.info("*********读取线程已开启***********");
} else {
// logger.info("*********读取线程已开启-不再启动线程*********");
}
if("Y".equals(isNeedSend)){
IMF_Sender kako_sender = new IMF_Sender(client);
if(!IMF_Sender.isrunning) {
kako_sender.start();
}
}
}
}
/**
* 对外部调用提供的的发送接口
* @param msg
*/
public void sendMsg(String msg){
if (!msg.equals(null) && !msg.isEmpty()){
if(LOGIN_OK && null!=client && isSuc){
IMF_Sender sender = new IMF_Sender(client,msg);
sender.setContent(msg);
sender.start();
}
}
}
/**
* 登录流程
* @param client 客户端实例
* @param userName 登录账号
* @param password 登录密码
* @param confFileName IMF服务器信息配置文件路径
*/
private static void loginIMF(IMFClient client, String userName, String password, String confFileName) {
if (client.initial(confFileName)) {
String message = client.login(userName, password);
logger.info("message=" + message);
if (message.indexOf("<CODE>1</CODE>") > 0) {
logger.info("登陆成功");
LOGIN_OK = true;
} else {
int times = 0;
while(times <= 3) {
logger.info("try connection...");
++times;
logger.info("message.=" + message);
if (message.indexOf("<CODE>1</CODE>") > 0) {
logger.info("登陆成功");
LOGIN_OK = true;
break;
}
logger.info("登录失败~~~~");
message = client.login(userName, password);
try {
Thread.sleep(4000L);
} catch (InterruptedException var7) {
var7.printStackTrace();
}
}
if (!LOGIN_OK) {
logger.info("多次尝试登录失败,退出登陆");
client.disconnect();
isSuc =false;
System.exit(-1);
}
}
}
}
}