springboot集成rabbitMQ之對象傳輸?shù)姆椒?/h1>
更新時間:2017年12月27日 11:46:22 作者:east123321
這篇文章主要介紹了springboot集成rabbitMQ之對象傳輸?shù)姆椒ǎ【幱X得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
rabbitMQ的安裝方法網(wǎng)上有很多教程,這里就不重復(fù)了。
在springboot上使用rabbitMQ傳輸字符串和對象,本文所給出的例子是在兩個不同的項(xiàng)目之間進(jìn)行對象和和字符串的傳輸。
rabbitMQ的依賴(在兩個項(xiàng)目中一樣的配置):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
pom配置文件(在兩個項(xiàng)目中一樣的配置):
spring.application.name: demo1 //項(xiàng)目名
spring.rabbitmq.host: 192.168.1.111 //寫自己的ip
spring.rabbitmq.port: 5672
spring.rabbitmq.username: guest
spring.rabbitmq.password: guest
spring.rabbitmq.virtual-host: /
spring.rabbitmq.publisher-confirms: true
spring.rabbitmq.publisher-returns: true
spring.rabbitmq.template.mandatory: true
字符轉(zhuǎn)的相互傳輸(本例使用的topic類型)
1>. 首先,在生產(chǎn)者(項(xiàng)目A)中寫配置文件,其中生成隊(duì)列queue,交換機(jī)exchange并且進(jìn)行綁定binding
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author:fdh
* @Description:
* @Date: Create in 16:13 2017/12/22
*/
@Configuration
public class senderConfigration {
/**
*@Description: 新建隊(duì)列 topic.messages
*@Data:16:14 2017/12/22
*/
@Bean(name = "messages")
public Queue queueMessages(){
return new Queue("topic.messages");
}
/**
*@Description: 定義交換器
*@Data:16:15 2017/12/22
*/
@Bean
public TopicExchange exchange(){
return new TopicExchange("exchange");
}
/**
*@Description: 交換機(jī)與消息隊(duì)列進(jìn)行綁定 隊(duì)列messages綁定交換機(jī)with topic.messages
*@Data:16:18 2017/12/22
*/
@Bean
Binding bindingExchangeMessages(@Qualifier("messages") Queue queueMessages,TopicExchange exchange){
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.messages");
}
}
2>. 第二步(項(xiàng)目A),生產(chǎn)者把消息發(fā)送到消息隊(duì)列,
/**
* @Author:fdh
* @Description:
* @Date: Create in 14:15 2017/12/22
*/
@Controller
public class RabbitController {
@Autowired
private AmqpTemplate amqpTemplate;
@RequestMapping("/sendss")
public void send1(){
amqpTemplate.convertAndSend("exchange","topic.messages","hello topic.messages RabbitMQ");
}
}
3>. 接下來,在消費(fèi)者(項(xiàng)目B)端寫一個監(jiān)聽器,交換器會根據(jù)綁定的routing key(topic.messages)把生產(chǎn)者生產(chǎn)的消息放到匹配的消息隊(duì)列中,監(jiān)聽器會監(jiān)聽相應(yīng)的消息隊(duì)列來獲取路由到該消息隊(duì)列上的消息。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
/**
* @ Author:fdh
* @ Description: 消息隊(duì)列監(jiān)聽器
* @ Date: Create in 14:19 2017/12/22
*/
@Component
public class Receiver {
@RabbitListener(queues = "topic.messages")
public void process2(String str1) throws ClassNotFoundException{
System.out.println("messages :"+str1);
System.out.println(Thread.currentThread().getName()+"接收到來自topic.message隊(duì)列的消息: "+str1);
}
這樣,一個簡單的字符串的傳輸便寫好了,下面打開剛才定義的mapping: 192.168.1.111:8080/sendss
在消費(fèi)者端的console窗口便會看到打印的消息

以上就是一個簡單的傳輸字符串的例子了。
2. 下面重點(diǎn)介紹一下消費(fèi)者和生產(chǎn)者之間對象的傳輸。
對象的傳輸,要現(xiàn)在生產(chǎn)者(A)中進(jìn)行序列化,即把對象轉(zhuǎn)化為字節(jié)數(shù)組進(jìn)行傳輸,在消費(fèi)者中,再把轉(zhuǎn)化的字節(jié)數(shù)組反序列化為對象。序列化和反序列化的方法很多,這里采用的是java的Serializable 接口
1>. 在生產(chǎn)者(項(xiàng)目A)和消費(fèi)者(項(xiàng)目B)的項(xiàng)目中創(chuàng)建實(shí)體類。
!注意?。盒陆▽?shí)體類Boy.java 該實(shí)體類在項(xiàng)目A、B中的位置,必須一致,即包名必須一致,在本項(xiàng)目中,Boy.java 在項(xiàng)目A、B中都是: import com.fengdonghao.shiro.bean.Boy;
實(shí)體類也要一致。
package com.fengdonghao.shiro.bean;
import javax.persistence.*;
import java.io.Serializable;
/**
* @Author:fdh
* @Description:
* @Date:Create in11:14 2017/12/16
*/
@Entity
public class Boy implements Serializable{
private static final long serialVersionUID=1L;
@Id
@GeneratedValue
private int id;
private String name;
private int age;
@Override
public String toString() {
return "Boy{" +
"age=" + age +
", id=" + id +
", name='" + name + '\'' +
'}';
}
//此處省略getter 和setter 方法
}
2>. 在生產(chǎn)者(A)中配置 消息隊(duì)列,交換器,并進(jìn)行綁定binding,和在 例子1中的第一步是一樣的
3>. 在生產(chǎn)者(A)中的RabbitController.java 中另寫一個mapping,如下
@RequestMapping("/send")
public void sendMessage() {
Boy boy= new Boy();
boy.setName("tim");
boy.setAge(11);
System.out.println(boy);
//以下是序列化操作
//Write Obj to File
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(new File("E:\\WebPackage\\a.txt")));//把序列化之后的字節(jié)數(shù)組暫時存放在該目錄下
oos.writeObject(boy);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(oos);
}
rabbitMQService.send("對象已序列化");
4>. 在消費(fèi)者(B)中對字節(jié)數(shù)組進(jìn)行反序列化。
在Receiver中,重新編寫例1重點(diǎn)的監(jiān)聽器
@RabbitListener(queues = "topic.messages")
public void process2(String str1) {
System.out.println(Thread.currentThread().getName()+"接收到來自topic.message隊(duì)列的消息: "+str1+" 并進(jìn)行反序列化");
File file = new File("E:\\WebPackage\\a.txt");//消費(fèi)者和生產(chǎn)者中路徑要保持一致,才能讀取文件,進(jìn)行解析
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(file));
Boy newUser = (Boy) ois.readObject();
System.out.println("反序列之后:"+newUser);
System.out.println("反序列之后getname:"+newUser.getName());
System.out.println("反序列之后getAge"+newUser.getAge());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(ois);
try {
FileUtils.forceDelete(file);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("messages :"+str1);
}
驗(yàn)證mapping: ip:8080/send
結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- RabbitMQ 3.9.7 鏡像模式集群與Springboot 2.5.5 整合
- SpringAMQP消息隊(duì)列(SpringBoot集成RabbitMQ方式)
- 一文掌握Springboot集成RabbitMQ的方法
- springboot2.5.6集成RabbitMq實(shí)現(xiàn)Topic主題模式(推薦)
- Springboot集成RabbitMQ死信隊(duì)列的實(shí)現(xiàn)
- SpringBoot集成RabbitMQ的方法(死信隊(duì)列)
- springboot2.0集成rabbitmq的示例代碼
- Spring Boot系列教程之7步集成RabbitMQ的方法
- spring boot集成rabbitmq的實(shí)例教程
- 詳解spring boot集成RabbitMQ
- Spring Boot 3 集成 RabbitMQ 實(shí)踐指南(原理解析)
相關(guān)文章
-
Java中的Semaphore信號量簡單使用代碼實(shí)例
這篇文章主要介紹了Java中的Semaphore信號量簡單使用代碼實(shí)例,Semaphore是用來保護(hù)一個或者多個共享資源的訪問,Semaphore內(nèi)部維護(hù)了一個計(jì)數(shù)器,其值為可以訪問的共享資源的個數(shù),一個線程要訪問共享資源,需要的朋友可以參考下 2023-12-12
-
Java中獲取?List中最后一個元素3種方法以及實(shí)際應(yīng)用
這篇文章主要給大家介紹了關(guān)于Java中獲取?List中最后一個元素3種方法以及實(shí)際應(yīng)用的相關(guān)資料,由于List的索引是從0開始的,所以最后一個元素的索引是List的大小減1,需要的朋友可以參考下 2023-11-11
-
java實(shí)現(xiàn)簡單的圖書管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單的圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
2021-07-07
-
datatables 帶查詢條件java服務(wù)端分頁處理實(shí)例
本篇文章主要介紹了datatables 帶查詢條件java服務(wù)端分頁處理實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
2017-06-06
最新評論
rabbitMQ的安裝方法網(wǎng)上有很多教程,這里就不重復(fù)了。
在springboot上使用rabbitMQ傳輸字符串和對象,本文所給出的例子是在兩個不同的項(xiàng)目之間進(jìn)行對象和和字符串的傳輸。
rabbitMQ的依賴(在兩個項(xiàng)目中一樣的配置):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
pom配置文件(在兩個項(xiàng)目中一樣的配置):
spring.application.name: demo1 //項(xiàng)目名 spring.rabbitmq.host: 192.168.1.111 //寫自己的ip spring.rabbitmq.port: 5672 spring.rabbitmq.username: guest spring.rabbitmq.password: guest spring.rabbitmq.virtual-host: / spring.rabbitmq.publisher-confirms: true spring.rabbitmq.publisher-returns: true spring.rabbitmq.template.mandatory: true
字符轉(zhuǎn)的相互傳輸(本例使用的topic類型)
1>. 首先,在生產(chǎn)者(項(xiàng)目A)中寫配置文件,其中生成隊(duì)列queue,交換機(jī)exchange并且進(jìn)行綁定binding
import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Author:fdh * @Description: * @Date: Create in 16:13 2017/12/22 */ @Configuration public class senderConfigration { /** *@Description: 新建隊(duì)列 topic.messages *@Data:16:14 2017/12/22 */ @Bean(name = "messages") public Queue queueMessages(){ return new Queue("topic.messages"); } /** *@Description: 定義交換器 *@Data:16:15 2017/12/22 */ @Bean public TopicExchange exchange(){ return new TopicExchange("exchange"); } /** *@Description: 交換機(jī)與消息隊(duì)列進(jìn)行綁定 隊(duì)列messages綁定交換機(jī)with topic.messages *@Data:16:18 2017/12/22 */ @Bean Binding bindingExchangeMessages(@Qualifier("messages") Queue queueMessages,TopicExchange exchange){ return BindingBuilder.bind(queueMessages).to(exchange).with("topic.messages"); } }
2>. 第二步(項(xiàng)目A),生產(chǎn)者把消息發(fā)送到消息隊(duì)列,
/** * @Author:fdh * @Description: * @Date: Create in 14:15 2017/12/22 */ @Controller public class RabbitController { @Autowired private AmqpTemplate amqpTemplate; @RequestMapping("/sendss") public void send1(){ amqpTemplate.convertAndSend("exchange","topic.messages","hello topic.messages RabbitMQ"); } }
3>. 接下來,在消費(fèi)者(項(xiàng)目B)端寫一個監(jiān)聽器,交換器會根據(jù)綁定的routing key(topic.messages)把生產(chǎn)者生產(chǎn)的消息放到匹配的消息隊(duì)列中,監(jiān)聽器會監(jiān)聽相應(yīng)的消息隊(duì)列來獲取路由到該消息隊(duì)列上的消息。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.amqp.rabbit.annotation.RabbitListener; /** * @ Author:fdh * @ Description: 消息隊(duì)列監(jiān)聽器 * @ Date: Create in 14:19 2017/12/22 */ @Component public class Receiver { @RabbitListener(queues = "topic.messages") public void process2(String str1) throws ClassNotFoundException{ System.out.println("messages :"+str1); System.out.println(Thread.currentThread().getName()+"接收到來自topic.message隊(duì)列的消息: "+str1); }
這樣,一個簡單的字符串的傳輸便寫好了,下面打開剛才定義的mapping: 192.168.1.111:8080/sendss
在消費(fèi)者端的console窗口便會看到打印的消息
以上就是一個簡單的傳輸字符串的例子了。
2. 下面重點(diǎn)介紹一下消費(fèi)者和生產(chǎn)者之間對象的傳輸。
對象的傳輸,要現(xiàn)在生產(chǎn)者(A)中進(jìn)行序列化,即把對象轉(zhuǎn)化為字節(jié)數(shù)組進(jìn)行傳輸,在消費(fèi)者中,再把轉(zhuǎn)化的字節(jié)數(shù)組反序列化為對象。序列化和反序列化的方法很多,這里采用的是java的Serializable 接口
1>. 在生產(chǎn)者(項(xiàng)目A)和消費(fèi)者(項(xiàng)目B)的項(xiàng)目中創(chuàng)建實(shí)體類。
!注意?。盒陆▽?shí)體類Boy.java 該實(shí)體類在項(xiàng)目A、B中的位置,必須一致,即包名必須一致,在本項(xiàng)目中,Boy.java 在項(xiàng)目A、B中都是: import com.fengdonghao.shiro.bean.Boy;
實(shí)體類也要一致。
package com.fengdonghao.shiro.bean; import javax.persistence.*; import java.io.Serializable; /** * @Author:fdh * @Description: * @Date:Create in11:14 2017/12/16 */ @Entity public class Boy implements Serializable{ private static final long serialVersionUID=1L; @Id @GeneratedValue private int id; private String name; private int age; @Override public String toString() { return "Boy{" + "age=" + age + ", id=" + id + ", name='" + name + '\'' + '}'; } //此處省略getter 和setter 方法 }
2>. 在生產(chǎn)者(A)中配置 消息隊(duì)列,交換器,并進(jìn)行綁定binding,和在 例子1中的第一步是一樣的
3>. 在生產(chǎn)者(A)中的RabbitController.java 中另寫一個mapping,如下
@RequestMapping("/send") public void sendMessage() { Boy boy= new Boy(); boy.setName("tim"); boy.setAge(11); System.out.println(boy); //以下是序列化操作 //Write Obj to File ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream(new File("E:\\WebPackage\\a.txt")));//把序列化之后的字節(jié)數(shù)組暫時存放在該目錄下 oos.writeObject(boy); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(oos); } rabbitMQService.send("對象已序列化");
4>. 在消費(fèi)者(B)中對字節(jié)數(shù)組進(jìn)行反序列化。
在Receiver中,重新編寫例1重點(diǎn)的監(jiān)聽器
@RabbitListener(queues = "topic.messages") public void process2(String str1) { System.out.println(Thread.currentThread().getName()+"接收到來自topic.message隊(duì)列的消息: "+str1+" 并進(jìn)行反序列化"); File file = new File("E:\\WebPackage\\a.txt");//消費(fèi)者和生產(chǎn)者中路徑要保持一致,才能讀取文件,進(jìn)行解析 ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream(file)); Boy newUser = (Boy) ois.readObject(); System.out.println("反序列之后:"+newUser); System.out.println("反序列之后getname:"+newUser.getName()); System.out.println("反序列之后getAge"+newUser.getAge()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(ois); try { FileUtils.forceDelete(file); } catch (IOException e) { e.printStackTrace(); } } System.out.println("messages :"+str1); }
驗(yàn)證mapping: ip:8080/send
結(jié)果如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- RabbitMQ 3.9.7 鏡像模式集群與Springboot 2.5.5 整合
- SpringAMQP消息隊(duì)列(SpringBoot集成RabbitMQ方式)
- 一文掌握Springboot集成RabbitMQ的方法
- springboot2.5.6集成RabbitMq實(shí)現(xiàn)Topic主題模式(推薦)
- Springboot集成RabbitMQ死信隊(duì)列的實(shí)現(xiàn)
- SpringBoot集成RabbitMQ的方法(死信隊(duì)列)
- springboot2.0集成rabbitmq的示例代碼
- Spring Boot系列教程之7步集成RabbitMQ的方法
- spring boot集成rabbitmq的實(shí)例教程
- 詳解spring boot集成RabbitMQ
- Spring Boot 3 集成 RabbitMQ 實(shí)踐指南(原理解析)
相關(guān)文章
Java中的Semaphore信號量簡單使用代碼實(shí)例
這篇文章主要介紹了Java中的Semaphore信號量簡單使用代碼實(shí)例,Semaphore是用來保護(hù)一個或者多個共享資源的訪問,Semaphore內(nèi)部維護(hù)了一個計(jì)數(shù)器,其值為可以訪問的共享資源的個數(shù),一個線程要訪問共享資源,需要的朋友可以參考下2023-12-12Java中獲取?List中最后一個元素3種方法以及實(shí)際應(yīng)用
這篇文章主要給大家介紹了關(guān)于Java中獲取?List中最后一個元素3種方法以及實(shí)際應(yīng)用的相關(guān)資料,由于List的索引是從0開始的,所以最后一個元素的索引是List的大小減1,需要的朋友可以參考下2023-11-11java實(shí)現(xiàn)簡單的圖書管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單的圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07datatables 帶查詢條件java服務(wù)端分頁處理實(shí)例
本篇文章主要介紹了datatables 帶查詢條件java服務(wù)端分頁處理實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06