直接设置各个属性值,其中许多属性有其默认值,例如
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setUsername("admin");
connectionFactory.setPort(5672);
connectionFactory.setPassword("admin");
connectionFactory.setHost("172.30.18.114");
Connection connection = connectionFactory.newConnection();
也可以通过URI的方式进行连接
URI的格式为"amqp://userName:password@hostName:portNumber/virtualHost"
PS:虚拟主机的默认值为"/",这里是个坑,根据amqp协议详情,如下图所示:
我们可以知道,当虚拟主机为"/"时,URI中虚拟主机的配置则为%2f
connectionFactory.setUri( "amqp://admin:admin@172.30.18.114:5672/%2f");
Connection connection = connectionFactory.newConnection("test");
提示:连接名称,名称是可选的;但是,强烈建议开发人员提供一个,如果设置,则标识符将在日志条目和管理UI中提及。
完成连接之后,下面进行一个简单的测试:
建立通道;
声明交换机;
声明队列;
Channel channel = connection.createChannel(2);
channel.exchangeDeclare("test", "direct", true, false, null);
channel.queueDeclare("testQueue",true,false,false,null);
channel.queueBind("testQueue","test","");
RabbitMQ
- 2.0w
-
Fundebug
Docker
JavaScript
- 1.1w
-
ohcomeyes
Redis
RabbitMQ