今天,我在学习RabbitMQ时,运行代码报错了!!
简单生产者代码:
package com.xiaoli.producer;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author 小李Code
* RabbitMQ 发送消息
*/
public class Producer_HelloWorld {
public static void main(String[] args) throws IOException, TimeoutException {
//1. 创建链接工厂
ConnectionFactory factory = new ConnectionFactory();
//2. 设置参数
factory.setHost("123.60.156.74"); //ip 默认值: localhost
factory.setPort(5672); //端口 默认值: 5672
factory.setVirtualHost("/"); //虚拟机 默认值: /
factory.setUsername("guest"); //用户名 默认值: guest
factory.setPassword("guest"); //密码 默认值: guest
//3. 创建链接 Connection
Connection connection = factory.newConnection();
//4. 创建 Channel
Channel channel = connection.createChannel();
//5. 创建队列 Queue
/**
* queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
* 1.queue: 队列名称。
* 2.durable: 是否持久化,当rabbitmq重启之后,还在。
* 3.exclusive:
* * 是否独占,只能有一个消费者监听队列。
* * 当Connection关闭时,是否删除队列。
* 4.autoDelete: 是否自动删除,当没有Consumer时,自动删除。
* 5.arguments: 参数。
*/
// 如果没有一个名字叫hello_world的队列,则会创建该队列,如果有则不会创建
channel.queueDeclare("hello_world", true, false, false, null);
//6. 发送消息
/**
* basicPublish(String exchange, String routingKey, AMQP.BasicProperties props, byte[] body)
* 1.exchange:交换机名称。简单模式下交换机会使用默认的 “”
* 2.routingKey:路由名称
* 3.props:配置信息
* 4.body:发送消息数据
*/
String body = "hello rabbitmq~~~";
channel.basicPublish("", "hello_world", null, body.getBytes());
//7. 释放资源
channel.close();
connection.close();
}
}
RabbitMQ管理控制台:
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/859525845776155.png?x-oss-process=image/auto-orient,1/resize,w_320,m_lfit)
运行结果及详细报错内容:
"C:\Program Files\Java\jdk1.8.0_351\bin\java.exe" "-javaagent:D:\AppSourceFile\IntelliJ IDEA 2022.3.2\lib\idea_rt.jar=61186:D:\AppSourceFile\IntelliJ IDEA 2022.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_351\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_351\jre\lib\rt.jar;D:\java-codebase\advanced05\task\rabbitmq\rabbitmq-producer\target\classes;D:\apache-maven-repository\com\rabbitmq\amqp-client\5.13.1\amqp-client-5.13.1.jar;D:\apache-maven-repository\org\slf4j\slf4j-api\1.7.31\slf4j-api-1.7.31.jar" com.xiaoli.producer.Producer_HelloWorld
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:81)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
at java.net.Socket.connect(Socket.java:606)
at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:63)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:160)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1216)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1173)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1131)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1294)
at com.xiaoli.producer.Producer_HelloWorld.main(Producer_HelloWorld.java:26)
进程已结束,退出代码1
线程“main”异常:连接超时,如何解决这个问题?