|
|
@@ -14,47 +14,61 @@ namespace TimedUpload.QuartzJobs
|
|
|
[DisallowConcurrentExecution]
|
|
|
public class HanTingDataUploadJob:IJob
|
|
|
{
|
|
|
- private readonly ILog log = LogManager.GetLogger(typeof(DataUploadJob));
|
|
|
+ private readonly ILog log = LogManager.GetLogger(typeof(HanTingDataUploadJob));
|
|
|
|
|
|
public void Execute(IJobExecutionContext context)
|
|
|
{
|
|
|
- string[] uploadUrls = Constants.HTUploadUrl.Split('|');
|
|
|
- Dictionary<string, IConnection> connections = new Dictionary<string, IConnection>();
|
|
|
- Dictionary<string, IModel> channels = new Dictionary<string, IModel>();
|
|
|
- Dictionary<string, IBasicProperties> properties = new Dictionary<string, IBasicProperties>();
|
|
|
-
|
|
|
- foreach (string uploadUrl in uploadUrls)
|
|
|
+ log.Info("HanTing数据上传任务开始执行.................\r\n");
|
|
|
+ try
|
|
|
{
|
|
|
- ConnectionFactory factory = new ConnectionFactory();
|
|
|
- factory.HostName = uploadUrl;//主机名,Rabbit会拿这个IP生成一个endpoint,这个很熟悉吧,就是socket绑定的那个终结点。
|
|
|
- factory.UserName = Constants.HTUploadUserName;//默认用户名,用户可以在服务端自定义创建,有相关命令行
|
|
|
- factory.Password = Constants.HTUploadPassword;//默认密码
|
|
|
+ string[] uploadUrls = Constants.HTUploadUrl.Split('|');
|
|
|
+ log.Info("准备连接RabbitMQ,地址:" + Constants.HTUploadUrl + ",端口:5672");
|
|
|
+ Dictionary<string, IConnection> connections = new Dictionary<string, IConnection>();
|
|
|
+ Dictionary<string, IModel> channels = new Dictionary<string, IModel>();
|
|
|
+ Dictionary<string, IBasicProperties> properties = new Dictionary<string, IBasicProperties>();
|
|
|
|
|
|
- factory.AutomaticRecoveryEnabled = true; // 链接断开会自动重连
|
|
|
+ foreach (string uploadUrl in uploadUrls)
|
|
|
+ {
|
|
|
+ log.Info("正在连接RabbitMQ:" + uploadUrl + "...");
|
|
|
+ ConnectionFactory factory = new ConnectionFactory();
|
|
|
+ factory.HostName = uploadUrl;//主机名,Rabbit会拿这个IP生成一个endpoint,这个很熟悉吧,就是socket绑定的那个终结点。
|
|
|
+ factory.UserName = Constants.HTUploadUserName;//默认用户名,用户可以在服务端自定义创建,有相关命令行
|
|
|
+ factory.Password = Constants.HTUploadPassword;//默认密码
|
|
|
+ factory.Port = 5672;
|
|
|
|
|
|
- IConnection connection = factory.CreateConnection();
|
|
|
- IModel channel = connection.CreateModel();
|
|
|
- channel.QueueDeclare("zone.device", true, false, false, null);//创建一个名称为kibaqueue的消息队列
|
|
|
- channel.QueueDeclare("zone.deviceHis", true, false, false, null);//创建一个名称为kibaqueue的消息队列
|
|
|
+ factory.AutomaticRecoveryEnabled = true; // 链接断开会自动重连
|
|
|
|
|
|
- IBasicProperties property = channel.CreateBasicProperties();
|
|
|
- property.ContentType = "text/plain";
|
|
|
- property.DeliveryMode = 2; //持久化
|
|
|
- connections.Add(uploadUrl, connection);
|
|
|
- properties.Add(uploadUrl, property);
|
|
|
- channels.Add(uploadUrl, channel);
|
|
|
- }
|
|
|
+ IConnection connection = factory.CreateConnection();
|
|
|
+ log.Info("RabbitMQ连接成功:" + uploadUrl);
|
|
|
+ IModel channel = connection.CreateModel();
|
|
|
+ channel.QueueDeclare("zone.device", true, false, false, null);//创建一个名称为kibaqueue的消息队列
|
|
|
+ channel.QueueDeclare("zone.deviceHis", true, false, false, null);//创建一个名称为kibaqueue的消息队列
|
|
|
|
|
|
- if (channels.Count > 0)
|
|
|
- {
|
|
|
- SendZoneDevice(channels, properties);
|
|
|
- SendZoneDeviceHis(channels, properties);
|
|
|
- }
|
|
|
+ IBasicProperties property = channel.CreateBasicProperties();
|
|
|
+ property.ContentType = "text/plain";
|
|
|
+ property.DeliveryMode = 2; //持久化
|
|
|
+ connections.Add(uploadUrl, connection);
|
|
|
+ properties.Add(uploadUrl, property);
|
|
|
+ channels.Add(uploadUrl, channel);
|
|
|
+ }
|
|
|
|
|
|
- foreach (KeyValuePair<string, IConnection> item in connections)
|
|
|
+ if (channels.Count > 0)
|
|
|
+ {
|
|
|
+ //SendZoneDevice(channels, properties);
|
|
|
+ log.Info("开始执行历史数据同步...");
|
|
|
+ SendZoneDeviceHis(channels, properties);
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (KeyValuePair<string, IConnection> item in connections)
|
|
|
+ {
|
|
|
+ IConnection connection = item.Value;
|
|
|
+ connection.Close();
|
|
|
+ }
|
|
|
+ log.Info("HanTing数据上传任务执行结束.................\r\n");
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- IConnection connection = item.Value;
|
|
|
- connection.Close();
|
|
|
+ log.Error("HanTing数据上传任务执行错误:" + ex.Message + "===========\r\n" + ex.StackTrace);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -164,7 +178,7 @@ namespace TimedUpload.QuartzJobs
|
|
|
//arguments["x-overflow"] = "reject-publish";
|
|
|
|
|
|
Dictionary<String, String> uploadHis = new Dictionary<string, string>();
|
|
|
- using (StreamReader sr = new StreamReader(@"TextFileHanting.txt"))
|
|
|
+ using (StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "TextFileHanting.txt"))
|
|
|
{
|
|
|
String line = "";
|
|
|
while ((line = sr.ReadLine()) != null)
|
|
|
@@ -310,12 +324,12 @@ namespace TimedUpload.QuartzJobs
|
|
|
private void SavaUploadHis(Dictionary<String,String> uploadHis)
|
|
|
{
|
|
|
// 清除之前的内容
|
|
|
- FileStream stream = File.Open(@"TextFileHanting.txt", FileMode.OpenOrCreate, FileAccess.Write);
|
|
|
+ FileStream stream = File.Open(AppDomain.CurrentDomain.BaseDirectory + "TextFileHanting.txt", FileMode.OpenOrCreate, FileAccess.Write);
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
stream.SetLength(0);
|
|
|
stream.Close();
|
|
|
|
|
|
- using (StreamWriter sw = new StreamWriter(@"TextFileHanting.txt"))
|
|
|
+ using (StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "TextFileHanting.txt"))
|
|
|
{
|
|
|
foreach (var item in uploadHis)
|
|
|
{
|