RabbitMQ on Android and Java

Alvaro Gomez :

I am trying to reproduce the first example of a Java publisher that can be found in RabbitMQ's main page.

First, I did it in Java and it worked fine. Then, I tried it on Android and here is where the weird part comes.

I have added manually the same jar libraries that I used in my Java program and that are suggested in RabbitMQ's tutorial. That is to say, amqp-client-5.4.1, slf4j-api-1.7.21 and slf4j-simple-1.7.22 are added in /libs directory and then referenced in the buid.gradle (module:app) with the commands implementation files('libs/amqp-client-5.4.1.jar') and so on.

Then, I have added the required package dependencies in my MainActivity.java file without encountering any error. But when adding the piece of code that should publish the data, the different methods of the imported libraries are not found, for instance, factory appears as it did not have the method setHost.

I attach the code bellow I am currently using.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

public class MainActivity extends AppCompatActivity {

    String QUEUE_NAME = "hello";
    ConnectionFactory factory = new ConnectionFactory();

    factory.setHost("192.0.0.0"); //Marked as error
    factory.setUsername("test");
    factory.setPassword("test");
    Connection connection;
    Channel channel;
    connection = factory.newConnection();
    channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    String message = "Example3";
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");
    channel.close();
    connection.close();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Any ideas of why this code is working fine on Java but these libraries fails to be correctly imported in Android?

Benoit :

In java you cannot have code outside of a method. All what you can do is initializing the class members. IMHO it's not a jar import problem.

Try this:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

public class MainActivity extends AppCompatActivity {

    String QUEUE_NAME = "hello";
    ConnectionFactory factory = new ConnectionFactory();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        try {
            factory.setHost("192.0.0.0");
            factory.setUsername("test");
            factory.setPassword("test");
            Connection connection;
            Channel channel;
            connection = factory.newConnection();
            channel = connection.createChannel();

            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Example3";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
            channel.close();
            connection.close();
        } catch (IOException | TimeoutException e) {
            throw new RuntimeException("Rabbitmq problem", e);
        }
    }
}

Coming back to your original concern, I don't see any reason why you manually download all you dependencies rather than using built-in gradle dependency management.

If you update the dependencies section in the build.gradle file, the required dependencies will be automatically downloaded. It's much more easier to add/remove/upgrade dependencies.

dependencies {
    compile group: 'com.rabbitmq', name: 'amqp-client', version: '5.4.1'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.21'
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=36387&siteId=1