码迷,mamicode.com
首页 > 其他好文 > 详细

RabbitMQ

时间:2020-04-14 16:56:00      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:一个   pack   java   nts   efault   特性   图片   负载   create   

RabbitMQ是用Erlang实现的一个高并发高可靠AMQP消息队列服务器。
支持消息的持久化、事务、拥塞控制、负载均衡等特性,使得RabbitMQ拥有更加广泛的应用场景。

技术图片

 

 使用场景:

场景1:单发送单接收 

使用场景:简单的发送与接收,没有特别的处理。

技术图片

package com.qa.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConnectionUtil {

    private static ConnectionFactory factory;

    static{
        //连接rabbitmq服务器
        factory = new ConnectionFactory();
        factory.setHost("192.168.189.128");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("/admin");
    }

    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 Provider {
    public static void main(String[] args) throws Exception {
        Connection connection =  ConnectionUtil.getConnection();
        //获取通道Channel对象,后续所有的操作都是基于Channel实现
        Channel channel = connection.createChannel();
        //创建队列
        channel.queueDeclare("queue1",false,false,false,null);
        //给队列发送消息
        channel.basicPublish(
                "",
                "queue1",
                null,
                "Hello RabbitMQ".getBytes("utf-8"));
        //断开链接
        connection.close();
    }
}
public class Consumer {
    public static void main(String[] args) throws Exception {
        Connection connection =  ConnectionUtil.getConnection();
        //获取Channel对象
        Channel channel = connection.createChannel();
        //监听队列中的消息
        channel.basicConsume("queue1",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费端获得消息:"+new String(body,"utf-8"));
            }
        });
    }
}

场景2:单发送多接收

使用场景:一个发送端,多个接收端,如分布式的任务派发。为了保证消息发送的可靠性,不丢失消息,使消息持久化了。同时为了防止接收端在处理消息时down掉,只有在消息处理完成后才发送ack消息。

技术图片

public class Provider {
    public static void main(String[] args) throws Exception {
        Connection connection =  ConnectionUtil.getConnection();
        //获取通道Channel对象,后续所有的操作都是基于Channel实现
        Channel channel = connection.createChannel();
        //创建队列
        channel.queueDeclare("queue1",false,false,false,null);
        //给队列发送消息
        for (int i=1;i<=10;i++){
            channel.basicPublish(
                    "",
                    "queue1",
                    null,
                    ("Hello RabbitMQ"+i).getBytes("utf-8"));
        }
        //断开链接
        connection.close();
    }
}
public class Consumer {
    public static void main(String[] args) throws Exception {
        Connection connection =  ConnectionUtil.getConnection();
        //获取Channel对象
        Channel channel = connection.createChannel();
        //监听队列中的消息
        channel.basicConsume("queue1",true,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,"utf-8"));
            }
        });
    }
}
public class Consumer2 {
    public static void main(String[] args) throws Exception {
        Connection connection =  ConnectionUtil.getConnection();
        //获取Channel对象
        Channel channel = connection.createChannel();
        //监听队列中的消息
        channel.basicConsume("queue1",true,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,"utf-8"));
            }
        });
    }
}

技术图片

 

RabbitMQ

标签:一个   pack   java   nts   efault   特性   图片   负载   create   

原文地址:https://www.cnblogs.com/wakey/p/12698798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!