RabbitMQ的工作模式之二工作(Work)模式

RabbitMQ的工作模式之二工作(Work)模式

由一个生产者发送消息到队列中,可由多个消费者接收,每个消费者获取的信息不同,如果不设置则是平均分配消息。

导入依赖

<!--RabbitMQ的依赖-->
<dependency>
  <groupId>com.rabbitmq</groupId>
  <artifactId>amqp-client</artifactId>
  <version>5.7.3</version>
</dependency>

首先创建工具类,用以生成连接,避免重复书写此段代码

public class RabbitMQUtil {
    private static ConnectionFactory factory = new ConnectionFactory();
    static{
        //用户名
        factory.setUsername("hello");
        //密码
        factory.setPassword("123456");
        //虚拟机
        factory.setVirtualHost("/myhost");
        //IP地址,运行rabbitmq组件的主机ip地址
        factory.setHost("192.168.31.14");
        //端口
        factory.setPort(5672);
    }

    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = factory.newConnection();
            return  connection;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return null;
    }
}

生产者代码

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取连接
        Connection connection= RabbitMQUtil.getConnection();
        //创建通道
        Channel channel = connection.createChannel();
        /**
         * 声明一个队列:因为要将消息发送到队列中
         * 参数一:队列名称,有则使用,无则创建
         * 参数二:是否进行持久化 false表示不进行持久化
         * 参数三:是否私有化,false表示不进行私有化,true表示私有化:只有第一次使用该队列的消费者才可以使用此队列
         * 参数四:表示连接关闭后,是否自动删除该对列,false表示不删除
         * 参数五:扩展参数,额外的一配置
        */
        channel.queueDeclare("work_queue",false,false,false,null);
        //循环发送一百条信息到对列中
        for (int i = 0; i <100; i++) {
            //创建消息对象
            Msg msg = new Msg(i,"发送的短信"+i);
            //发送信息到队列中
            //参数一: 是交换机的名称,目前暂时不用
            //参数二: 对列的名称,表示向哪个队列发送消息
            //参数三:是额外的配置
            //参数四: 发送的消息
            channel.basicPublish("","work_queue",null,msg.getContent().getBytes());
        }
        //关闭通道
        channel.close();
        //关闭连接
        connection.close();
    }
}

其中消息对象代码

public class Msg {

    private Integer id;

    private String content;

    public Msg() {
    }

    public Msg(Integer id, String content) {
        this.id = id;
        this.content = content;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}

消费者端代码

public class Consumer1 {
    public static void main(String[] args) throws IOException {
        //获取连接
        Connection connection = RabbitMQUtil.getConnection();
        //创建通道
        final Channel channel = connection.createChannel();
        //声明对列
        channel.queueDeclare("work_queue",false,false,false,null);
        //一条消息签收完毕,继续消费获取消息,根据性能分配任务,如不配置则平均分配
        channel.basicQos(1);
        //签收
        channel.basicConsume("work_queue",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者1收到的消息:"+new String(body));
                //消息的id,false表示只签收当前消息
                channel.basicAck(envelope.getDeliveryTag(),false);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
public class Consumer2 {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtil.getConnection();
        final Channel channel = connection.createChannel();
        channel.queueDeclare("work_queue",false,false,false,null);
        channel.basicQos(1);
        //签收
        channel.basicConsume("work_queue",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者2收到的消息:"+new String(body));
                //消息的id,false表示只签收当前消息
                channel.basicAck(envelope.getDeliveryTag(),false);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值