无法从远程计算机(Android设备)连接到RabbitMQ服务器

时间:2016-12-27 00:01:42

标签: android rabbitmq

我在本地计算机(Windows 10)上启动了RabbitMQ服务器。 localhost上的管理面板:5672运行良好。我已经建立了新用户,并在java中编写了用于发送和接收的示例项目。它适用于localhost和我的IP地址,适用于新建立的用户:

enter image description here

很遗憾我在我的Android项目中编写了相同的配置并出现错误

12-27 00:42:04.601 4393-4393/com.gornik.sendsensors E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.gornik.sendsensors, PID: 4393
                                                                  android.os.NetworkOnMainThreadException
                                                                      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
                                                                      at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
                                                                      at libcore.io.IoBridge.connectErrno(IoBridge.java:154)
                                                                      at libcore.io.IoBridge.connect(IoBridge.java:122)
                                                                      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
                                                                      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
                                                                      at java.net.Socket.connect(Socket.java:884)
                                                                      at com.rabbitmq.client.impl.FrameHandlerFactory.create(FrameHandlerFactory.java:47)
                                                                      at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:822)
                                                                      at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:778)
                                                                      at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:868)
                                                                      at com.gornik.sendsensors.SendMessages.connectAndPublishMessage(SendMessages.java:27)
                                                                      at com.gornik.sendsensors.MainActivity$1.onClick(MainActivity.java:25)
                                                                      at android.view.View.performClick(View.java:5702)
                                                                      at android.view.View$PerformClick.run(View.java:22541)
                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                      at android.os.Looper.loop(Looper.java:158)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

" connectAndPublishMessage"方法:

public static void connectAndPublishMessage(String serverIPAddress) throws IOException{
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("IP ADDRESS");
    factory.setUsername("NEWUSER");
    factory.setPassword("USERPASSWORD");
    factory.setVirtualHost("/");
    factory.setPort(5672);
    try {
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello world";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
        Log.d("MESSAGE SENT: ", " [x] Sent '" + message + "'");

        channel.close();
        connection.close();
    }catch(TimeoutException e){
        e.printStackTrace();
    }
}

你能算出为什么它在我的本地机器和Android设备上有所不同如何将我的Android设备连接到远程RabbitMQ服务器?

2 个答案:

答案 0 :(得分:2)

尝试添加以下两行:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

答案 1 :(得分:1)

感谢@Shubham Agarwal解决问题:) 为了将来的目的 - 为了确保它正常工作,我必须留意一些事情:

  1. 正确配置 - IP地址,端口(默认为5672),虚拟主机(" /")
  2. 默认新用户"来宾"只能连接到localhost
  3. 将queue.declare()函数中的布尔参数更改为true
  4. 添加两行关于线程策略。