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();
}
}
}