New file |
| | |
| | | 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(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | } |