国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

springboot集成rabbitMQ之對象傳輸的方法

2024-07-13 10:15:34
字體:
來源:轉載
供稿:網友

rabbitMQ的安裝方法網上有很多教程,這里就不重復了。

在springboot上使用rabbitMQ傳輸字符串和對象,本文所給出的例子是在兩個不同的項目之間進行對象和和字符串的傳輸。

rabbitMQ的依賴(在兩個項目中一樣的配置):

    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-amqp</artifactId>    </dependency>

pom配置文件(在兩個項目中一樣的配置):

spring.application.name: demo1  //項目名spring.rabbitmq.host: 192.168.1.111 //寫自己的ipspring.rabbitmq.port: 5672spring.rabbitmq.username: guestspring.rabbitmq.password: guestspring.rabbitmq.virtual-host: /spring.rabbitmq.publisher-confirms: truespring.rabbitmq.publisher-returns: truespring.rabbitmq.template.mandatory: true

字符轉的相互傳輸(本例使用的topic類型)

1>. 首先,在生產者(項目A)中寫配置文件,其中生成隊列queue,交換機exchange并且進行綁定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 */@Configurationpublic class senderConfigration {  /**  *@Description: 新建隊列 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: 交換機與消息隊列進行綁定 隊列messages綁定交換機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>. 第二步(項目A),生產者把消息發送到消息隊列,

/** * @Author:fdh * @Description: * @Date: Create in 14:15 2017/12/22 */@Controllerpublic class RabbitController {  @Autowired  private AmqpTemplate amqpTemplate;  @RequestMapping("/sendss")  public void send1(){    amqpTemplate.convertAndSend("exchange","topic.messages","hello topic.messages RabbitMQ");  }}

3>. 接下來,在消費者(項目B)端寫一個監聽器,交換器會根據綁定的routing key(topic.messages)把生產者生產的消息放到匹配的消息隊列中,監聽器會監聽相應的消息隊列來獲取路由到該消息隊列上的消息。

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.amqp.rabbit.annotation.RabbitListener;/** * @ Author:fdh * @ Description: 消息隊列監聽器 * @ Date: Create in 14:19 2017/12/22 */@Componentpublic 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隊列的消息: "+str1);   }

這樣,一個簡單的字符串的傳輸便寫好了,下面打開剛才定義的mapping: 192.168.1.111:8080/sendss

在消費者端的console窗口便會看到打印的消息

springboot,rabbitMQ,對象傳輸,spring,boot,傳輸對象,springboot集成rabbitMQ

以上就是一個簡單的傳輸字符串的例子了。

2. 下面重點介紹一下消費者和生產者之間對象的傳輸。

對象的傳輸,要現在生產者(A)中進行序列化,即把對象轉化為字節數組進行傳輸,在消費者中,再把轉化的字節數組反序列化為對象。序列化和反序列化的方法很多,這里采用的是java的Serializable 接口

1>. 在生產者(項目A)和消費者(項目B)的項目中創建實體類。

!注意!:新建實體類Boy.java 該實體類在項目A、B中的位置,必須一致,即包名必須一致,在本項目中,Boy.java 在項目A、B中都是: import com.fengdonghao.shiro.bean.Boy;

實體類也要一致。

package com.fengdonghao.shiro.bean;import javax.persistence.*;import java.io.Serializable;/** * @Author:fdh * @Description: * @Date:Create in11:14 2017/12/16 */@Entitypublic 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>. 在生產者(A)中配置 消息隊列,交換器,并進行綁定binding,和在 例子1中的第一步是一樣的

3>. 在生產者(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")));//把序列化之后的字節數組暫時存放在該目錄下      oos.writeObject(boy);    } catch (IOException e) {      e.printStackTrace();    } finally {      IOUtils.closeQuietly(oos);    }    rabbitMQService.send("對象已序列化");

4>. 在消費者(B)中對字節數組進行反序列化。

在Receiver中,重新編寫例1重點的監聽器

@RabbitListener(queues = "topic.messages")  public void process2(String str1) {    System.out.println(Thread.currentThread().getName()+"接收到來自topic.message隊列的消息: "+str1+" 并進行反序列化");    File file = new File("E://WebPackage//a.txt");//消費者和生產者中路徑要保持一致,才能讀取文件,進行解析    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);  }

驗證mapping: ip:8080/send

結果如下:

springboot,rabbitMQ,對象傳輸,spring,boot,傳輸對象,springboot集成rabbitMQ

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新竹市| 锡林浩特市| 璧山县| 阿拉善盟| 伊川县| 湖州市| 墨竹工卡县| 靖安县| 涿鹿县| 长阳| 通许县| 绥中县| 壤塘县| 屏东市| 金寨县| 子洲县| 淳化县| 叶城县| 班玛县| 元江| 大港区| 许昌县| 本溪市| 安泽县| 山丹县| 辉县市| 新绛县| 乌海市| 丰县| 阿合奇县| 九寨沟县| 华容县| 类乌齐县| 阿拉尔市| 灵寿县| 麻栗坡县| 观塘区| 佛山市| 城市| 蓬莱市| 昆山市|