RabbitMQ的使用(五)RabbitMQ Java Client简单生产者、消费者代码示例

 

pom文件:

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.rabbitmq</groupId>
  4. <artifactId>amqp-client</artifactId>
  5. <version>5.0.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.amqp</groupId>
  9. <artifactId>spring-rabbit</artifactId>
  10. <version>2.0.2.RELEASE</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-web</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-amqp</artifactId>
  19. </dependency>
  20. </dependencies>

连接工具类:

  1. package top.wj.rabbitmq.client.utils;
  2. import com.rabbitmq.client.Channel;
  3. import com.rabbitmq.client.Connection;
  4. import com.rabbitmq.client.ConnectionFactory;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. public class ChannelUtils {
  8. public static Channel getChannelInstance(String connectionDescription) {
  9. try {
  10. ConnectionFactory connectionFactory = getConnectionFactory();
  11. Connection connection = connectionFactory.newConnection(connectionDescription);
  12. return connection.createChannel();
  13. } catch (Exception e) {
  14. throw new RuntimeException("获取Channel连接失败");
  15. }
  16. }
  17. private static ConnectionFactory getConnectionFactory() {
  18. ConnectionFactory connectionFactory = new ConnectionFactory();
  19. // 配置连接信息
  20. connectionFactory.setHost( "127.0.0.1");
  21. connectionFactory.setPort( 5672);
  22. connectionFactory.setVirtualHost( "/");
  23. connectionFactory.setUsername( "guest");
  24. connectionFactory.setPassword( "guest");
  25. // 网络异常自动连接恢复
  26. connectionFactory.setAutomaticRecoveryEnabled( true);
  27. // 每10秒尝试重试连接一次
  28. connectionFactory.setNetworkRecoveryInterval( 10000);
  29. return connectionFactory;
  30. }
  31. }

创建生产者:

  1. package top.wj.rabbitmq.client.producer;
  2. import com.rabbitmq.client.AMQP;
  3. import com.rabbitmq.client.BuiltinExchangeType;
  4. import com.rabbitmq.client.Channel;
  5. import top.wj.rabbitmq.client.utils.ChannelUtils;
  6. import java.io.IOException;
  7. import java.util.HashMap;
  8. import java.util.concurrent.TimeoutException;
  9. public class MessageProducer {
  10. public static void main(String[] args) throws IOException, TimeoutException {
  11. Channel channel = ChannelUtils.getChannelInstance("队列消息生产者");
  12. // 声明交换机 (交换机名, 交换机类型, 是否持久化, 是否自动删除, 是否是内部交换机, 交换机属性);
  13. channel.exchangeDeclare( "rabbitmq.wj", BuiltinExchangeType.DIRECT, true, false, false, new HashMap<>());
  14. // 设置消息属性 发布消息 (交换机名, Routing key, 可靠消息相关属性 后续会介绍, 消息属性, 消息体);
  15. AMQP.BasicProperties basicProperties = new AMQP.BasicProperties().builder().deliveryMode(2).contentType("UTF-8").build();
  16. channel.basicPublish( "rabbitmq.wj", "add", false, basicProperties, "body中的消息内容!".getBytes());
  17. }
  18. }

创建消费者:

  1. package top.wj.rabbitmq.client.consumer;
  2. import com.rabbitmq.client.*;
  3. import top.wj.rabbitmq.client.utils.ChannelUtils;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import java.util.concurrent.TimeoutException;
  7. public class MessageConsumer {
  8. public static void main(String[] args) throws IOException, TimeoutException {
  9. Channel channel = ChannelUtils.getChannelInstance( "队列消息消费者");
  10. // 声明队列 (队列名, 是否持久化, 是否排他, 是否自动删除, 队列属性);
  11. AMQP.Queue.DeclareOk declareOk = channel.queueDeclare( "rabbitmq.wj.add", true, false, false, new HashMap<>());
  12. // 声明交换机 (交换机名, 交换机类型, 是否持久化, 是否自动删除, 是否是内部交换机, 交换机属性);
  13. channel.exchangeDeclare( "rabbitmq.wj", BuiltinExchangeType.DIRECT, true, false, false, new HashMap<>());
  14. // 将队列Binding到交换机上 (队列名, 交换机名, Routing key, 绑定属性);
  15. channel.queueBind(declareOk.getQueue(), "rabbitmq.wj", "add", new HashMap<>());
  16. // 消费者订阅消息 监听如上声明的队列 (队列名, 是否自动应答(与消息可靠有关 后续会介绍), 消费者标签, 消费者)
  17. DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
  18. @Override
  19. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  20. System.out.println(consumerTag);
  21. System.out.println(envelope.toString());
  22. System.out.println(properties.toString());
  23. System.out.println( "消息内容:" + new String(body));
  24. }
  25. };
  26. channel.basicConsume(declareOk.getQueue(), true, "消费者标签",defaultConsumer );
  27. }
  28. }

控制台打印信息:

  1. 消费者标签
  2. Envelope(deliveryTag= 1, redeliver=false, exchange=rabbitmq.wj, routingKey=add)
  3. #contentHeader<basic>(content-type=UTF -8, content-encoding=null, headers=null, delivery-mode=2, priority=null, correlation-id=null, reply-to=null, expiration=null, message-id=null, timestamp=null, type=null, user-id=null, app-id=null, cluster-id=null)
  4. 消息内容:body中的消息内容!

rabbitmq管理界面显示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微笑点燃希望

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值