我正在使用cloudamqp消息服务来尝试MQ(在本例中为RabbitMQ)。我试图从Java类将消息推送到Queue,但它总是说:
拒绝连接
System.getenv也返回null(我也坚持了)。
以下是代码和例外:
String uri = System.getenv("amqp://xxuserxx:password_xxxxxxxxxx@Cloudamqhost/vhost");
if (uri == null) uri = "amqp://guest:guest@localhost";
ConnectionFactory factory = new ConnectionFactory();
try {
factory.setUri(uri);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
//Recommended settings
factory.setRequestedHeartbeat(30);
factory.setConnectionTimeout(60000);
Connection connection = null;
try {
connection = factory.newConnection(); //Getting Null Pointer here
} catch (TimeoutException e) {
e.toString();
堆栈跟踪:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:134)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:997)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:956)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:914)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1068)
当我捕获泛型异常时,它正在捕获空指针异常:
显示java.lang.NullPointerException @connection = factory.newConnection()
答案 0 :(得分:1)
String uri =
System.getenv("amqp://xxuserxx:password_xxxxxxxxxx@Cloudamqhost/vhost");
if (uri == null) uri = "amqp://guest:guest@localhost";
这很奇怪, 要么将uri设置为常量:
String uri = "amqp://xxuserxx:password_xxxxxxxxxx@Cloudamqhost/vhost";
或者您尝试通过名称
获取环境变量的uri值System.getenv("AMQP_CONNECTION_STRING");
if (uri == null) uri = "amqp://guest:guest@localhost";