连接 RabbitMQ 外部服务时连接被拒绝
Connection refused while connect with RabbitMQ external service
我正在尝试连接 RabbitMQ 外部服务器,但出现以下错误:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
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.ConnectionFactory.createFrameHandler(ConnectionFactory.java:362)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:400)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:423)
at com.towertech.JSONSend.run(JSONSend.java:36)
at com.towertech.JSONSend.main(JSONSend.java:63)
Exception in thread "main" java.lang.NullPointerException
at com.towertech.JSONSend.run(JSONSend.java:54)
at com.towertech.JSONSend.main(JSONSend.java:63)
我的代码如下所示:
public class JSONSend {
private final static String QUEUE_NAME = "json-example";
private ConnectionFactory factory = null;
public JSONSend()
{
// TODO Auto-generated constructor stub
}
@SuppressWarnings("unchecked")
public void run() throws Exception
{
factory = new ConnectionFactory();
Connection connection = null;
Channel channel = null;
try{
factory.setHost("172.26.10.805");
factory.setPort(5672);
factory.setUsername("TEST");
factory.setPassword("TEST@123");
factory.setVirtualHost("TEST");
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
for (int i = 1; i <= 10; i++)
{
JSONObject obj = new JSONObject();
obj.put("message", String.format("Person", i));
obj.put("mask", String.format("TTL", i));
obj.put("destination_address", String.format("03215929139", i));
channel.basicPublish("", QUEUE_NAME, null, obj.toJSONString().getBytes());
System.out.println(" [x] Sent '" + obj.toJSONString() + "'");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally{
if(channel!=null){
channel.close();
connection.close();
}
}
}
public static void main(String[] args) throws Exception
{
// TODO Auto-generated method stub
JSONSend test = new JSONSend();
test.run();
}
}
我的端口被阻止了,在端口打开后我的连接已经建立但是没有发送消息
我正在尝试连接 RabbitMQ 外部服务器,但出现以下错误:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
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.ConnectionFactory.createFrameHandler(ConnectionFactory.java:362)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:400)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:423)
at com.towertech.JSONSend.run(JSONSend.java:36)
at com.towertech.JSONSend.main(JSONSend.java:63)
Exception in thread "main" java.lang.NullPointerException
at com.towertech.JSONSend.run(JSONSend.java:54)
at com.towertech.JSONSend.main(JSONSend.java:63)
我的代码如下所示:
public class JSONSend {
private final static String QUEUE_NAME = "json-example";
private ConnectionFactory factory = null;
public JSONSend()
{
// TODO Auto-generated constructor stub
}
@SuppressWarnings("unchecked")
public void run() throws Exception
{
factory = new ConnectionFactory();
Connection connection = null;
Channel channel = null;
try{
factory.setHost("172.26.10.805");
factory.setPort(5672);
factory.setUsername("TEST");
factory.setPassword("TEST@123");
factory.setVirtualHost("TEST");
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
for (int i = 1; i <= 10; i++)
{
JSONObject obj = new JSONObject();
obj.put("message", String.format("Person", i));
obj.put("mask", String.format("TTL", i));
obj.put("destination_address", String.format("03215929139", i));
channel.basicPublish("", QUEUE_NAME, null, obj.toJSONString().getBytes());
System.out.println(" [x] Sent '" + obj.toJSONString() + "'");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally{
if(channel!=null){
channel.close();
connection.close();
}
}
}
public static void main(String[] args) throws Exception
{
// TODO Auto-generated method stub
JSONSend test = new JSONSend();
test.run();
}
}
我的端口被阻止了,在端口打开后我的连接已经建立但是没有发送消息