java连接rabbitMQ

maven依赖

com.rabbitmq amqp-client 5.2.0

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”);

文章知识点与官方知识档案匹配,可进一步学习相关知识Java技能树首页概览92745 人正在系统学习中

java连接rabbitMQ 微信名片 java连接rabbitMQ

来源:普通网友

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年8月6日
下一篇 2022年8月6日

相关推荐