新增jmsdemo

This commit is contained in:
fxb 2018-07-31 21:46:57 +08:00
parent 05a2b1d40a
commit 190c8ef703
8 changed files with 194 additions and 1 deletions

2
.gitignore vendored
View File

@ -34,7 +34,7 @@ hs_err_pid*
.sts4-cache
### IntelliJ IDEA ###
.idea
*.idea
*.iws
*.iml
*.ipr

54
jms_demo/pom.xml Normal file
View File

@ -0,0 +1,54 @@
<?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>com.fxb</groupId>
<artifactId>jms_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jms_demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<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.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,12 @@
package com.fxb.jms_demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JmsDemoApplication {
public static void main(String[] args) {
SpringApplication.run(JmsDemoApplication.class, args);
}
}

View File

@ -0,0 +1,38 @@
package com.fxb.jms_demo.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* Created with IntelliJ IDEA.
* Description: 模拟消息队列接受者
* User: ${fxb}
* Email: fanxb.tl@gmail.com
* Date: 2018-07-20
*/
@Component
public class Comsumer {
//接受消息队列1消息
@JmsListener(destination = "active.queue1")
public void readActiveQueue11(String message){
System.out.println(1+message);
}
//接受消息队列1消息
@JmsListener(destination = "active.queue1")
public void readActiveQueue12(String message){
System.out.println(2+message);
}
//接受消息队列2消息
@JmsListener(destination = "active.queue2")
public void readActiveQueue21(String message){
System.out.println(1+message);
}
//接受消息队列2消息
@JmsListener(destination = "active.queue2")
public void readActiveQueue22(String message){
System.out.println(2+message);
}
}

View File

@ -0,0 +1,27 @@
package com.fxb.jms_demo.jmsConfig;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
/**
* Created with IntelliJ IDEA.
* Description:
* User: ${fxb}
* Email: fanxb.tl@gmail.com
* Date: 2018-07-20
*/
@Configuration
public class Config {
@Bean(name = "queue2")
public Queue queue2(){
return new ActiveMQQueue("active.queue2");
}
@Bean(name = "queue1")
public Queue queue1(){
return new ActiveMQQueue("active.queue1");
}
}

View File

@ -0,0 +1,40 @@
package com.fxb.jms_demo.publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Queue;
/**
* Created with IntelliJ IDEA.
* Description:
* User: ${fxb}
* Email: fanxb.tl@gmail.com
* Date: 2018-07-20
*/
@RestController
public class Producer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired()
@Qualifier("queue2")
private Queue queue2;
@Autowired()
@Qualifier("queue1")
private Queue queue1;
@GetMapping("/queue2")
public void sendMessage1(String message){
jmsMessagingTemplate.convertAndSend(queue2,"I'm from queue2:"+message);
}
@GetMapping("/queue1")
public void sendMessage2(String message){
jmsMessagingTemplate.convertAndSend(queue1,"I'm from queue1:"+message);
}
}

View File

@ -0,0 +1,6 @@
spring:
activemq:
broker-url: tcp://localhost:61616
in-memory: true
pool:
enabled: false

View File

@ -0,0 +1,16 @@
package com.fxb.jms_demo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class JmsDemoApplicationTests {
@Test
public void contextLoads() {
}
}