rabbitmq官方的六种工作模式

1.RabbitMq
1.1介绍
RabbitMQ是一个消息代理:它接受并转发消息。你可以把它当成一个邮局:当你想邮寄信件的时候,你会把信件放在投递箱中,并确信邮递员最终会将信件送到收件人的手里。在这个例子中,RabbitMQ就相当与投递箱、邮局和邮递员。

AMQP协议中的核心思想就是生产者和消费者隔离,生产者从不直接将消息发送给队列。生产者通常不知道是否一个消息会被发送到队列中,只是将消息发送到一个交换机。先由Exchange来接收,然后Exchange按照特定的策略转发到Queue进行存储。同理,消费者也是如此。Exchange 就类似于一个交换机,转发各个消息分发到相应的队列中。
引入pom文件:


org.springframework.boot
spring-boot-starter-web

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>com.rabbitmq</groupId>
		<artifactId>amqp-client</artifactId>
		<version>3.4.1</version>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-log4j12</artifactId>
		<version>1.7.7</version>
	</dependency>
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-lang3</artifactId>
		<version>3.3.2</version>
	</dependency>

	<dependency>
		<groupId>org.springframework.amqp</groupId>
		<artifactId>spring-rabbit</artifactId>
		<version>1.4.0.RELEASE</version>
	</dependency>
</dependencies>


编写连接工具类:

package com.sakura.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
public class ConnectionUtil {
public static Connection getConnection() throws IOException {
//连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(“localhost”);
//连接5672端口 注意15672为工具界面端口 25672为集群端口
factory.setPort(5672);
factory.setVirtualHost("/jxd");
factory.setUsername(“jxd”);
factory.setPassword(“123”);
//获取连接
Connection connection = factory.newConnection();

    return connection;

}

}

1.2.1简单模式
生产者:
package com.sakura.simple;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.sakura.util.ConnectionUtil;
import java.io.IOException;
public class Sender {
private final static String QUEUE_NAME = “simple_queue”;

public static void main(String[] args) throws IOException {
    //创建连接
    Connection connection = ConnectionUtil.getConnection();
    //创建通道
    Channel channel = connection.createChannel();
    //声明队列
    /**
     * 队列名
     * 是否持久化
     *  是否排外  即只允许该channel访问该队列   一般等于true的话用于一个队列只能有一个消费者来消费的场景
     *  是否自动删除  消费完删除
     *  其他属性
     *
     */
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    //消息内容
    /**
     * 交换机
     * 队列名
     * 其他属性  路由
     * 消息body
     */
    String message = "错的不是我,是这个世界~";
    channel.basicPublish("", QUEUE_NAME,null,message.getBytes());
    System.out.println("[x]Sent '"+message + "'");

    //最后关闭通关和连接
    channel.close();
    connection.close();


}

}
消费者:
package com.sakura.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;
import java.io.IOException;
public class Receiver {
private final static String QUEUE_NAME = “simple_queue”;

public static void main(String[] args) throws IOException, InterruptedException {
    //获取连接
    Connection connection = ConnectionUtil.getConnection();
    //获取通道
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, true, consumer);

    while(true){
        //该方法会阻塞
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println("[x] Received '"+message+"'");

    }
}

}

1.2.1work模式
生产者:
package com.sakura.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;
public class Sender {
private final static String QUEUE_NAME = “queue_work”;

public static void main(String[] args) throws IOException, InterruptedException {
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    for(int i = 0; i < 100; i++){
        String message = "冬马小三" + i;
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println("[x] Sent '"+message + "'");
        Thread.sleep(i*10);
    }

    channel.close();
    connection.close();
}

}
消费者:
package com.sakura.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

public class Receiver1 {
private final static String QUEUE_NAME = “queue_work”;

public static void main(String[] args) throws IOException, InterruptedException {
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false,false, false,null);
    //同一时刻服务器只会发送一条消息给消费者
    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    //关于手工确认 待之后有时间研究下
    channel.basicConsume(QUEUE_NAME, false, consumer);

    while(true){
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println("[x] Received1 '"+message+"'");
        Thread.sleep(10);
        //返回确认状态
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }

}

}

消费者二:
package com.sakura.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

public class Receiver2 {
private final static String QUEUE_NAME = “queue_work”;

public static void main(String[] args) throws IOException, InterruptedException {
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false,false, false,null);
    //同一时刻服务器只会发送一条消息给消费者
    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, false, consumer);

    while(true){
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println("[x] Received2 '"+message+"'");
        Thread.sleep(1000);
        //返回确认状态
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }

}

}
1.2.3订阅模式
1.2.4路由模式
生产者
package com.sakura.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;
public class Sender {
private final static String EXCHANGE_NAME = “exchange_direct”;
private final static String EXCHANGE_TYPE = “direct”;

public static void main(String[] args) throws IOException {
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME,EXCHANGE_TYPE);

    String message = "那一定是蓝色";
    channel.basicPublish(EXCHANGE_NAME,"key2", null, message.getBytes());
    System.out.println("[x] Sent '"+message+"'");

    channel.close();
    connection.close();
}

}
消费者:
package com.sakura.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

public class Receiver1 {
private final static String QUEUE_NAME = “queue_routing”;
private final static String EXCHANGE_NAME = “exchange_direct”;

public static void main(String[] args) throws IOException, InterruptedException {
    // 获取到连接以及mq通道
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false,false,false,null);
    channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"key");
    channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"key2");

    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, false, consumer);

    while(true){
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println("[x] Received1 "+message);
        Thread.sleep(10);

        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }


}

}
消费者二
package com.sakura.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

public class Receiver2 {
private final static String QUEUE_NAME = “queue_routing2”;
private final static String EXCHANGE_NAME = “exchange_direct”;

public static void main(String[] args) throws IOException, InterruptedException {
    // 获取到连接以及mq通道
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false,false,false,null);
    channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"key2");

    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, false, consumer);

    while(true){
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println("[x] Received2 "+message);
        Thread.sleep(10);

        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }


}

}

路由模式下,一个队列可以绑定多个路由

1.2.5topic模式
生产者:
package com.sakura.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

public class Sender {
private final static String EXCHANGE_NAME = “exchange_topic”;
private final static String EXCHANGE_TYPE = “topic”;

public static void main(String[] args) throws IOException {
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);

    //消息内容
    String message = "如果真爱有颜色";
    channel.basicPublish(EXCHANGE_NAME,"key.1",null,message.getBytes());
    System.out.println("[x] Sent '"+message+"'");

    //关通道 关连接
    channel.close();
    connection.close();
}

}
消费者:
package com.sakura.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

public class Receiver1 {
private final static String QUEUE_NAME = “queue_topic”;
private final static String EXCHANGE_NAME = “exchange_topic”;
private final static String EXCHANGE_TYPE = “topic”;

public static void main(String[] args) throws IOException, InterruptedException {
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false,false, null);
    channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "key.*");

    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, false, consumer);

    while(true){
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println("[x] Received1 '"+message + "'");
        Thread.sleep(10);

        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

}
消费者二
package com.sakura.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.sakura.util.ConnectionUtil;

import java.io.IOException;

public class Receiver2 {
private final static String QUEUE_NAME = “queue_topic2”;
private final static String EXCHANGE_NAME = “exchange_topic”;
private final static String EXCHANGE_TYPE = “topic”;

public static void main(String[] args) throws IOException, InterruptedException {
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false,false, null);
    channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "*.*");

    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, false, consumer);

    while(true){
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println("[x] Received2 '"+message + "'");
        Thread.sleep(10);

        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值