目录结构如下
common包下的ExchangeType。定义相应的交换模式
package com.rosam.demo.common;
public enum ExchangeType {
FANOUT("fanout", "fanout"),
DIRECT("direct", "direct"),
TOPIC("topic", "topic"),
HEADERS("headers", "headers");
private ExchangeType(String name, String type){
this.name = name;
this.type = type;
}
private String name;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
**
direct模式
**
comsumer:消费者
package com.rosam.demo.rabbitmq.direct;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rosam.demo.common.ExchangeType;
class Consumer {
private static final String QUEUE_NAME = "BOYS";
private static final String QUEUE_NAME_2 = "GRILS";
private static ConnectionFactory factory;
private static Connection connection;
private static Channel channel;
static{
try {
factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(ExchangeType.DIRECT.getName(), ExchangeType.DIRECT.getType());
/*1.队列名称,2.是否可持久化,3.是否排他列队,4.是否自动删除(没有消费者订阅时)*/
channel.queueDeclare(QUEUE_NAME, false, false, true, null);
channel.queueBind(QUEUE_NAME, ExchangeType.DIRECT.getName(), "boys");
channel.queueDeclare(QUEUE_NAME_2, false, false, true, null);
channel.queueBind(QUEUE_NAME_2, ExchangeType.DIRECT.getName(), "girls");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
public static void receive() throws ShutdownSignalException, ConsumerCancelledException, InterruptedException, IOException{
com.rabbitmq.client.Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body);
System.out.println("cousumer received message:" + msg);
}
};
com.rabbitmq.client.Consumer consumer2 = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body);
System.out.println("cousumer2 received message:" + msg);
}
};
/*1.队列名称;2.是否自动发送ack;3.消费者*/
channel.basicConsume(QUEUE_NAME, true, consumer);
channel.basicConsume(QUEUE_NAME_2, true, consumer2);
}
public static void main(String[] args) throws IOException, TimeoutException {
try {
receive();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Producer:生产者
package com.rosam.demo.rabbitmq.direct;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import com.rosam.demo.common.ExchangeType;
public class Producer {
private static final String QUEUE_NAME = "BOYS";
private static final String QUEUE_NAME_2 = "GRILS";
private static ConnectionFactory factory;
private static Connection connection;
private static Channel channel;
static{
try {
factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(ExchangeType.DIRECT.getName(), ExchangeType.DIRECT.getType());
/*1.队列名称,2.是否可持久化,3.是否排他列队,4.是否自动删除(空闲时)*/
channel.queueDeclare(QUEUE_NAME, false, false, true, null);
channel.queueBind(QUEUE_NAME, ExchangeType.DIRECT.getName(), "boys");
channel.queueDeclare(QUEUE_NAME_2, false, false, true, null);
channel.queueBind(QUEUE_NAME_2, ExchangeType.DIRECT.getName(), "girls");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
public static void send(String msg) throws IOException, TimeoutException{
try {
/*1.交换机名称;2.路由关键字即routing key;3.配置信息,contentType、contentEncoding等;4.消息,字节数组格式*/
channel.basicPublish(ExchangeType.DIRECT.getName(), "boys", MessageProperties.MINIMAL_BASIC, msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
channel.close();
connection.close();
}
}
public static void main(String[] args) {
try {
send("hello rabbitmq");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
**
Topic模式
**
Producer:生产者
package com.rosam.demo.rabbitmq.topic;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rosam.demo.common.ExchangeType;
public class Producer {
private static final String QUEUE_NAME = "BOYS";
private static final String QUEUE_NAME_2 = "GRILS";
private static ConnectionFactory factory;
private static Connection connection;
private static Channel channel;
static{
try {
factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(ExchangeType.TOPIC.getName(), ExchangeType.TOPIC.getType());
/*1.队列名称,2.是否可持久化,3.是否排他列队,4.是否自动删除(空闲时)*/
channel.queueDeclare(QUEUE_NAME, false, false, true, null);
channel.queueBind(QUEUE_NAME, ExchangeType.TOPIC.getName(), "boys.*.*");
channel.queueDeclare(QUEUE_NAME_2, false, false, true, null);
channel.queueBind(QUEUE_NAME_2, ExchangeType.TOPIC.getName(), "*.*.girls");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
public static void send(String msg) throws IOException, TimeoutException{
try {
/*1.交换机名称;2.路由关键字即routing key;3;4.消息,字节数组格式*/
channel.basicPublish(ExchangeType.TOPIC.getName(), "boys.like.girls", null, msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
channel.close();
connection.close();
}
}
public static void main(String[] args) {
try {
send("hello rabbitmq");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
Comsumer:消费者
package com.rosam.demo.rabbitmq.topic;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rosam.demo.common.ExchangeType;
public class Producer {
private static final String QUEUE_NAME = "BOYS";
private static final String QUEUE_NAME_2 = "GRILS";
private static ConnectionFactory factory;
private static Connection connection;
private static Channel channel;
static{
try {
factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(ExchangeType.TOPIC.getName(), ExchangeType.TOPIC.getType());
/*1.队列名称,2.是否可持久化,3.是否排他列队,4.是否自动删除(空闲时)*/
channel.queueDeclare(QUEUE_NAME, false, false, true, null);
channel.queueBind(QUEUE_NAME, ExchangeType.TOPIC.getName(), "boys.*.*");
channel.queueDeclare(QUEUE_NAME_2, false, false, true, null);
channel.queueBind(QUEUE_NAME_2, ExchangeType.TOPIC.getName(), "*.*.girls");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
public static void send(String msg) throws IOException, TimeoutException{
try {
/*1.交换机名称;2.路由关键字即routing key;3;4.消息,字节数组格式*/
channel.basicPublish(ExchangeType.TOPIC.getName(), "boys.like.girls", null, msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
channel.close();
connection.close();
}
}
public static void main(String[] args) {
try {
send("hello rabbitmq");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}