admin 1 жил өмнө
parent
commit
be004d6865

+ 53 - 0
jmsSpringBoot/pom.xml

@@ -0,0 +1,53 @@
+<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.example</groupId>
+    <artifactId>jmsSpringBoot</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.2.2.RELEASE</version>
+    </parent>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </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-activemq</artifactId>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 77 - 0
jmsSpringBoot/src/main/java/org/example/ActiveMQConfig.java

@@ -0,0 +1,77 @@
+package org.example;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.activemq.command.ActiveMQQueue;
+import org.apache.activemq.command.ActiveMQTopic;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
+import org.springframework.jms.config.JmsListenerContainerFactory;
+
+import javax.jms.ConnectionFactory;
+import javax.jms.Queue;
+import javax.jms.Topic;
+
+@Configuration
+public class ActiveMQConfig {
+    @Value("${spring.activemq.user}")
+    private String usrName;
+    @Value("${spring.activemq.password}")
+    private  String password;
+    @Value("${spring.activemq.broker-url}")
+    private  String brokerUrl;
+
+    @Value("${queueName}")
+    private String queueName;
+    @Value("${topicName}")
+    private String topicName;
+
+    /**
+     * 队列模式实例
+     *
+     * @return
+     */
+    @Bean
+    public Queue queue() {
+        return new ActiveMQQueue(queueName);
+    }
+
+    /**
+     * 订阅模式实例
+     *
+     * @return
+     */
+    @Bean
+    public Topic topic() {
+        return new ActiveMQTopic(topicName);
+    }
+
+    /**
+     * 配置以下两个bean,同时支持队列模式和广播模式,配置中spring.jms.pub-sub-domain=true将失效
+     * @return
+     */
+    @Bean("connectionFactory")
+    public ActiveMQConnectionFactory connectionFactory() {
+        return new ActiveMQConnectionFactory(usrName, password, brokerUrl);
+    }
+
+    @Bean("queueListenerFactory")
+    public JmsListenerContainerFactory<?> queueListenerFactory(ConnectionFactory connectionFactory) {
+        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
+        factory.setConnectionFactory(connectionFactory);
+        factory.setPubSubDomain(false);
+        return factory;
+    }
+
+    @Bean("topicListenerFactory")
+    public JmsListenerContainerFactory <?> topicListenerFactory(ConnectionFactory connectionFactory) {
+        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
+        factory.setConnectionFactory(connectionFactory);
+        //设置为发布订阅方式, 默认情况下使用的生产消费者方式
+        factory.setPubSubDomain(true);
+        return factory;
+    }
+
+
+}

+ 66 - 0
jmsSpringBoot/src/main/java/org/example/JmsComponent.java

@@ -0,0 +1,66 @@
+package org.example;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jms.annotation.JmsListener;
+import org.springframework.jms.core.JmsMessagingTemplate;
+import org.springframework.stereotype.Component;
+
+import javax.jms.Queue;
+import javax.jms.Topic;
+import java.io.Serializable;
+import java.util.Date;
+
+@Component
+public class JmsComponent {
+
+    @Autowired
+    private JmsMessagingTemplate jmsMessagingTemplate;
+
+    @Autowired
+    private Queue queue;
+    @Autowired
+    private Topic topic;
+
+    /**
+     * 发送消息
+     */
+    public void send(Message message){
+        jmsMessagingTemplate.convertAndSend(this.queue,message.toString());
+    }
+
+    /**
+     * 接收消息
+     */
+    @JmsListener(destination= "${queueName}")    //用这个注解去监听 监听的队列
+    public void receive(String messageString){
+        System.out.println("receive:"+messageString);
+    }
+
+
+    /**
+     * 广播发送消息
+     */
+    public void sendForTopic(Message message){
+        jmsMessagingTemplate.convertAndSend(this.topic,message.toString());
+    }
+
+    /**
+     * 接收消息
+     */
+    @JmsListener(destination= "${topicName}",containerFactory = "queueListenerFactory")
+    public void receiveForTopic1(String messageString){
+        System.out.println("receive1:"+messageString);
+    }
+
+    /**
+     * 接收消息
+     */
+    @JmsListener(destination= "${topicName}",containerFactory = "topicListenerFactory")
+    public void receiveForTopic2(String messageString){
+        System.out.println("receive2:"+messageString);
+    }
+
+}
+
+
+

+ 33 - 0
jmsSpringBoot/src/main/java/org/example/JmsController.java

@@ -0,0 +1,33 @@
+package org.example;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.Date;
+
+@RestController
+public class JmsController {
+    private final Logger logger= LoggerFactory.getLogger(JmsController.class);
+    @Autowired
+    private JmsComponent jmsComponent;
+    @GetMapping("/pushMessage")
+    public void send(){
+        logger.info("pushMessage推送消息");
+        Message message=new Message();
+        message.setContent("推送消息");
+        message.setData(new Date());
+        jmsComponent.send(message);
+    }
+
+    @GetMapping("/pushMessageForTopic")
+    public void pushMessageForTopic(){
+        logger.info("pushMessageForTopic广播消息");
+        Message message=new Message();
+        message.setContent("广播消息");
+        message.setData(new Date());
+        jmsComponent.sendForTopic(message);
+    }
+}

+ 13 - 0
jmsSpringBoot/src/main/java/org/example/Main.java

@@ -0,0 +1,13 @@
+package org.example;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
+// then press Enter. You can now see whitespace characters in your code.
+@SpringBootApplication
+public class Main {
+    public static void main(String[] args){
+        SpringApplication.run(Main.class, args);
+    }
+}

+ 25 - 0
jmsSpringBoot/src/main/java/org/example/Message.java

@@ -0,0 +1,25 @@
+package org.example;
+
+import java.io.Serializable;
+import java.util.Date;
+
+public class Message implements Serializable {
+    private String content;
+    private Date data;
+
+    //set,get方法
+    public void setContent(String content){
+        this.content = content;
+    }
+
+    public void setData(Date data){
+        this.data = data;
+    }
+    @Override
+    public String toString() {
+        return "Message{" +
+                "content='" + content + '\'' +
+                ", data=" + data +
+                '}';
+    }
+}

+ 11 - 0
jmsSpringBoot/src/main/resources/application.properties

@@ -0,0 +1,11 @@
+spring.activemq.broker-url=tcp://127.0.0.1:61616
+##????????????????
+spring.activemq.packages.trust-all=true
+spring.activemq.user=admin
+spring.activemq.password=admin
+queueName=publish.queue
+##topicid
+topicName=publish.topic
+#Spring Boot??ActiveMQ????????????????????spring.jms.pub-sub-domain????
+# true ??????false??????????????????
+spring.jms.pub-sub-domain=true

+ 48 - 0
servletSpringBoot/pom.xml

@@ -0,0 +1,48 @@
+<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.example</groupId>
+    <artifactId>servletSpringBoot</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.2.2.RELEASE</version>
+    </parent>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>