激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - springboot集成rabbitMQ之對象傳輸的方法

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

2021-03-12 14:15east123321 Java教程

這篇文章主要介紹了springboot集成rabbitMQ之對象傳輸的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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

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

?
1
2
3
4
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-amqp</artifactid>
</dependency>

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

?
1
2
3
4
5
6
7
8
9
spring.application.name: demo1  //項目名
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

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

1>. 首先,在生產者(項目a)中寫配置文件,其中生成隊列queue,交換機exchange并且進行綁定binding

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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: 新建隊列 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),生產者把消息發送到消息隊列,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * @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>. 接下來,在消費者(項目b)端寫一個監聽器,交換器會根據綁定的routing key(topic.messages)把生產者生產的消息放到匹配的消息隊列中,監聽器會監聽相應的消息隊列來獲取路由到該消息隊列上的消息。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
 */
@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隊列的消息: "+str1);
  }

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

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

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

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

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

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

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

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

實體類也要一致。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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>. 在生產者(a)中配置 消息隊列,交換器,并進行綁定binding,和在 例子1中的第一步是一樣的

3>. 在生產者(a)中的rabbitcontroller.java 中另寫一個mapping,如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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重點的監聽器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@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之對象傳輸的方法

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/east123321/article/details/78900791

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美aⅴ视频 | 色综合久久久久久久久久久 | 国产一级免费不卡 | 一区二区三区日韩在线 | 成人免费毛片在线观看 | 一级黄色在线观看 | 中文字幕xxx | 广西一级毛片 | 一级成人在线 | 中文字幕精品一二三四五六七八 | 成人免费观看49www在线观看 | 国产精品免费久久久久久 | 精品国产一区二区三区四 | 粉嫩蜜桃麻豆免费大片 | 国产精品成人免费一区久久羞羞 | 欧美一级黄色影院 | 91精品国产九九九久久久亚洲 | 久久精品成人影院 | 久久国产在线观看 | 久久久久久久久久久一区 | 午夜影视一区二区 | 美女网站色免费 | 国产亚洲精品久久久久婷婷瑜伽 | 毛片免费在线观看视频 | 一级做人爱c黑人影片 | 91久久夜色精品国产网站 | 欧美一级特级 | 免费日本一区二区 | 天天天干夜夜夜操 | 成人在线观看地址 | 久久久精品精品 | 一区二区三区欧美在线 | www.国产免费 | www.成人免费视频 | 日韩不卡一区二区 | 精品一区久久久 | 久久精品视频3 | 精品一区二区三区在线播放 | 一级黄色片武则天 | 欧美一级高潮 | 欧美黄色一级片视频 |