目录结构如下
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;
priv