八.springboot+rabbitmq(文章写的比较好)(java)版direct和topic类型的exchange

RabbitMq写的比较好的:https://www.cnblogs.com/vipstone/p/9275256.html

Springboot+rabbotMq的:https://blog.csdn.net/ztx114/article/details/78410727

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

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

/**
 * Created by Administrator on 2018/7/15 0015.
 */
public class ProducerMain {

    public static void main(String[] args) throws Exception {
        fanoutExchange();

    }

    public static void fanoutExchange() throws Exception {
        Connection connection = getConnectionl();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("Fexchange1", BuiltinExchangeType.FANOUT);

        channel.queueDeclare("Fqueue1",true,false,false,null);
        channel.queueDeclare("Fqueue2",true,false,false,null);

        channel.queueBind("Fqueue1","Fexchange1","hhh");
        channel.queueBind("Fqueue2","Fexchange1","jjj");

        channel.basicPublish("Fexchange1","ggg",null,"xiaoxiceshi".getBytes());

        closeResource(channel,connection);
    }

    public static void topicExchange() throws Exception {
        Connection connection = getConnectionl();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("Texchange1", BuiltinExchangeType.TOPIC);

        channel.queueDeclare("Tqueue1",true,false,false,null);
        channel.queueDeclare("Tqueue2",true,false,false,null);

        channel.queueBind("Tqueue1","Texchange1","one.#");
        channel.queueBind("Tqueue2","Texchange1","one.*");

        channel.basicPublish("Texchange1","one.#",null,"xiaoxiceshi".getBytes());

        closeResource(channel,connection);
    }

    public static void defauleExchange()throws Exception {
        Connection connection = getConnectionl();
        Channel channel = connection.createChannel();

        channel.queueDeclare("Dqueueq1",true,false,false,null);
        channel.basicPublish("","Dqueueq1",null,"first message1".getBytes());

        closeResource(channel,connection);
    }




















    public static Connection getConnectionl() throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");

        Connection connection = factory.newConnection();

        return connection;
    }

    public static void closeResource(Channel channel,Connection connection)throws Exception{
        channel.close();
        connection.close();
    }
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tiglle/article/details/80692542
文章标签: RabbitMq
个人分类: RabbitMq
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭