Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ on Android and Java

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?

like image 676
Alvaro Gomez Avatar asked Sep 24 '18 21:09

Alvaro Gomez


People also ask

Does RabbitMQ use Java?

The RabbitMQ Java client library allows Java and JVM-based applications to connect to and interact with RabbitMQ nodes. 5. x release series of this library require JDK 8, both for compilation and at runtime. On Android, this means only Android 7.0 or later versions are supported.

Is RabbitMQ bidirectional?

The basic pipe is unidirectional.

What language does RabbitMQ use?

Written in Erlang, the RabbitMQ server is built on the Open Telecom Platform framework for clustering and failover. Client libraries to interface with the broker are available for all major programming languages.

Is RabbitMQ push or pull?

RabbitMQ: Push-based approachRabbitMQ uses a push model and stops overwhelming consumers through a prefetch limit defined on the consumer. This can be used for low latency messaging..


1 Answers

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'
}
like image 172
Benoit Avatar answered Oct 05 '22 23:10

Benoit