redis主從哨兵和叢集的區別
阿新 • • 發佈:2021-12-27
生產者
專案結構
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>boot_mq_producer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>boot_mq_producer</name> <description>boot_mq_producer</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
ConfigBean.java
package com.example.boot_mq_producer.config; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.jms.annotation.EnableJms; import org.springframework.stereotype.Component; @Component // 讓spring 管理的註解,相當於spring 中在xml 中寫了個bean @EnableJms // 開啟jms 適配 public class ConfigBean { @Value("${myqueue}") private String myQueue ; // 注入配置檔案中的 myqueue @Bean // bean id="" class="…" public ActiveMQQueue queue(){ return new ActiveMQQueue(myQueue); } }
Queue_Produce.java
package com.example.boot_mq_producer.producer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.jms.Queue; import java.util.UUID; @Component public class Queue_Produce { @Autowired private JmsMessagingTemplate jmsMessagingTemplate ; @Autowired private Queue queue ; // 呼叫一次一個資訊發出 public void produceMessage(){ jmsMessagingTemplate.convertAndSend(queue,"****"+ UUID.randomUUID().toString().substring(0,6)); } // 間隔3s定投,下面的註解需要在主啟動類新增 @EnableScheduling @Scheduled(fixedDelay = 3000) // 每3秒自動呼叫 public void produceMessageScheduled(){ jmsMessagingTemplate.convertAndSend(queue,"** 定投訊息 **"+ UUID.randomUUID().toString().substring(0,6)); System.out.println(" produceMessage send ok "); } }
BootMqProducerApplication.java
package com.example.boot_mq_producer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class BootMqProducerApplication { public static void main(String[] args) { SpringApplication.run(BootMqProducerApplication.class, args); } }
觸發式的呼叫:
BootMqProducerApplicationTests.java
package com.example.boot_mq_producer; import com.example.boot_mq_producer.producer.Queue_Produce; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class BootMqProducerApplicationTests { @Autowired private Queue_Produce queue_produce; @Test void contextLoads() { } // 觸發式呼叫 @Test void send() { queue_produce.produceMessage(); } }
定時呼叫,啟動主啟動類,需要加上@EnableScheduling 註解
2022年1月2日17:58:58略