如何在RabbitMQ上连接Android?

时间:2015-01-31 19:44:34

标签: android rabbitmq amqp

我想从Android应用程序中使用RabbitMQ客户端。 从服务器端,我使用带弹簧AMQP的SpringBoot。 RabbitMQ(rabbitmq_server-3.4.3)已正确安装,集成测试可验证服务器行为。

困难的部分是当我尝试在我的android项目中从RabbitMQ connectionFactory创建连接时。

我得到了这个例外:

failed to connect to localhost/127.0.0.1 (port 5672): 
connect failed: ECONNREFUSED (Connection refused)

在android清单中,设置了Internet权限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

这是我的代码:

public class LoadingTask extends AsyncTask<String, Integer, Integer> {

    private static String replyQueue = "REPLY_QUEUE";

    public interface LoadingTaskListener {
        void onResourceLoaded();
    }

    private final ProgressBar progressBar;
    private final LoadingTaskListener loadingTaskListener;
    private ConnectionFactory connectionFactory;

    public LoadingTask(ProgressBar progressBar, LoadingTaskListener loadingTaskListener) {
        this.progressBar = progressBar;
        this.loadingTaskListener = loadingTaskListener;
        this.connectionFactory = new ConnectionFactory();
        connectionFactory.setAutomaticRecoveryEnabled(true);
    }

    @Override
    protected Integer doInBackground(String... params) {
        try {

            /*init rabbitMQ Context*/
            Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
            channel.basicQos(1);
            channel.queueDeclare(DATA_QUEUE.getName(), false, false, false, null);
            AMQP.Queue.DeclareOk q = channel.queueDeclare();
            channel.queueBind(q.getQueue(), "amq.fanout", "chat");
            QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume(replyQueue, true, consumer);


            /* initiate RPC */
            String corrId = java.util.UUID.randomUUID().toString();

            AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
                .correlationId(corrId)
                .replyTo(replyQueue)
                .build();

            AmqpRequest request = new AmqpRequest(LIST_IMAGE);
            Gson gson = new Gson();
            String jsonRequest = gson.toJson(request);

            channel.basicPublish(MAIN_EXCHANGE.getName(), DATA_QUEUE.getRoutingKey(), props, jsonRequest.getBytes());

            String response;

            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                if (delivery.getProperties().getCorrelationId().equals(corrId)) {
                    response = new String(delivery.getBody());
                    break;
                }
            }

            System.out.println(response);

        } catch (IOException e) {
             e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // Dummy Data until i can't connect to rabbitMQ
        return 1234;
    }
}

真的可以在RabbitMQ上连接Android,还是应该使用某种http桥? 任何人都可以为我提供http桥接或RabbitMQ连接的例子。

谢谢

0 个答案:

没有答案