RabbitMq学习(一)

一 RabbitMq的重要概念

在这里插入图片描述

  1. Exchanges :交换器,必须的作用类似于转换器,可以将消息通过routeKey转发到对应的队列中;
  2. routeKey:路由键,类似于路牌,生产着将要发送的消息发到交换器上,并指定路由键,然后交换器通过路由键将消息发送到对应的queue中;
  3. Queue:消息队列。消息的载体。
  4. Binding:作用就是将Exchange和Queue按照某种路由规则绑定起来。

二 如何使用rabbitmq提供的客户端

		<dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.2.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
  1. 创建一个ConnectionFactory,并设置一系列的属性
	ConnectionFactory connectionFactory = new ConnectionFactory();
 	connectionFactory.setHost("192.168.0.107");//设置主机名称
 	connectionFactory.setPort(5672);//端口
    connectionFactory.setUsername("mqadmin");//用户名
    connectionFactory.setPassword("mqadminpassword");//密码
    connectionFactory.setConnectionTimeout(3000);//超时时间
  1. 通过connectionFactory创建一个连接,通过该链接创建一个Channel,所有的操作都是基于Channel进行的(工厂模式无处不在)
	Connection connection = connectionFactory.newConnection();
	Channel channel = connection.createChannel();
  1. 创建一个队列,使用queueDeclareI(),至于各个参数可以自己测验一下
	 /**
     * Declare a queue
     * @see com.rabbitmq.client.AMQP.Queue.Declare
     * @see com.rabbitmq.client.AMQP.Queue.DeclareOk
     * @param queue the name of the queue  队列名称,随意写
     * @param durable true if we are declaring a durable queue (the queue will survive a server restart) 是否持久化,true:持久化,就是重启服务之后队列任然存在,但是消息不回存在。
     * @param exclusive true if we are declaring an exclusive queue (restricted to this connection) 是否只有该创建这才能使用该队列。
     * @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use) 当长时间没有连接的时候是否自动删除。
     * @param arguments other properties (construction arguments) for the queue 其他属性 Map对象
     * @return a declaration-confirm method to indicate the queue was successfully declared
     * @throws java.io.IOException if an error is encountered
     */
	 channel.queueDeclare("queueName",true,false,false,null);
  1. 创建一个Exchange 参数意思都差不多
	/**
     * Declare an exchange.
     * @see com.rabbitmq.client.AMQP.Exchange.Declare
     * @see com.rabbitmq.client.AMQP.Exchange.DeclareOk
     * @param exchange the name of the exchange
     * @param type the exchange type  交换器的类型,常用的topic,和direct,fanout,其他的有headers,接下来会对其作解释,并举例,代码说明
     * @param durable true if we are declaring a durable exchange (the exchange will survive a server restart)
     * @param autoDelete true if the server should delete the exchange when it is no longer in use
     * @param arguments other properties (construction arguments) for the exchange
     * @return a declaration-confirm method to indicate the exchange was successfully declared
     * @throws java.io.IOException if an error is encountered
     */
	 channel.exchangeDeclare("exchangeName","topic",true,false,null);
  1. exchange创建玩成之后,需要将queue与exchange绑定。
	 /**
     * Bind a queue to an exchange, with no extra arguments.
     * @see com.rabbitmq.client.AMQP.Queue.Bind
     * @see com.rabbitmq.client.AMQP.Queue.BindOk
     * @param queue the name of the queue
     * @param exchange the name of the exchange
     * @param routingKey the routing key to use for the binding
     * @return a binding-confirm method if the binding was successfully created
     * @throws java.io.IOException if an error is encountered
     */
	 channel.queueBind("queueName","exchangeName","routeKey");
  1. 发送消息,并不需要指明队列是哪个,只通过交换器和routeKey来发消息
	 /**
     * Publish a message.
     *
     * Publishing to a non-existent exchange will result in a channel-level
     * protocol exception, which closes the channel.
     *
     * Invocations of <code>Channel#basicPublish</code> will eventually block if a
     * <a href="http://www.rabbitmq.com/alarms.html">resource-driven alarm</a> is in effect.
     *
     * @see com.rabbitmq.client.AMQP.Basic.Publish
     * @see <a href="http://www.rabbitmq.com/alarms.html">Resource-driven alarms</a>
     * @param exchange the exchange to publish the message to
     * @param routingKey the routing key
     * @param props other properties for the message - routing headers etc
     * @param body the message body
     * @throws java.io.IOException if an error is encountered
     */
	 channel.basicPublish("exchange Name","routingKey", MessageProperties.TEXT_PLAIN,"hello world".getBytes());
  1. 消费消息 主要注意的是Consumer,你可以自己定义一个类并实现Consumer接口。 通过void handleDelivery(String consumerTag,
    Envelope envelope,
    AMQP.BasicProperties properties,
    byte[] body)
    throws IOException; 该方法对队列的消息进行其他的操作,body 消息内容。
	/**
     * Start a non-nolocal, non-exclusive consumer, with
     * explicit acknowledgement and a server-generated consumerTag.
     * @param queue the name of the queue
     * @param callback an interface to the consumer object
     * @return the consumerTag generated by the server
     * @throws java.io.IOException if an error is encountered
     * @see com.rabbitmq.client.AMQP.Basic.Consume
     * @see com.rabbitmq.client.AMQP.Basic.ConsumeOk
     * @see #basicAck
     * @see #basicConsume(String, boolean, String, boolean, boolean, Map, Consumer)
     */
    String basicConsume(String queue, Consumer callback) throws IOException;

以上一个比较简单的例子就完成了。接下来的文章将会对四种模式进行说明。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值