rabbitmq_DefaultExchange

pom.xml
<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>

    <version>4.0.2</version>
</dependency>
Exchange对象有四种  (若没有指定Exchange则会设置默认的一个Default Exchange)
Direct Exchange
Fanout Exchange  Fanout展开分列
Topic Exchange
Header Exchange

Default Exchange生产者图示
图1

图1中的channel.basicPublish("", "firstQueue", props, message.getBytes());的具体


Default Exchange生产者类

package com.qbsea.rabbitmq.product.defaultexchange;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

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

public class ProducerApp {
	public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = null;
        Channel channel = null;
        try{
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("127.0.0.1");
            factory.setPort(5672);
            factory.setUsername("sunlei");
            factory.setPassword("sunlei");
            factory.setVirtualHost("/vhost_sunlei");
 
            //创建与RabbitMQ服务器的TCP连接
            connection  = factory.newConnection();
            channel = connection.createChannel();
            channel.queueDeclare("firstQueue", true, false, false, null);
            String message = "Dirst Message";    
            //非持久化消息 props=null;
            //channel.basicPublish("", "firstQueue", null, message.getBytes());
            
            //持久化消息 props!=null;
            AMQP.BasicProperties props =
                    new AMQP.BasicProperties("text/plain",
                            "UTF-8",
                            null,
                            2,
                            0, null, null, null,
                            null, null, null, null,
                            null, null);
            channel.basicPublish("", "firstQueue", props, message.getBytes());
            System.out.println("Send Message is:'" + message + "'");            
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            if(channel != null){
                channel.close();
            }
            if(connection != null){
                connection.close();
            }
        }
    }
}

--------------------------------
Default Exchange消费者的类

消费者类的具体代码

package com.qbsea.modules.rabbitmqconsumer;

import java.io.IOException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class ConsumerApp {
	public static void main(String[] args) {
		Connection connection = null;
		Channel channel = null;
		try {
			ConnectionFactory factory = new ConnectionFactory();
			factory.setHost("127.0.0.1");
			factory.setPort(5672);
			factory.setUsername("admin");
			factory.setPassword("admin");
			factory.setVirtualHost("/vhost_sunlei");
			connection = factory.newConnection();
			channel = connection.createChannel();
			Consumer consumer = new DefaultConsumer(channel) {
				/**
				 * 参数1 consumerTag 接收到消息时的消费者Tag 如果没指定默认随机生成
				 * 参数2 envelope 消息打包信息 包含四个属性_deliveryTag 消息发送编号,这条消息发送的第几条消息
				 * 								   _redeliver重传标志 执行失败后是否重发 fasle为不重发
				 *        _exchange消息发送到的Exchange名称 当exchange名称为空使用的是Default Exchange
				 *                                  _routingKey发送的路由Key这里设置的“firstQueue”
				 * 参数3 properties
				 * 参数4 body 消息体
				 * */
				@Override
				public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
						byte[] body) throws IOException {
					String message = new String(body, "UTF-8");
					System.out.println(" Consumer have received '" + message + "'");
				}
			};
			/**
			 * String queue, boolean autoAck, Consumer callback
			 * 参数1 queue 绑定的队列的名称
			 * 参数2 autoAck 自动确认参数,true表示接收到消息后会自动发送确认消息 然后消息队列会把这台消息删除
			 * 参数3 callback consumer对象
			 * */
			channel.basicConsume("firstQueue", true, consumer);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值