RabbitMQ入门及常用的5种模式的简单使用(一)

RabbitMQ是一个非常常用也非常强大的消息中间件,主要用于应用与应用之间的通信,有五种常见的使用方式,分别是:简单模式,工作模式,发布订阅模式,路由模式以及通配符模式,这里主要是简单模式和工作模式!

package cn.itcast.rabbitmq.simple;


import cn.itcast.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;


/**
 * rabbitmq的第一种模式:简单模式(点对点)
 *                   该模式下,一个生产者对应一个消费者
 * 
 * @author 桑伟东
 *
 */
public class Send {


private final static String QUEUE_NAME = "test_queue";


public static void main(String[] argv) throws Exception {
// 获取到连接以及mq通道
Connection connection = ConnectionUtil.getConnection();
// 从连接中创建通道
Channel channel = connection.createChannel();

// 声明(创建)队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);

// 消息内容
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");

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

}


为了简化代码,这里抽取了一个公共的方法,用来获取连接

package cn.itcast.rabbitmq.util;


import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
/**
 * 通用的获取连接的工具类
 * @author 桑伟东
 *
 */
public class ConnectionUtil {


    public static Connection getConnection() throws Exception {
        //定义连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置服务地址
        factory.setHost("localhost");
        //端口
        factory.setPort(5672);
        //设置账号信息,用户名、密码、vhost
        factory.setVirtualHost("/swd");//虚拟主机
        factory.setUsername("root");
        factory.setPassword("root");
        // 通过工程获取连接
        Connection connection = factory.newConnection();
        return connection;
    }


}

package cn.itcast.rabbitmq.simple;


import cn.itcast.rabbitmq.util.ConnectionUtil;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
/**
 * 简单模式的接收消息
 * @author 桑伟东
 *
 */
public class Recv {


    private final static String QUEUE_NAME = "test_queue";

    public static void main(String[] argv) throws Exception {

        // 获取到连接以及mq通道
        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 + "'");
        }
        
    }

}


工作模式

package cn.itcast.rabbitmq.work;


import cn.itcast.rabbitmq.util.ConnectionUtil;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;


public class Send {


    private final static String QUEUE_NAME = "test_queue_work";


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


        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);


        for (int i = 0; i < 50; 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 cn.itcast.rabbitmq.work;


import cn.itcast.rabbitmq.util.ConnectionUtil;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;


public class Recv {


    private final static String QUEUE_NAME = "test_queue_work";


    public static void main(String[] argv) throws Exception {


        // 获取到连接以及mq通道
        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] Received '" + message + "'");
            //休眠
            Thread.sleep(10);
            // 返回确认状态
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }
}

package cn.itcast.rabbitmq.work;


import cn.itcast.rabbitmq.util.ConnectionUtil;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;


public class Recv2 {


    private final static String QUEUE_NAME = "test_queue_work";


    public static void main(String[] argv) throws Exception {


        // 获取到连接以及mq通道
        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] Received '" + message + "'");
            // 休眠1秒
            Thread.sleep(1000);


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

}


你可能感兴趣的:(java技术)