ZengTao
2024-05-20 103e41d02eaccd836870bce3e544bb794447e2ac
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
package com.example.springboot.component;
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
 
 
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
 
@Component
public class PlcParameter extends Thread {
 
//  private final MessageSender messageSender;
//
//  public PlcParameter(MessageSender messageSender) {
//    this.messageSender = messageSender;
//  }
 
  @Override
  public void run() {
    while (this != null) {
      try {
//        try {
//          RabbitMQUtils.sendMessage("Hello, world!", "module_queue");
//        } catch (Exception e) {
//          e.printStackTrace();
//        }
//        try {
//          RabbitMQUtils.receiveMessage("module_queue");
//        } catch (Exception e) {
//          e.printStackTrace();
//        }
//        String message = "Hello, RabbitMQ!";
//        messageSender.sendMessage(message);
        Thread.sleep(500);
 
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
 
 
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("key1", "value1");
      jsonObject.put("key2", 123);
      JSONArray jsonArray = new JSONArray();
      jsonArray.add("item1");
      jsonArray.add("item2");
      jsonObject.put("key3", jsonArray);
 
      try {
        // 将 JSON 对象转换为字符串并发送到队列//RabbitMQUtils.sendJsonMessage(jsonObject, "json_queue");
      } catch (Exception e) {
        e.printStackTrace();
      }
 
      try {
        // 从队列接收并解析 JSON 消息为 JSONObject 对象
        JSONObject receivedJsonObject = RabbitMQUtils.receiveJsonMessage(JSONObject.class, "json_queue");
        System.out.println(receivedJsonObject.toString());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
 
 
}