RabbitMq之发布订阅模式

这里写了一个简单的springboot的demo来处理RabbitMq的发布订阅

添加pom依赖

		<dependency>
			<groupId>com.rabbitmq</groupId>
			<artifactId>amqp-client</artifactId>
			<version>5.8.0</version>
		</dependency>

创建连接管理类

package com.my.tool;
 
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
 
/**
 * Create by hadoop
 */
public class ConnectionUtil {
 
    public static Connection getConnection(String host,int port,String vHost,String userName,String passWord) throws Exception{
        //1、定义连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //2、设置服务器地址
        factory.setHost(host);
        //3、设置端口
        factory.setPort(port);
        //4、设置虚拟主机、用户名、密码
        factory.setVirtualHost(vHost);
        factory.setUsername(userName);
        factory.setPassword(passWord);
        //5、通过连接工厂获取连接
        Connection connection = factory.newConnection();
        return connection;
    }
}

创建订阅发布者

package com.my.tool;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.springframework.stereotype.Component;

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

@Component
public class Publish {
    private final static String EXCHANGE_NAME = "test_exchange_fanout";
    Channel channel = null;
    Connection connection = null;
    @PostConstruct
    void init() throws Exception {
        //获取连接
        Connection connection = ConnectionUtil.getConnection("127.0.0.1",5672,"/","guest","guest");
        //从连接中获取一个通道
        channel = connection.createChannel();
        //声明交换机(分发:发布/订阅模式
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
    }
    @PreDestroy
    void uninit() throws IOException, TimeoutException {
        channel.close();
        connection.close();
    }

    public  void  SendData(String message) throws IOException {
        channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("utf-8"));
    }
}

创建订阅者需要两个类来提供支撑

package com.my.tool;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

@Component
public class subscribe {
    //交换机名称
    private final static String EXCHANGE_NAME = "test_exchange_fanout";
    //队列名称
    private static final String QUEUE_NAME    = "test_queue_email";

    Channel channel = null;
    Connection connection = null;
    MyConsumer myConsumer = null;

    @PostConstruct
    void init() throws Exception {
        //获取连接
        Connection connection = ConnectionUtil.getConnection("127.0.0.1",5672,"/","guest","guest");
        //从连接中获取一个通道
        channel = connection.createChannel();
        //声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //将队列绑定到交换机
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
        //保证一次只分发一个
        int prefetchCount = 1;
        channel.basicQos(prefetchCount);

        myConsumer = new MyConsumer(channel);

        //设置应答
        boolean autoAck = true;
        //监听队列
        channel.basicConsume(QUEUE_NAME, autoAck, myConsumer);
    }

    @PreDestroy
    void uninit() throws IOException, TimeoutException {
        channel.close();
        connection.close();
    }
}
package com.my.tool;
 
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
 
import java.io.IOException;
 
public class MyConsumer extends DefaultConsumer {
 
    public MyConsumer(Channel channel) {
        super(channel);
    }
 
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        System.out.println("-----------consume message----------");
        System.out.println("consumerTag: " + consumerTag);
        System.out.println("envelope: " + envelope);
        System.out.println("properties: " + properties);
        System.out.println("body: " + new String(body));
    }
}

效果如下:

 

 

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页