一、点对点 1、提供者目录展示
2、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring boot web支持:mvc,aop... -->
<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>
4、生产者MyProvider,通过JMSTemplate模板发送消息
package com.zn.p2p;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import Javax.annotation.Resource;
/**
* 生产者,通过JMSTemplate模板发送消息
*/
@Component
public class MyProvider {
//注入JMSTemplate模板
@Resource
private JmsTemplate jmsTemplate;
//创建方法
public void sendMessage(){
//点对点,创建队列
ActiveMQQueue queue=new ActiveMQQueue("SpringBoot_Queue");
//发送消息
jmsTemplate.convertAndSend(queue,"生产者产生的消息!");
}
}
5、客户端访问ProviderController
package com.zn.p2p;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 客户端访问的方法
*/
@RestController
public class ProvideController {
@Resource
private MyProvider provider;
@RequestMapping("/sendMessage")
public String sendMessage(){
provider.sendMessage();
return "sucess!!";
}
}
6、provider启动类StartProvider
package com.zn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StartProvider {
public static void main(String[] args) {
SpringApplication.run(StartProvider.class,args);
}
}
8、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring boot web支持:mvc,aop... -->
<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>
10、consumer启动类StartP2PConsumer
package com.zn;