9个文件已修改
3 文件已重命名
13个文件已添加
File was renamed from springboot-vue3/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_11_1.xml |
| | |
| | | <component name="libraryTable"> |
| | | <library name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.11.1"> |
| | | <CLASSES> |
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.11.1/jackson-annotations-2.11.1.jar!/" /> |
| | | </CLASSES> |
| | | <JAVADOC> |
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.11.1/jackson-annotations-2.11.1-javadoc.jar!/" /> |
| | | </JAVADOC> |
| | | <SOURCES> |
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.11.1/jackson-annotations-2.11.1-sources.jar!/" /> |
| | | </SOURCES> |
| | | </library> |
| | | <component name="libraryTable">
|
| | | <library name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.13.0">
|
| | | <CLASSES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.13.0/jackson-annotations-2.13.0.jar!/" />
|
| | | </CLASSES>
|
| | | <JAVADOC>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.13.0/jackson-annotations-2.13.0-javadoc.jar!/" />
|
| | | </JAVADOC>
|
| | | <SOURCES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-annotations/2.13.0/jackson-annotations-2.13.0-sources.jar!/" />
|
| | | </SOURCES>
|
| | | </library>
|
| | | </component> |
File was renamed from springboot-vue3/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_11_1.xml |
| | |
| | | <component name="libraryTable"> |
| | | <library name="Maven: com.fasterxml.jackson.core:jackson-core:2.11.1"> |
| | | <CLASSES> |
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.11.1/jackson-core-2.11.1.jar!/" /> |
| | | </CLASSES> |
| | | <JAVADOC> |
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.11.1/jackson-core-2.11.1-javadoc.jar!/" /> |
| | | </JAVADOC> |
| | | <SOURCES> |
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.11.1/jackson-core-2.11.1-sources.jar!/" /> |
| | | </SOURCES> |
| | | </library> |
| | | <component name="libraryTable">
|
| | | <library name="Maven: com.fasterxml.jackson.core:jackson-core:2.13.0">
|
| | | <CLASSES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.13.0/jackson-core-2.13.0.jar!/" />
|
| | | </CLASSES>
|
| | | <JAVADOC>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.13.0/jackson-core-2.13.0-javadoc.jar!/" />
|
| | | </JAVADOC>
|
| | | <SOURCES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-core/2.13.0/jackson-core-2.13.0-sources.jar!/" />
|
| | | </SOURCES>
|
| | | </library>
|
| | | </component> |
File was renamed from springboot-vue3/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_11_1.xml |
| | |
| | | <component name="libraryTable"> |
| | | <library name="Maven: com.fasterxml.jackson.core:jackson-databind:2.11.1"> |
| | | <CLASSES> |
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.11.1/jackson-databind-2.11.1.jar!/" /> |
| | | </CLASSES> |
| | | <JAVADOC> |
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.11.1/jackson-databind-2.11.1-javadoc.jar!/" /> |
| | | </JAVADOC> |
| | | <SOURCES> |
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.11.1/jackson-databind-2.11.1-sources.jar!/" /> |
| | | </SOURCES> |
| | | </library> |
| | | <component name="libraryTable">
|
| | | <library name="Maven: com.fasterxml.jackson.core:jackson-databind:2.13.0">
|
| | | <CLASSES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.13.0/jackson-databind-2.13.0.jar!/" />
|
| | | </CLASSES>
|
| | | <JAVADOC>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.13.0/jackson-databind-2.13.0-javadoc.jar!/" />
|
| | | </JAVADOC>
|
| | | <SOURCES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/fasterxml/jackson/core/jackson-databind/2.13.0/jackson-databind-2.13.0-sources.jar!/" />
|
| | | </SOURCES>
|
| | | </library>
|
| | | </component> |
New file |
| | |
| | | <component name="libraryTable">
|
| | | <library name="Maven: com.rabbitmq:amqp-client:5.13.1">
|
| | | <CLASSES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/rabbitmq/amqp-client/5.13.1/amqp-client-5.13.1.jar!/" />
|
| | | </CLASSES>
|
| | | <JAVADOC>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/rabbitmq/amqp-client/5.13.1/amqp-client-5.13.1-javadoc.jar!/" />
|
| | | </JAVADOC>
|
| | | <SOURCES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/com/rabbitmq/amqp-client/5.13.1/amqp-client-5.13.1-sources.jar!/" />
|
| | | </SOURCES>
|
| | | </library>
|
| | | </component> |
New file |
| | |
| | | <component name="libraryTable">
|
| | | <library name="Maven: org.springframework.amqp:spring-amqp:2.2.9.RELEASE">
|
| | | <CLASSES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/amqp/spring-amqp/2.2.9.RELEASE/spring-amqp-2.2.9.RELEASE.jar!/" />
|
| | | </CLASSES>
|
| | | <JAVADOC>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/amqp/spring-amqp/2.2.9.RELEASE/spring-amqp-2.2.9.RELEASE-javadoc.jar!/" />
|
| | | </JAVADOC>
|
| | | <SOURCES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/amqp/spring-amqp/2.2.9.RELEASE/spring-amqp-2.2.9.RELEASE-sources.jar!/" />
|
| | | </SOURCES>
|
| | | </library>
|
| | | </component> |
New file |
| | |
| | | <component name="libraryTable">
|
| | | <library name="Maven: org.springframework.amqp:spring-rabbit:2.2.9.RELEASE">
|
| | | <CLASSES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/amqp/spring-rabbit/2.2.9.RELEASE/spring-rabbit-2.2.9.RELEASE.jar!/" />
|
| | | </CLASSES>
|
| | | <JAVADOC>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/amqp/spring-rabbit/2.2.9.RELEASE/spring-rabbit-2.2.9.RELEASE-javadoc.jar!/" />
|
| | | </JAVADOC>
|
| | | <SOURCES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/amqp/spring-rabbit/2.2.9.RELEASE/spring-rabbit-2.2.9.RELEASE-sources.jar!/" />
|
| | | </SOURCES>
|
| | | </library>
|
| | | </component> |
New file |
| | |
| | | <component name="libraryTable">
|
| | | <library name="Maven: org.springframework.boot:spring-boot-starter-amqp:2.3.2.RELEASE">
|
| | | <CLASSES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/boot/spring-boot-starter-amqp/2.3.2.RELEASE/spring-boot-starter-amqp-2.3.2.RELEASE.jar!/" />
|
| | | </CLASSES>
|
| | | <JAVADOC>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/boot/spring-boot-starter-amqp/2.3.2.RELEASE/spring-boot-starter-amqp-2.3.2.RELEASE-javadoc.jar!/" />
|
| | | </JAVADOC>
|
| | | <SOURCES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/boot/spring-boot-starter-amqp/2.3.2.RELEASE/spring-boot-starter-amqp-2.3.2.RELEASE-sources.jar!/" />
|
| | | </SOURCES>
|
| | | </library>
|
| | | </component> |
New file |
| | |
| | | <component name="libraryTable">
|
| | | <library name="Maven: org.springframework.retry:spring-retry:1.2.5.RELEASE">
|
| | | <CLASSES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/retry/spring-retry/1.2.5.RELEASE/spring-retry-1.2.5.RELEASE.jar!/" />
|
| | | </CLASSES>
|
| | | <JAVADOC>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/retry/spring-retry/1.2.5.RELEASE/spring-retry-1.2.5.RELEASE-javadoc.jar!/" />
|
| | | </JAVADOC>
|
| | | <SOURCES>
|
| | | <root url="jar://$MAVEN_REPOSITORY$/org/springframework/retry/spring-retry/1.2.5.RELEASE/spring-retry-1.2.5.RELEASE-sources.jar!/" />
|
| | | </SOURCES>
|
| | | </library>
|
| | | </component> |
| | |
| | | <artifactId>gson</artifactId> |
| | | <version>2.8.9</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.rabbitmq</groupId> |
| | | <artifactId>amqp-client</artifactId> |
| | | <version>5.13.1</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-amqp</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.fasterxml.jackson.core</groupId> |
| | | <artifactId>jackson-databind</artifactId> |
| | | <version>2.12.2</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.fasterxml.jackson.core</groupId> |
| | | <artifactId>jackson-core</artifactId> |
| | | <version>2.13.0</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.fasterxml.jackson.core</groupId> |
| | | <artifactId>jackson-databind</artifactId> |
| | | <version>2.13.0</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.fasterxml.jackson.core</groupId> |
| | | <artifactId>jackson-annotations</artifactId> |
| | | <version>2.13.0</version> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | <build> |
| | |
| | | <orderEntry type="library" name="Maven: io.jsonwebtoken:jjwt-api:0.10.7" level="project" /> |
| | | <orderEntry type="library" scope="RUNTIME" name="Maven: io.jsonwebtoken:jjwt-impl:0.10.7" level="project" /> |
| | | <orderEntry type="library" scope="RUNTIME" name="Maven: io.jsonwebtoken:jjwt-jackson:0.10.7" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.11.1" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.11.1" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.11.1" level="project" /> |
| | | <orderEntry type="library" name="Maven: org.apache.shiro:shiro-spring-boot-starter:1.8.0" level="project" /> |
| | | <orderEntry type="library" name="Maven: org.apache.shiro:shiro-spring:1.8.0" level="project" /> |
| | | <orderEntry type="library" name="Maven: org.apache.shiro:shiro-core:1.8.0" level="project" /> |
| | |
| | | <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.3.2.RELEASE" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.4.5" level="project" /> |
| | | <orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.2.8.RELEASE" level="project" /> |
| | | <orderEntry type="library" name="Maven: org.springframework:spring-tx:5.2.8.RELEASE" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-core:3.2.0" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-annotation:3.2.0" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.github.jsqlparser:jsqlparser:2.1" level="project" /> |
| | |
| | | <orderEntry type="library" name="Maven: org.springframework:spring-websocket:5.2.8.RELEASE" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.github.xingshuangs:iot-communication:1.4.2" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.9" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.rabbitmq:amqp-client:5.13.1" level="project" /> |
| | | <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-amqp:2.3.2.RELEASE" level="project" /> |
| | | <orderEntry type="library" name="Maven: org.springframework.amqp:spring-rabbit:2.2.9.RELEASE" level="project" /> |
| | | <orderEntry type="library" name="Maven: org.springframework.amqp:spring-amqp:2.2.9.RELEASE" level="project" /> |
| | | <orderEntry type="library" name="Maven: org.springframework.retry:spring-retry:1.2.5.RELEASE" level="project" /> |
| | | <orderEntry type="library" name="Maven: org.springframework:spring-tx:5.2.8.RELEASE" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.13.0" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.13.0" level="project" /> |
| | | <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.13.0" level="project" /> |
| | | </component> |
| | | </module> |
| | |
| | | package com.example.springboot; |
| | | |
| | | import com.example.springboot.common.MessageSender; |
| | | import org.springframework.amqp.rabbit.annotation.EnableRabbit; |
| | | import org.springframework.boot.SpringApplication; |
| | | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| | | import org.springframework.context.ConfigurableApplicationContext; |
| | | |
| | | import com.example.springboot.component.*; |
| | | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| | | import org.springframework.context.annotation.ComponentScan; |
| | | |
| | | |
| | | @SpringBootApplication |
| | |
| | | SpringApplication springApplication = new SpringApplication(AuthorityApplication.class); |
| | | ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args); |
| | | WebSocketServer.setApplicationContext(configurableApplicationContext); |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.example.springboot.common;
|
| | |
|
| | | import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
| | | import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
| | | import org.springframework.amqp.support.AmqpHeaders;
|
| | | import org.springframework.beans.factory.annotation.Autowired;
|
| | | import org.springframework.messaging.handler.annotation.Header;
|
| | | import org.springframework.stereotype.Component;
|
| | | import com.rabbitmq.client.Channel;
|
| | | import java.io.IOException;
|
| | |
|
| | |
|
| | | @Component
|
| | | public class MessageReceiver {
|
| | |
|
| | | @RabbitListener(queues = RabbitConfig.QUEUE_NAME)
|
| | | public void receiveMessage(String message) {
|
| | | System.out.println("Received message: " + message);
|
| | |
|
| | | // 手动确认消息
|
| | | //channel.basicAck(deliveryTag, false);
|
| | | }
|
| | |
|
| | | @RabbitListener(queues = RabbitConfig.SECOND_QUEUE_NAME)
|
| | | public void receiveMessage2(MessageSender.Person person) {
|
| | | // 对接收到的Person对象进行处理
|
| | | String name = person.getName();
|
| | | int age = person.getAge();
|
| | |
|
| | |
|
| | | System.out.println("Received Person message: " + name +age);
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | }
|
| | |
|
New file |
| | |
| | | package com.example.springboot.common;
|
| | |
|
| | | import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
| | | import org.springframework.beans.factory.annotation.Autowired;
|
| | | import org.springframework.stereotype.Component;
|
| | |
|
| | | import java.io.Serializable;
|
| | |
|
| | | @Component
|
| | | public class MessageSender {
|
| | |
|
| | | @Autowired
|
| | | private RabbitTemplate rabbitTemplate;
|
| | |
|
| | | public void sendMessage(String message) {
|
| | | rabbitTemplate.convertAndSend(RabbitConfig.DIRECT_EXCHANGE_NAME, RabbitConfig.ROUTING_KEY, message);
|
| | | System.out.println("send message: " + message);
|
| | | }
|
| | |
|
| | | public void sendMessage2(Person person) {
|
| | | rabbitTemplate.convertAndSend(RabbitConfig.DIRECT_EXCHANGE_NAME, RabbitConfig.SECOND_ROUTING_KEY, person);
|
| | | }
|
| | |
|
| | | public static class Person implements Serializable {
|
| | | private String name;
|
| | | private int age;
|
| | |
|
| | | public Person(String name, int age) {
|
| | | this.name = name;
|
| | | this.age = age;
|
| | | }
|
| | |
|
| | | public String getName() {
|
| | | return name;
|
| | | }
|
| | |
|
| | | public int getAge() {
|
| | | return age;
|
| | | }
|
| | |
|
| | | // getter和setter方法省略
|
| | | }
|
| | |
|
| | | }
|
| | |
|
New file |
| | |
| | | package com.example.springboot.common;
|
| | |
|
| | | import org.springframework.amqp.core.Binding;
|
| | | import org.springframework.amqp.core.BindingBuilder;
|
| | | import org.springframework.amqp.core.DirectExchange;
|
| | | import org.springframework.amqp.core.Queue;
|
| | | import org.springframework.context.annotation.Bean;
|
| | | import org.springframework.context.annotation.Configuration;
|
| | |
|
| | | @Configuration
|
| | | public class RabbitConfig {
|
| | |
|
| | | // 定义交换机名称
|
| | | public static final String DIRECT_EXCHANGE_NAME = "direct.exchange";
|
| | | // 定义队列名称
|
| | | public static final String QUEUE_NAME = "canadames";
|
| | | // 定义路由键
|
| | | public static final String ROUTING_KEY = "message.routingKey";
|
| | | // 定义第二个队列名称
|
| | | public static final String SECOND_QUEUE_NAME = "anotherQueue";
|
| | | // 定义第二个路由键
|
| | | public static final String SECOND_ROUTING_KEY = "another.routingKey";
|
| | |
|
| | | // 定义交换机
|
| | |
|
| | | // 直连交换机(Direct Exchange):直连交换机是最简单的一种交换机类型。它根据消息的路由键(Routing Key)将消息发送到与之完全匹配的队列。
|
| | | // 主题交换机(Topic Exchange):主题交换机允许通过使用通配符的方式来进行消息的路由。它将消息根据匹配的规则(通配符)发送到一个或多个队列上
|
| | | //扇形交换机(Fanout Exchange):扇形交换机会将收到的所有消息广播到所有绑定的队列上。它忽略消息的路由键,只需将消息发送给所有绑定的队列。
|
| | | @Bean
|
| | | public DirectExchange directExchange() {
|
| | | return new DirectExchange(DIRECT_EXCHANGE_NAME);
|
| | | }
|
| | |
|
| | | // 定义队列
|
| | | @Bean
|
| | | public Queue queue() {
|
| | | return new Queue(QUEUE_NAME);
|
| | | }
|
| | |
|
| | | // 定义第二个队列
|
| | | @Bean
|
| | | public Queue secondQueue() {
|
| | | return new Queue(SECOND_QUEUE_NAME);
|
| | | }
|
| | |
|
| | | //第二个参数表示队列是否为持久化的,如果你想要创建一个持久化的队列,可以将该参数设置为 true。
|
| | | // 持久化队列可以在 RabbitMQ 服务器重启后继续存在,以防止消息丢失。
|
| | | //第三个参数 exclusive 设置为 false,表示队列非排他性;
|
| | | // 将第四个参数 autoDelete 设置为 false,表示队列不会自动删除;
|
| | | // 将第五个参数 arguments 设置为 null,表示不指定其他参数。
|
| | | // @Bean
|
| | | // public Queue queue() {
|
| | | // return new Queue(QUEUE_NAME, false, false, false, null);
|
| | | // }
|
| | |
|
| | | // 将队列绑定到交换机上,并指定路由键
|
| | | @Bean
|
| | | public Binding binding() {
|
| | | return BindingBuilder.bind(queue()).to(directExchange()).with(ROUTING_KEY);
|
| | | }
|
| | |
|
| | | // 将第二个队列绑定到交换机上,并指定第二个路由键
|
| | | @Bean
|
| | | public Binding secondBinding() {
|
| | | return BindingBuilder.bind(secondQueue()).to(directExchange()).with(SECOND_ROUTING_KEY);
|
| | | }
|
| | | }
|
| | |
| | | }
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | private static String extractAddressIndex(String address) {
|
| | | // Assuming the address format is "DB103.DBW0" or "DB103.DBB100~DBB113"
|
| | | if (address.startsWith("DB") && address.contains(".DBW")) {
|
New file |
| | |
| | | package com.example.springboot.component;
|
| | |
|
| | | import com.rabbitmq.client.*;
|
| | |
|
| | | import java.io.IOException;
|
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | |
|
| | | public class MessageQueueReader {
|
| | |
|
| | | private static final String QUEUE_NAME = "hangzhoumes";
|
| | |
|
| | | public static void main(String[] args) throws Exception {
|
| | | ConnectionFactory factory = new ConnectionFactory();
|
| | | factory.setHost("localhost");
|
| | |
|
| | | List<String> messages = new ArrayList<>();
|
| | |
|
| | | try (Connection connection = factory.newConnection();
|
| | | Channel channel = connection.createChannel()) {
|
| | | boolean autoAck = false;
|
| | | // autoAck 参数设置为 false,然后手动确认消息处理完成
|
| | | // 循环获取队列中的所有消息
|
| | | // while (true) {
|
| | | GetResponse response = channel.basicGet(QUEUE_NAME, autoAck);
|
| | |
|
| | | if (response != null) {
|
| | | String message = new String(response.getBody(), "UTF-8");
|
| | | messages.add(message);
|
| | |
|
| | | // 手动确认消息处理完成
|
| | | long deliveryTag = response.getEnvelope().getDeliveryTag();
|
| | | channel.basicAck(deliveryTag, false);
|
| | | // } else {
|
| | | // // 如果队列为空,则退出循环
|
| | | // break;
|
| | | // }
|
| | | }
|
| | | }
|
| | |
|
| | | // 打印所有消息内容
|
| | | for (String message : messages) {
|
| | | System.out.println("Received message: " + message);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
New file |
| | |
| | | package com.example.springboot.component;
|
| | |
|
| | | import com.rabbitmq.client.*;
|
| | |
|
| | | public class ModuleA {
|
| | | private final static String QUEUE_NAME = "hangzhoumes";
|
| | |
|
| | | public static void main(String[] argv) throws Exception {
|
| | | // 创建连接工厂
|
| | | ConnectionFactory factory = new ConnectionFactory();
|
| | | factory.setHost("localhost");
|
| | |
|
| | | try (Connection connection = factory.newConnection();
|
| | | //channel 是通过 connection 创建的一个 AMQP 信道对象
|
| | | Channel channel = connection.createChannel()) {
|
| | | // `channel.queueDeclare(QUEUE_NAME, false, false, false, null);` 是用来声明一个队列的方法
|
| | | //该方法有五个参数:
|
| | | //1. `QUEUE_NAME`:指定队列的名称,这里使用了之前声明的常量`"module_queue"`作为队列名称。
|
| | | //2. `false`:指定队列是否为持久化的。在这里设置为`false`,表示不将队列持久化到磁盘,一旦RabbitMQ服务停止或崩溃,队列将会丢失。
|
| | | //3. `false`:指定是否只允许当前连接声明此队列。在这里设置为`false`,表示可允许其他连接也声明同名的队列。
|
| | | //4. `false`:指定是否在不再使用时自动删除队列。在这里设置为`false`,表示当没有消费者或者所有消费者断开连接后,队列不会自动删除。
|
| | | //5. `null`:指定其他队列属性的参数。在这里设置为`null`,表示没有其他属性需要设置。
|
| | | //执行`channel.queueDeclare()`方法后,如果队列不存在,将会创建一个新的队列,如果队列已经存在,则不做任何操作。
|
| | | channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
| | |
|
| | |
|
| | |
|
| | | String message = "你好模块A2";
|
| | |
|
| | | for ( int i=1;i< 10000;i++)
|
| | | {
|
| | | message+="你好模块A2";
|
| | |
|
| | | }
|
| | |
|
| | | message+="b";
|
| | |
|
| | |
|
| | |
|
| | | // 将消息发布到指定的队列中。空字符串""表示默认的交换机,QUEUE_NAME指定了目标队列名称,null表示没有指定其他属性,message.getBytes()将消息内容转换为字节数组进行传输。
|
| | | channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
|
| | | System.out.println(" [x] Sent '" + message + "'");
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
New file |
| | |
| | | package com.example.springboot.component;
|
| | |
|
| | | import com.rabbitmq.client.*;
|
| | |
|
| | | public class ModuleB {
|
| | | private final static String QUEUE_NAME = "hangzhoumes";
|
| | |
|
| | | public static void main(String[] argv) throws Exception {
|
| | | // 创建连接工厂
|
| | | ConnectionFactory factory = new ConnectionFactory();
|
| | | factory.setHost("localhost");
|
| | | try (Connection connection = factory.newConnection();
|
| | | Channel channel = connection.createChannel()) {
|
| | | // 声明队列
|
| | | channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
| | | // 创建消费者
|
| | | DeliverCallback deliverCallback = (consumerTag, delivery) -> {
|
| | | String receivedMessage = new String(delivery.getBody(), "UTF-8");
|
| | | System.out.println(" [x] Received '" + receivedMessage + "'");
|
| | | };
|
| | | // 开始消费消息
|
| | | channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
|
| | | });
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | package com.example.springboot.component; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | import com.example.springboot.entity.device.PlcParameterObject; |
| | | |
| | | import com.example.springboot.entity.device.PlcBitObject; |
| | | import com.example.springboot.entity.device.PlcParameterObject; |
| | | |
| | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import static com.example.springboot.component.InitUtil.readAndUpdateBitValues; |
| | | import static com.example.springboot.component.InitUtil.readAndUpdateWordValues; |
| | | |
| | | public class PLCAutoMes extends Thread { |
| | | |
| | | // 用于存储应用程序的配置信息 |
| | | private Configuration config; |
| | | private static InitUtil initUtil; |
| | | public static PlcParameterObject plcParameterObject; |
| | | // 创建一个自定义的 S7 控制器消息处理器对象 |
| | | MessageHandler customS7Control = new MessageHandler(); |
| | | ; |
| | | |
| | | |
| | | // 单例实例 |
| | | private static PLCAutoMes instance; |
| | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | |
| | | |
| | | String jsonFilePath = PLCAutoMes.class.getResource("/JsonFile/PlcMes.json").getPath(); |
| | | |
| | | String jsonFilePath2 = PLCAutoMes.class.getResource("/JsonFile/PlcRead.json").getPath(); |
| | | // String jsonFilePath3 = PLCAutoMes.class.getResource("/JsonFile/Plcframe.json").getPath(); |
| | | // System.out.println(jsonFilePath); |
| | | |
| | | String jsonFilePath = PLCAutomaticParameterSettingReview2.class.getResource("/JsonFile/PlcParameter.json").getPath(); |
| | | // String jsonFilePath2 = PLCAutomaticParameterSettingReview2.class.getResource("/JsonFile/PlcSign.json").getPath(); |
| | | //String jsonFilePath4 = PLCAutomaticParameterSettingReview2.class.getResource("/JsonFile/PlcState.json").getPath(); |
| | | //System.out.println(jsonFilePath4); |
| | | // 调用initword方法 |
| | | PlcParameterObject PlcMesObject= initUtil.initword(jsonFilePath); |
| | | // 调用initbit方法 |
| | | PlcParameterObject PlcReadObject= initUtil.initword(jsonFilePath2); |
| | | plcParameterObject = initUtil.initword(jsonFilePath); |
| | | |
| | | // List<String> addresses = new ArrayList<>(); |
| | | // addresses.add("conveyorVelocity(AutoFAST)"); |
| | | // addresses.add("A02ID"); |
| | | // addresses.add("conveyorVelocity(Manual)"); |
| | | // |
| | | // PlcParameterObject paramObject = PLCAutomaticParameterSettingReview2.plcParameterObject; |
| | | // |
| | | // |
| | | // |
| | | // List<String> address = paramObject.getAddressListByCodeId(addresses); |
| | | // System.out.println(address); |
| | | |
| | | |
| | | //PLCAutomaticParameterSettingReview2 plc ; |
| | | |
| | | // String jsonFilePath = PLCAutoMes.class.getResource("/JsonFile/PlcMes.json").getPath(); |
| | | // |
| | | // String jsonFilePath2 = PLCAutoMes.class.getResource("/JsonFile/PlcRead.json").getPath(); |
| | | // // String jsonFilePath3 = PLCAutoMes.class.getResource("/JsonFile/Plcframe.json").getPath(); |
| | | // // System.out.println(jsonFilePath); |
| | | // |
| | | // // 调用initword方法 |
| | | // PlcMesObject= initUtil.initword(jsonFilePath); |
| | | // // 调用initbit方法 |
| | | // PlcParameterObject PlcReadObject= initUtil.initword(jsonFilePath2); |
| | | // // 调用initbit方法 |
| | | // PlcParameterObject PlcframeObject= initUtil.initword(jsonFilePath3); |
| | | readAndUpdateWordValues(PlcReadObject); |
| | | readAndUpdateWordValues(PlcMesObject); |
| | | // readAndUpdateWordValues(PlcframeObject); |
| | | // readAndUpdateWordValues(PlcReadObject); |
| | | // readAndUpdateWordValues(PlcMesObject); |
| | | // readAndUpdateWordValues(PlcframeObject); |
| | | |
| | | |
| | | |
| | | |
| | | // readAndUpdateWordValues(plcStateObject); |
| | | int index = PlcMesObject.getPlcParameter("AddStart").getAddressIndex(); |
| | | //System.out.println(index); |
| | | PlcMesObject.getPlcParameter("AddStart").getAddress(index); |
| | | //System.out.println(PlcMesObject.getPlcParameter("AddStart").getAddress(index)); |
| | | List<String> addresses = new ArrayList<>(); |
| | | addresses.add("FeedID"); |
| | | addresses.add("AddStart"); |
| | | //System.out.println(addresses); |
| | | //System.out.println(PlcMesObject.getPlcParameterValues(addresses)); |
| | | List<String> addresses2 = new ArrayList<>(); |
| | | addresses2.add("FeedID"); |
| | | addresses2.add("FeedCarStatus"); |
| | | // int index = PlcMesObject.getPlcParameter("AddStart").getAddressIndex(); |
| | | // //System.out.println(index); |
| | | // PlcMesObject.getPlcParameter("AddStart").getAddress(index); |
| | | // //System.out.println(PlcMesObject.getPlcParameter("AddStart").getAddress(index)); |
| | | // List<String> addresses = new ArrayList<>(); |
| | | // addresses.add("FeedID"); |
| | | // addresses.add("AddStart"); |
| | | // //System.out.println(addresses); |
| | | // //System.out.println(PlcMesObject.getPlcParameterValues(addresses)); |
| | | // List<String> addresses2 = new ArrayList<>(); |
| | | // addresses2.add("FeedID"); |
| | | // addresses2.add("FeedCarStatus"); |
| | | |
| | | System.out.println(PlcReadObject.getPlcParameterValues(addresses2)); |
| | | // System.out.println(PlcReadObject.getPlcParameterValues(addresses2)); |
| | | |
| | | |
| | | |
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import com.example.springboot.entity.device.PlcBitObject; |
| | | |
| | | |
| | | import com.example.springboot.entity.device.PlcParameterObject; |
| | | |
| | | import java.io.IOException; |
| | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | |
| | | import static com.example.springboot.component.InitUtil.readAndUpdateBitValues; |
| | | import static com.example.springboot.component.InitUtil.readAndUpdateWordValues; |
| | | |
| | | public class PLCAutomaticParameterSettingReview2 extends Thread { |
| | | |
| | | |
| | | private static PlcParameterObject plcParameterObject; |
| | | // 用于存储应用程序的配置信息 |
| | | private Configuration config; |
| | | private static InitUtil initUtil; |
| | | // 创建一个自定义的 S7 控制器消息处理器对象 |
| | | MessageHandler customS7Control = new MessageHandler(); |
| | | |
| | | // public static PlcParameterObject plcStateObject; |
| | | |
| | | // 单例实例 |
| | | private static PLCAutomaticParameterSettingReview2 instance; |
| | |
| | | // 私有构造函数 |
| | | public PLCAutomaticParameterSettingReview2() throws IOException { |
| | | config = new Configuration("config.properties"); |
| | | initUtil = new InitUtil(); |
| | | // initUtil = new InitUtil(); |
| | | } |
| | | |
| | | // 获取单例实例 |
| | |
| | | return instance; |
| | | } |
| | | |
| | | String jsonFilePath = PLCAutomaticParameterSettingReview2.class.getResource("/JsonFile/PlcParameter.json").getPath(); |
| | | String jsonFilePath2 = PLCAutomaticParameterSettingReview2.class.getResource("/JsonFile/PlcSign.json").getPath(); |
| | | String jsonFilePath4 = PLCAutomaticParameterSettingReview2.class.getResource("/JsonFile/PlcState.json").getPath(); |
| | | //System.out.println(jsonFilePath4); |
| | | // 调用initword方法 |
| | | PlcParameterObject plcParameterObject = initUtil.initword(jsonFilePath); |
| | | PlcParameterObject plcStateObject = initUtil.initword(jsonFilePath4); |
| | | |
| | | // 调用initbit方法 |
| | | PlcBitObject plcBitObject = initUtil.initbit(jsonFilePath2); |
| | | |
| | | @Override |
| | | public void run() { |
| | |
| | | } |
| | | |
| | | |
| | | // String jsonFilePath = PLCAutomaticParameterSettingReview2.class.getResource("/JsonFile/PlcParameter.json").getPath(); |
| | | // String jsonFilePath2 = PLCAutomaticParameterSettingReview2.class.getResource("/JsonFile/PlcSign.json").getPath(); |
| | | //String jsonFilePath4 = PLCAutomaticParameterSettingReview2.class.getResource("/JsonFile/PlcState.json").getPath(); |
| | | //System.out.println(jsonFilePath4); |
| | | // 调用initword方法 |
| | | // plcParameterObject = initUtil.initword(jsonFilePath); |
| | | // plcStateObject = initUtil.initword(jsonFilePath4); |
| | | |
| | | // 调用initbit方法 |
| | | // PlcBitObject plcBitObject = initUtil.initbit(jsonFilePath2); |
| | | |
| | | // int index3 = plcParameterObject.getPlcParameter("A01A02conveyorVelocity(Max)").getAddressIndex(); |
| | | // System.out.println(index3); |
| | | // System.out.println(plcParameterObject.getPlcParameter("A01A02conveyorVelocity(Max)").getAddress(index3)); |
| | |
| | | // readAndUpdateWordValues(plcParameterObject); |
| | | // readAndUpdateWordValues(plcStateObject); |
| | | |
| | | List<String> addresses = new ArrayList<>(); |
| | | addresses.add("conveyorVelocity(AutoFAST)"); |
| | | addresses.add("A02ID"); |
| | | // List<String> addresses = new ArrayList<>(); |
| | | // addresses.add("conveyorVelocity(AutoFAST)"); |
| | | // addresses.add("A02ID"); |
| | | // addresses.add("conveyorVelocity(Manual)"); |
| | | List<String> addresses = new ArrayList<>(); |
| | | addresses.add("conveyorVelocity(AutoFAST)"); |
| | | addresses.add("A02ID"); |
| | | addresses.add("conveyorVelocity(Manual)"); |
| | | |
| | | |
| | | List<String> address = plcParameterObject.getAddressListByCodeId(addresses); |
| | | |
| | | |
| | | |
| | | List<String> address = PLCAutomaticParameterSettingReview2.plcParameterObject.getAddressListByCodeId(addresses); |
| | | System.out.println(address); |
| | | |
| | | // List<String> address = plcParameterObject.getAddressListByCodeId(addresses); |
| | | // System.out.println(address); |
| | | |
| | | // int index = plcParameterObject.getPlcParameter("A01turnAngle1").getAddressIndex(); |
| | | // List<Short> arraylist = S7control.getinstance().ReadWord(plcParameterObject.getPlcParameter("A01turnAngle1").getAddress(index), 8); |
| | | |
| | |
| | | import java.io.InputStreamReader; |
| | | import java.io.UnsupportedEncodingException; |
| | | |
| | | import com.example.springboot.common.MessageSender; |
| | | 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 { |
| | | String name = ""; |
| | | Integer count = 0; |
| | | |
| | | public static String readFileToString(String filePath) throws IOException { |
| | | File file = new File(filePath); |
| | | return FileUtils.readFileToString(file, "UTF-8"); |
| | | } |
| | | |
| | | public void readValue() { |
| | | String str = ""; |
| | | BufferedReader bufferedReader = null; |
| | | FileInputStream fileInputStream; |
| | | try { |
| | | // 从文件中读取字节数据存入 fileInputStream |
| | | fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/Parameter.json"); |
| | | // 读取 fileInputStream 中字节并将其解码为字符 |
| | | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); |
| | | // 提高读取效率,在 BufferedReader 内包装 InputStreamReader |
| | | bufferedReader = new BufferedReader(inputStreamReader); |
| | | String line = null; |
| | | // 将 bufferedReader 内容一行一行赋值给str |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | str += line; |
| | | } |
| | | |
| | | // 将str字符串格式转为json |
| | | JSONObject jsonObject = new JSONObject(str); |
| | | |
| | | // 获取json中的值 |
| | | JSONArray address = jsonObject.getJSONArray("address"); |
| | | for (int i = 0; i < address.size(); i++) { |
| | | JSONObject ress = (JSONObject) address.get(i); |
| | | |
| | | this.name = ress.getStr("name"); |
| | | this.count = ress.getInt("count"); |
| | | |
| | | } |
| | | |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | // 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(); |
| | | } |
| | | // this.readValue(); |
| | | // String PlcAddress = this.name; |
| | | // Integer Plccount = this.count; |
| | | |
| | | // System.out.println(stt); |
| | | List<Short> arraylist = S7control.getinstance().ReadWord("DB100.0", 60); |
| | | List<Short> state = S7control.getinstance().ReadWord("DB103.0", 10); |
| | | // Short[] values1 = { 1231, 1, 1, 1, 1, 1, 2, 33, 2, 3, 4, 5 ,1231, 1, 1, 1, 1, 1, 2, 33, 2, 3, 4, 5 }; |
| | | // List<Short> arraylist = new ArrayList<>(Arrays.asList(values1)); |
| | | |
| | | // Short[] values2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| | | // List<Short> state = new ArrayList<>(Arrays.asList(values2)); |
| | | |
| | | // Boolean[] values = { false, true, true, true, true, true, true, true, true, |
| | | // true, true, true, true, true, true, true, true, true, true, true, true, true, |
| | | // true, |
| | | // true, false, true }; |
| | | // List<Boolean> paramlist = new ArrayList<>(Arrays.asList(values)); |
| | | |
| | | // short[] sholist = new short[paramlist.size()]; |
| | | |
| | | // for (int i = 0; i < paramlist.size(); i++) { |
| | | // boolean value = paramlist.get(i); |
| | | // sholist[i] = value ? (short) 1 : (short) 0; |
| | | // } |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | // new short[]{1,1, 1, 1, 1, 1, 2, 33, 2,3, 4, 5} |
| | | // new short[]{0,0, 0, 0, 0, 0, 0, 0, 0, 0} |
| | | jsonObject.put("key1", "value1"); |
| | | jsonObject.put("key2", 123); |
| | | JSONArray jsonArray = new JSONArray(); |
| | | jsonArray.add("item1"); |
| | | jsonArray.add("item2"); |
| | | jsonObject.put("key3", jsonArray); |
| | | |
| | | jsonObject.append("params", arraylist); |
| | | jsonObject.append("state", state); |
| | | // jsonObject.append("action", sholist); |
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | // if (sendwServer != null) { |
| | | // sendwServer.sendMessage(jsonObject.toString()); |
| | | // } |
| | | try { |
| | | // 将 JSON 对象转换为字符串并发送到队列 |
| | | RabbitMQUtils.sendJsonMessage(jsonObject, "json_queue"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | |
| | | // WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | if (webserver != null) { |
| | | List<String> messages = webserver.getMessages(); |
| | | |
| | | String addressList = "DB100.0"; |
| | | |
| | | if (!messages.isEmpty()) { |
| | | // 将最后一个消息转换为 short 类型的列表 |
| | | String lastMessage = messages.get(messages.size() - 1); |
| | | System.out.println("messages:" + messages); |
| | | String[] parts = lastMessage.split(","); |
| | | List<Short> messageValues = new ArrayList<>(); |
| | | for (String part : parts) { |
| | | try { |
| | | // 使用正则表达式清除非数字字符 |
| | | String cleanedPart = part.replaceAll("[^0-9-]", ""); |
| | | short value = Short.parseShort(cleanedPart.trim()); |
| | | messageValues.add(value); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | // 将消息值写入 PLC |
| | | S7control.getinstance().WriteWord(addressList, messageValues); |
| | | System.out.println("messageValues:" + messageValues); |
| | | System.out.println("addressList:" + addressList); |
| | | // 清空消息列表 |
| | | webserver.clearMessages(); |
| | | } |
| | | } |
| | | } |
| | | try { |
| | | // 从队列接收并解析 JSON 消息为 JSONObject 对象 |
| | | JSONObject receivedJsonObject = RabbitMQUtils.receiveJsonMessage(JSONObject.class, "json_queue"); |
| | | System.out.println(receivedJsonObject.toString()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.example.springboot.component;
|
| | |
|
| | | import com.rabbitmq.client.*;
|
| | | import com.fasterxml.jackson.databind.ObjectMapper;
|
| | |
|
| | | public class RabbitMQUtils {
|
| | | // ObjectMapper对象,用于序列化和反序列化JSON
|
| | | private static ObjectMapper objectMapper = new ObjectMapper();
|
| | |
|
| | | // 发送消息到 RabbitMQ 队列中
|
| | | public static void sendMessage(String message, String queueName) throws Exception {
|
| | | // 创建连接工厂并设置主机名
|
| | | ConnectionFactory factory = new ConnectionFactory();
|
| | | factory.setHost("localhost");
|
| | |
|
| | | // 使用 try-with-resources 语句创建连接和通道,并发送消息
|
| | | try (Connection connection = factory.newConnection();
|
| | | Channel channel = connection.createChannel()) {
|
| | | // 声明队列
|
| | | channel.queueDeclare(queueName, false, false, false, null);
|
| | |
|
| | | // 发布消息到队列
|
| | | channel.basicPublish("", queueName, null, message.getBytes());
|
| | | System.out.println(" [x] Sent '" + message + "' to queue '" + queueName + "'");
|
| | | }
|
| | | }
|
| | |
|
| | | // 从 RabbitMQ 队列中接收消息
|
| | | public static void receiveMessage(String queueName) throws Exception {
|
| | | // 创建连接工厂并设置主机名
|
| | | ConnectionFactory factory = new ConnectionFactory();
|
| | | factory.setHost("localhost");
|
| | |
|
| | | // 使用 try-with-resources 语句创建连接和通道,并接收消息
|
| | | try (Connection connection = factory.newConnection();
|
| | | Channel channel = connection.createChannel()) {
|
| | | // 声明队列
|
| | | channel.queueDeclare(queueName, false, false, false, null);
|
| | |
|
| | | // 设置消息接收回调
|
| | | DeliverCallback deliverCallback = (consumerTag, delivery) -> {
|
| | | String receivedMessage = new String(delivery.getBody(), "UTF-8");
|
| | | System.out.println(" [x] Received '" + receivedMessage + "'");
|
| | | };
|
| | |
|
| | | // 消费队列中的消息
|
| | | channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
|
| | | });
|
| | | }
|
| | | }
|
| | |
|
| | | // 发送 JSON 消息到队列
|
| | | public static void sendJsonMessage(Object message, String queueName) throws Exception {
|
| | | // 创建连接工厂并设置主机名
|
| | | ConnectionFactory factory = new ConnectionFactory();
|
| | | factory.setHost("localhost");
|
| | |
|
| | | // 使用 try-with-resources 语句创建连接和通道,并发送 JSON 消息
|
| | | try (Connection connection = factory.newConnection();
|
| | | Channel channel = connection.createChannel()) {
|
| | | // 声明队列
|
| | | channel.queueDeclare(queueName, false, false, false, null);
|
| | |
|
| | | // 将对象转换为 JSON 字符串
|
| | | String jsonMessage = objectMapper.writeValueAsString(message);
|
| | |
|
| | | // 发布 JSON 消息到队列
|
| | | channel.basicPublish("", queueName, null, jsonMessage.getBytes());
|
| | | System.out.println(" [x] Sent JSON message: '" + jsonMessage + "' to queue '" + queueName + "'");
|
| | | }
|
| | | }
|
| | |
|
| | | // 接收 JSON 消息并转换为对象
|
| | | public static <T> T receiveJsonMessage(Class<T> valueType, String queueName) throws Exception {
|
| | | // 创建连接工厂并设置主机名
|
| | | ConnectionFactory factory = new ConnectionFactory();
|
| | | factory.setHost("localhost");
|
| | |
|
| | | // 使用 try-with-resources 语句创建连接和通道,并接收 JSON 消息
|
| | | try (Connection connection = factory.newConnection();
|
| | | Channel channel = connection.createChannel()) {
|
| | | // 声明队列
|
| | | channel.queueDeclare(queueName, false, false, false, null);
|
| | |
|
| | | // 获取队列中的消息
|
| | | GetResponse response = channel.basicGet(queueName, true);
|
| | | if (response != null) {
|
| | | byte[] body = response.getBody();
|
| | | String jsonMessage = new String(body, "UTF-8");
|
| | | System.out.println(" [x] Received JSON message: '" + jsonMessage + "' from queue '" + queueName + "'");
|
| | |
|
| | | // 将 JSON 消息转换为对象
|
| | | return objectMapper.readValue(jsonMessage, valueType);
|
| | | } else {
|
| | | return null;
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | |
|
| | | // RabbitMQUtils.sendMessage("Hello, world!", "module_queue");
|
| | | //RabbitMQUtils.receiveMessage("module_queue");
|
| | | //RabbitMQUtils.sendJsonMessage(someObject, "another_queue");
|
| | | //RabbitMQUtils.receiveJsonMessage(SomeClass.class, "another_queue");
|
| | | // 需要将 someObject 替换为你要发送的对象,并将 SomeClass 替换为你要接收并转换的对象类型。 |
| | |
| | | package com.example.springboot.config; |
| | | |
| | | import com.example.springboot.component.*; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.ApplicationArguments; |
| | | import org.springframework.boot.ApplicationRunner; |
| | | import org.springframework.core.annotation.Order; |
| | |
| | | |
| | | public class AppRunnerConfig implements ApplicationRunner { |
| | | |
| | | private final PlcParameter plcParameter; |
| | | |
| | | public AppRunnerConfig(PlcParameter plcParameter) { |
| | | this.plcParameter = plcParameter; |
| | | } |
| | | @Override |
| | | public void run(ApplicationArguments args) throws Exception { |
| | | // TODO Auto-generated method stub |
| | | // |
| | | System.out.println("启动完成"); |
| | | |
| | | new PlcHold().start(); |
| | | // plcParameter.start(); |
| | | // |
| | | // new PlcHold().start(); |
| | | // |
| | | // new Plchome().start(); |
| | | // new PlcLayout().start(); |
| | | // new Plcalarm().start(); |
| | | // |
| | | // new Plcsign().start(); |
| | | // |
| | | // new Plcstate().start(); |
| | | // new PlcPositioning1().start(); |
| | | // |
| | | // new PlcParameter2().start(); |
| | | // new PLCAutomaticParameterSetting().start(); |
| | | // |
| | | // new PlcManualonePosition().start(); |
| | | // new PlcManualonePosition2().start(); |
| | | // new PlcServoManualone().start(); |
| | | // new PLCManualJog().start(); |
| | | // new Plclog().start(); |
| | | // |
| | | // new PlcInteractionState().start(); |
| | | |
| | | new Plchome().start(); |
| | | new PlcLayout().start(); |
| | | new Plcalarm().start(); |
| | | |
| | | new Plcsign().start(); |
| | | |
| | | new Plcstate().start(); |
| | | new PlcPositioning1().start(); |
| | | |
| | | new PlcParameter2().start(); |
| | | new PLCAutomaticParameterSetting().start(); |
| | | |
| | | new PlcManualonePosition().start(); |
| | | new PlcManualonePosition2().start(); |
| | | new PlcServoManualone().start(); |
| | | new PLCManualJog().start(); |
| | | new Plclog().start(); |
| | | |
| | | new PlcInteractionState().start(); |
| | | //new PlcParameter1Review().start(); |
| | | //new PLCAutoMes().start(); |
| | | //new PLCAutomaticParameterSettingReview2().start(); |
| | | |
| | | |
| | | // new PLCManualJogReview().start(); |
| | | } |
| | | } |
New file |
| | |
| | | package com.example.springboot.controller;
|
| | |
|
| | | import cn.hutool.json.JSONArray;
|
| | | import cn.hutool.json.JSONObject;
|
| | | import com.example.springboot.common.MessageSender;
|
| | | import com.example.springboot.component.RabbitMQUtils;
|
| | | import org.springframework.beans.factory.annotation.Autowired;
|
| | | import org.springframework.web.bind.annotation.GetMapping;
|
| | | import org.springframework.web.bind.annotation.PathVariable;
|
| | | import org.springframework.web.bind.annotation.RestController;
|
| | |
|
| | | import com.example.springboot.common.MessageSender.Person;
|
| | |
|
| | | @RestController
|
| | | public class MessageController {
|
| | |
|
| | | @Autowired
|
| | | private MessageSender messageSender;
|
| | |
|
| | | @GetMapping("/send/{message}")
|
| | | public String sendMessage(@PathVariable("message") String message) {
|
| | | messageSender.sendMessage(message);
|
| | | return "Message sent: " + message;
|
| | | }
|
| | |
|
| | | @GetMapping("/sendPerson/{name}/{age}")
|
| | | public String sendPersonMessage(@PathVariable("name") String name, @PathVariable("age") int age) {
|
| | | Person person = new Person(name, age);
|
| | | messageSender.sendMessage2(person);
|
| | | return "Person message sent: " + person;
|
| | | }
|
| | |
|
| | | @GetMapping("/sendJson")
|
| | | public String sendJsonMessage() {
|
| | | // 创建要发送的对象
|
| | | 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 {
|
| | | // 调用 RabbitMQUtils 发送 JSON 消息的方法
|
| | | RabbitMQUtils.sendJsonMessage(jsonObject, "yourQueueName");
|
| | | return "JSON message sent: " + jsonObject;
|
| | | } catch (Exception e) {
|
| | | return "Failed to send JSON message: " + e.getMessage();
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | }
|
| | |
| | | spring.jackson.date-format=yyyy-MM-dd HH:mm:ss |
| | | spring.jackson.time-zone=GMT+8 |
| | | |
| | | spring.rabbitmq.host=localhost |
| | | spring.rabbitmq.port=5672 |
| | | spring.rabbitmq.username=guest |
| | | spring.rabbitmq.password=guest |
| | | |
| | | |