好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springboot实现rabbitmq的队列初始化和绑定

配置文件,在rabbit中自动建立exchange,queue和绑定它们的关系

代码里初始化exchange 代码里初始化queue 代码里绑定exchange,queue和routekey 配置文件,直接声明vhost

代码里初始化exchange

?

1

2

3

4

5

6

7

8

9

/**

   * rabbitmq里初始化exchange.

   *

   * @return

   */

  @bean

  public topicexchange crmexchange() {

   return new topicexchange(exchange);

  }

代码里初始化queue

?

1

2

3

4

5

6

7

8

9

/**

   * rabbitmq里初始化队列crm.hello.

   *

   * @return

   */

  @bean

  public queue helloqueue() {

   return new queue(hello);

  }

代码里绑定exchange,queue和routekey

?

1

2

3

4

5

6

7

8

9

10

11

/**

   * 绑定exchange & queue & routekey.

   *

   * @param queuemessage 队列

   * @param exchange   交换机

   * @param routekey   路由

   * @return

   */

  public binding bindingexchange(queue queuemessage, topicexchange exchange, string routekey) {

   return bindingbuilder.bind(queuemessage).to(exchange).with(routekey);

  }

配置文件

?

1

2

3

4

5

6

7

spring:

   rabbitmq:

   host: localhost

   port: 5672

   username: guest

   password: guest

   virtual-host: lind

完整代码

?

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

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

package com.lind.microservice.productcenter.mq;

 

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.context.annotation.bean;

import org.springframework.context.annotation.configuration;

 

/**

  * amqp配置.

  */

@configuration

public class amqpconfig {

 

  /**

   * 交换机.

   */

  public final static string exchange = "crm" ;

  /**

   * hello队列.

   */

  public final static string hello = "crm.hello" ;

  /**

   * 建立订单队列.

   */

  public final static string lind_generate_order = "crm.generate.order" ;

 

 

  /**

   * 绑定exchange & queue & routekey.

   *

   * @param queuemessage 队列

   * @param exchange   交换机

   * @param routekey   路由

   * @return

   */

  public binding bindingexchange(queue queuemessage, topicexchange exchange, string routekey) {

   return bindingbuilder.bind(queuemessage).to(exchange).with(routekey);

  }

 

 

  /**

   * rabbitmq里初始化exchange.

   *

   * @return

   */

  @bean

  public topicexchange crmexchange() {

   return new topicexchange(exchange);

  }

 

  /**

   * rabbitmq里初始化队列crm.hello.

   *

   * @return

   */

  @bean

  public queue helloqueue() {

   return new queue(hello);

  }

 

  /**

   * rabbitmq里初始化队列crm.generate.order.

   *

   * @return

   */

  @bean

  public queue orderqueue() {

   return new queue(lind_generate_order);

  }

}

队列发布者

?

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

package com.lind.microservice.productcenter.mq;

 

import java.util.date;

import org.springframework.amqp.core.amqptemplate;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.context.annotation.configuration;

 

@configuration

public class hellopublisher {

  @autowired

  amqptemplate rabbittemplate;

  @autowired

  amqpconfig amqpconfig;

 

  public void hello() {

   string context = "hello " + new date();

   system.out.println( "hellopublisher : " + context);

   amqpconfig.bindingexchange(

     amqpconfig.helloqueue(),

     amqpconfig.crmexchange(),

     "crm.hello.#"

   );

   this .rabbittemplate.convertandsend(amqpconfig.exchange, amqpconfig.hello, context);

  }

}

队列订阅者

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package com.lind.microservice.productcenter.mq;

 

import org.springframework.amqp.rabbit.annotation.rabbithandler;

import org.springframework.amqp.rabbit.annotation.rabbitlistener;

import org.springframework.stereotype.component;

 

@component

@rabbitlistener (queues = amqpconfig.hello)

public class hellosubscriber {

  @rabbithandler

  public void process(string hello) {

   system.out.println( "hellosubscriber : " + hello);

  }

}

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:http://www.cnblogs.com/lori/p/9739130.html

查看更多关于springboot实现rabbitmq的队列初始化和绑定的详细内容...

  阅读:10次