您好,登錄后才能下訂單哦!
小編給大家分享一下springboot集成rabbitMQ之對象傳輸?shù)氖纠治?,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
在springboot上使用rabbitMQ傳輸字符串和對象,本文所給出的例子是在兩個(gè)不同的項(xiàng)目之間進(jìn)行對象和和字符串的傳輸。
rabbitMQ的依賴(在兩個(gè)項(xiàng)目中一樣的配置):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
pom配置文件(在兩個(gè)項(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)端寫一個(gè)監(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); }
這樣,一個(gè)簡單的字符串的傳輸便寫好了,下面打開剛才定義的mapping: 192.168.1.111:8080/sendss
在消費(fèi)者端的console窗口便會看到打印的消息
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 中另寫一個(gè)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ù)組暫時(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é)果如下:
以上是“springboot集成rabbitMQ之對象傳輸?shù)氖纠治觥边@篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。