天天看點

mysql監聽rabbitmq消息_RabbitMQ 消息發送、消息監聽

文章轉載自:

https://www.cnblogs.com/wangyaobk/articles/7885052.html

本文是基于spring-rabbit中間件來實作消息的發送接受功能

項目搭建采用spring-boot:

pom.xml如下:

1

2

3 org.springframework.boot

4 spring-boot-starter-amqp

5

rabbitMQ配置:

1 rabbitmq.host=

2 rabbitmq.userName=

3 rabbitmq.password=

4 rabbitmq.port=5672

5 rabbitmq.sendQueueName=test

6 rabbitmq.receiverQueueName=test

配置類:

mysql監聽rabbitmq消息_RabbitMQ 消息發送、消息監聽

1 @Configuration

2 @PropertySource("classpath:rabbitmq.properties")

3 public class RabbitMQConfiguration {

4 @Value("${rabbitmq.host}")

5 private String host;

6 @Value("${rabbitmq.userName}")

7 private String userName;

8 @Value("${rabbitmq.password}")

9 private String password;

10 @Value("${rabbitmq.port}")

11 private Integer port;

12 @Value("${rabbitmq.sendQueueName}")

13 private String sendQueueName;

14 @Value("${rabbitmq.receiverQueueName}")

15 private String receiverQueueName;

16

17 //此處省略getter ,setter

}

mysql監聽rabbitmq消息_RabbitMQ 消息發送、消息監聽

Amqp注冊與監聽:

mysql監聽rabbitmq消息_RabbitMQ 消息發送、消息監聽

@Configuration

public class AmqpListener {

@Autowired

private RabbitMQConfiguration rabbitMQConfiguration;

@Bean

public MessageListener exampleListener() {

return new MessageListener() {

public void onMessage(Message message) {

//amqpReceiver.onMessage(message);

System.out.print("接收消息:" + new String(message.getBody()));

}

};

}

@Bean

public SimpleMessageListenerContainer messageListenerContainer() {

SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();

container.setConnectionFactory(rabbitConnectionFactory());

//設定監聽的隊列名,數組[]"abc","test4"

String[] types = {rabbitMQConfiguration.getReceiverQueueName()};

container.setQueueNames(types);

container.setMessageListener(exampleListener());

return container;

}

@Bean

public ConnectionFactory rabbitConnectionFactory() {

CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitMQConfiguration.getHost());

connectionFactory.setUsername(rabbitMQConfiguration.getUserName());

connectionFactory.setPassword(rabbitMQConfiguration.getPassword());

connectionFactory.setPort(rabbitMQConfiguration.getPort());

return connectionFactory;

}

}

mysql監聽rabbitmq消息_RabbitMQ 消息發送、消息監聽

下面介紹簡單的消息發送:

mysql監聽rabbitmq消息_RabbitMQ 消息發送、消息監聽

1

4 public class AmqpSend {

5

6 private static Logger log = LoggerFactory.getLogger(AmqpSend.class);

7

8 private AmqpTemplate rabbitTemplate;

9 private RabbitMQConfiguration rabbitMQConfiguration;

10 private String context;

11

12 public AmqpSend(String context, RabbitMQConfiguration rabbitMQConfiguration, AmqpTemplate rabbitTemplate) {

13 this.context = context;

14 this.rabbitMQConfiguration = rabbitMQConfiguration;

15 this.rabbitTemplate = rabbitTemplate;

16 }

17

18 public void send() {

19 try {

20 this.rabbitTemplate.convertAndSend(rabbitMQConfiguration.getSendQueueName(), context);

21 log.info("消息發送成功");

22 } catch (Exception e) {

23 log.error(e.getMessage());

24 log.error("消息發送失敗");

25 }

26

27 }

28 }

mysql監聽rabbitmq消息_RabbitMQ 消息發送、消息監聽

然後登入rabbitmq web界面:

mysql監聽rabbitmq消息_RabbitMQ 消息發送、消息監聽