源代码如下: public class BindingKeyTest { public static void main(String[] args) throws IOException, TimeoutException, NoSuchAlgorithmException, InvalidKeyException {
// long timestamp = System.currentTimeMillis();
ConnectionFactory factory = new ConnectionFactory();
// 设置接入点,到控制台概览页面上查看获取
factory.setHost("**********");
//一定要这个才能自动恢复
factory.setCredentialsProvider(new AliyunCredentialsProvider("****", "*****", 1127314737175512L));//这个已设置正确
factory.setAutomaticRecoveryEnabled(true);
factory.setNetworkRecoveryInterval(5000);
// 设置 Vhost 名称,请确保已在控制台上创建完成
factory.setVirtualHost("test");
// 固定端口
factory.setPort(5672);
factory.setConnectionTimeout(300 * 1000);
factory.setHandshakeTimeout(300 * 1000);
factory.setShutdownTimeout(0);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 创建 ${ExchangeName}
//Exchange 可以在控制台创建,也可以用 API 创建。如果控制台已存在,则 Exchange 的类型必须和控制台一致
channel.exchangeDeclare("myExchange", "direct", true);
// 创建 ${QueueName}。Queue 可以在控制台创建,也可以用 API 创建
HashMap map = new HashMap();
channel.queueDeclare("myQueue", true, false, false, map);
// Queue 与 Exchange 进行绑定,注册 BindingKeyTest
channel.queueBind("myQueue", "myExchange", "myBindKey");
connection.close();
connection.close();
}
}
结果报错: Exception in thread "main" java.io.IOException at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:124) at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:120) at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:142) at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:407) at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:63) at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:138) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:973) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:932) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:890) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1044) at com.util.BindingKeyTest.main(BindingKeyTest.java:38)
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; protocol method: #method<connection.close>(reply-code=403, reply-text=AccessDeniedByRamPolicy, ReqId:5DAA80614431426413555927, class-id=10, method-id=40) at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66) at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36) at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:443) at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:263) at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:136) ... 8 more
[AMQP Connection 112.124.156.22:5672] ERROR com.rabbitmq.client.impl.ForgivingExceptionHandler - An unexpected connection driver error occured java.net.SocketException: socket closed at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:129) at java.io.BufferedInputStream.fill(BufferedInputStream.java:218) at java.io.BufferedInputStream.read(BufferedInputStream.java:237) at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:271) at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:91) at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:164) at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:602) at java.lang.Thread.run(Thread.java:619)
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; protocol method: #method<connection.close>(reply-code=403, reply-text=AccessDeniedByRamPolicy,
检查connection.close的使用
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
涵盖 RocketMQ、Kafka、RabbitMQ、MQTT、轻量消息队列(原MNS) 的消息队列产品体系,全系产品 Serverless 化。RocketMQ 一站式学习:https://rocketmq.io/