开发者社区 > 云原生 > 云消息队列 > 正文

参考文档示例,使用消息队列AMQP,创建连接报错

源代码如下: 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)

展开
收起
atsee 2019-10-19 11:24:47 2909 0
1 条回答
写回答
取消 提交回答
  • 技术架构师 阿里云开发者社区技术专家博主 CSDN签约专栏技术博主 掘金签约技术博主 云安全联盟专家 众多开源代码库Commiter

    Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; protocol method: #method<connection.close>(reply-code=403, reply-text=AccessDeniedByRamPolicy,

    检查connection.close的使用

    2019-11-05 09:08:42
    赞同 展开评论 打赏

涵盖 RocketMQ、Kafka、RabbitMQ、MQTT、轻量消息队列(原MNS) 的消息队列产品体系,全系产品 Serverless 化。RocketMQ 一站式学习:https://rocketmq.io/

热门讨论

热门文章

相关电子书

更多
企业互联网架构之消息队列 立即下载
基于消息队列RocketMQ的大型分布式应用上云最佳实践 立即下载
云原生消息队列Apache RocketMQ 立即下载