Rabbit Mq简单实现流程

第一步
在管理界面增加virtual host分区(分区注意加/),再创建对应的队列在这里插入图片描述
第二步
打开idea ,引入maven仓库中的jar包

        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.16.0</version>
        </dependency>

第三步
编码连接信息

public class MqService {
    public  static Connection getConnection() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setVirtualHost("/yjg218");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setHost("192.168.241.167");
        connectionFactory.setPort(5672);
        return connectionFactory.newConnection();

    }
}

第四步
生产者

public class Producer {
    private static  final  String QUEUE_NAME="yjg01";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = MqService.getConnection();
        Channel channel = connection.createChannel();
        String msg="哥真帅";
        channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());
        channel.close();
        connection.close();
    }
}

执行完之后将信息存入rabbit mq中
第五步

   public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接
        Connection connection = MqService.getConnection();
        //设置通道
        Channel channel = connection.createChannel();
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                  String msg = new String(body, "UTF-8");
                System.out.println("消费者" + msg);
            }
        };
        //监听队列 
        // autoAck true:自动
        channel.basicConsume(QUEUE_NAME,true,defaultConsumer);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值