maven依赖
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.2.0</version>
</dependency>
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class RabbitMQ {
//消息队列名称
public final static String QUEUE_NAME = "myTest";
public static Connection getConnection() throws IOException, TimeoutException {
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置rabbotMQ 主机
factory.setHost("x.x.x.x");
//设置rabbotMQ用户名
factory.setUsername("admin");
设置rabbotMQ密码
factory.setPassword("admin");
//设置rabbotMQ mq协议端口
factory.setPort(5672);
//设置rabbotMQ 数据库
factory.setVirtualHost("/wujin");
//创建一个连接并返回
Connection newConnection = factory.newConnection();
return newConnection;
}
}
消息生产者
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import cn.wujinkuaibao.common.RabbitMQ;
public class RabbitSend {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RabbitMQ.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(RabbitMQ.QUEUE_NAME, false, false, false, null);
//自动应答数量
channel.basicQos(1);
String msg = "Hello World";
channel.basicPublish("", RabbitMQ.QUEUE_NAME, null, msg.getBytes());
channel.close();
connection.close();
}
}
消费者
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import cn.wujinkuaibao.common.RabbitMQ;
public class RabbitGetmsg {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RabbitMQ.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(RabbitMQ.QUEUE_NAME, false, false, false, null);
channel.basicQos(1);
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body,"utf-8");
System.out.println("消费者:"+message);
//回执
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
boolean ack = false;//是否自动应答
channel.basicConsume(RabbitMQ.QUEUE_NAME, ack , consumer);
}
}
消息发布和订阅
交换机设置
public class RabbitSend {
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
Connection connection = RabbitMQ.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(RabbitMQ.EXCHANGE_NAME, "fanout");
String msg = "Hello World";
channel.basicPublish(RabbitMQ.EXCHANGE_NAME, "", null, msg.getBytes());
channel.close();
connection.close();
}
}
消费者
public class RabbitGetmsg {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RabbitMQ.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(RabbitMQ.SMS_NAME, false, false, false, null);
channel.queueBind(RabbitMQ.SMS_NAME, RabbitMQ.EXCHANGE_NAME, "");
channel.basicQos(1);
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
try {
String message = new String(body,"utf-8");
System.out.println("消费者:"+message);
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("消费者:结束");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
boolean ack = false;//自动应答
channel.basicConsume(RabbitMQ.SMS_NAME, ack, consumer);
}
}
配置路由
channel.exchangeDeclare(RabbitMQ.EXCHANGE_NAME2, "direct");
String routingKey = "info";
channel.basicPublish(RabbitMQ.EXCHANGE_NAME2, routingKey, null, msg.getBytes());
消费者配置路由可以配置多个
String routingKey = "error";
channel.queueBind(RabbitMQ.EMAIL_NAME2, RabbitMQ.EXCHANGE_NAME2, routingKey);
channel.queueBind(RabbitMQ.EMAIL_NAME2, RabbitMQ.EXCHANGE_NAME2, "info");
channel.queueBind(RabbitMQ.EMAIL_NAME2, RabbitMQ.EXCHANGE_NAME2, "warning");
topic模式
生产者
channel.exchangeDeclare(RabbitMQ.EXCHANGE_NAME2, "topic");
channel.queueBind(RabbitMQ.EMAIL_NAME2, RabbitMQ.EXCHANGE_NAME2, "warning.strength");
消费者 ( #代表匹配所有 *代表一个)
channel.queueBind(RabbitMQ.EMAIL_NAME2, RabbitMQ.EXCHANGE_NAME2, "warning.#");
channel.queueBind(RabbitMQ.EMAIL_NAME2, RabbitMQ.EXCHANGE_NAME2, "warning.strength");